Commit 0ddf825d authored by James Lopez's avatar James Lopez

WIP - adding a generic entity serializer that should accept a Hash coming from Arel

parent 3b179bc3
class AnalyticsGenericEntity < Grape::Entity
include RequestAwareEntity
include ActionView::Helpers::DateHelper
expose :title
expose :iid
expose :started_at, as: :date
expose :author, using: UserEntity
expose :total_time do |object|
distance_of_time_in_words(object[:total_time].to_f)
end
expose(:date) do |object|
interval_in_words(object[:created_at])
end
expose :url do |object|
url_to("namespace_project_#{object[:entity]}".to_sym, id: object[:iid].to_s)
end
private
def url_to(route, id = nil)
public_send("#{route}_url", options[:project].namespace, options[:project], id)
end
def interval_in_words(diff)
"#{distance_of_time_in_words(diff.to_f)} ago"
end
end
class AnalyticsGenericSerializer < BaseSerializer
entity AnalyticsGenericEntity
end
require 'spec_helper'
describe AnalyticsGenericEntity do
let(:user) { create(:user) }
let(:entity_hash) {
{
total_time: "172802.724419",
title: "Eos voluptatem inventore in sed.",
iid: "1",
id: "1",
created_at: "2016-11-12 15:04:02.948604",
author: user,
entity: :merge_request
}
}
let(:project) { create(:empty_project) }
let(:entity) do
described_class.new(entity_hash, request: double, project: project)
end
context 'generic entity' do
subject { entity.as_json }
it 'contains the entity URL' do
expect(subject).to include(:url)
end
it 'contains the author' do
expect(subject).to include(:author)
end
it 'does not contain sensitive information' do
expect(subject).not_to include(/token/)
expect(subject).not_to include(/variables/)
end
end
end
require 'spec_helper'
describe AnalyticsBuildSerializer do
let(:serializer) do
described_class
.new(project: project)
.represent(resource)
end
let(:json) { serializer.as_json }
let(:project) { create(:project) }
let(:resource) { create(:ci_build) }
context 'when there is a single object provided' do
it 'it generates payload for single object' do
expect(json).to be_an_instance_of Hash
end
it 'contains important elements of analyticsBuild' do
expect(json)
.to include(:name, :branch, :short_sha, :date, :total_time, :url, :branch_url, :commit_url, :author)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment