class ZuoraConnectUi::Serializer
Take a resource and dump it to Google Style JSON, really fast
Constants
- DATA_RESERVED
Reserved fields in the Data object kind the resource's type fields comma separated list of attributes given etag items if @resource is a collection, then items is the array of resources
- TOP_LEVEL_RESERVED
Public Class Methods
new(resource, options = {})
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 9 def initialize(resource, options = {}) @resource = resource @kind = options[:kind] @is_collection = options[:is_collection] @record_type = parse_record_type(@kind, @resource) @camel_type = key_transform(@record_type) @cache_keys = {} @associations = parse_associations( options[:has_many], options[:belongs_to], options[:has_one] ) @fields = parse_fields(options[:fields]) # TODO: if an attribute given is reserved, throw an error end
Public Instance Methods
hash_for_collection()
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 54 def hash_for_collection hash = { data: { items: [] } } @resource.each do |resource| hash[:data][:items] << record_hash(resource, @record_type) end return hash if @associations.empty? hash[:data][:relationships] = get_associations(@resource) hash end
hash_for_one()
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 40 def hash_for_one hash = { data: nil } return hash unless @resource hash[:data] = record_hash(@resource, @record_type) return hash if @associations.empty? hash[:data][:relationships] = get_associations([@resource]) hash end
serializable_hash()
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 34 def serializable_hash return hash_for_collection if collection?(@resource) hash_for_one end
serialized_json()
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 27 def serialized_json require 'oj' # Enable to show Oj working in console # Oj.default_options = { trace: true } Oj.dump(serializable_hash, mode: :compat, time_format: :ruby) end
Private Instance Methods
collection?(resource)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 74 def collection?(resource) return @is_collection unless @is_collection.nil? resource.respond_to?(:size) && !resource.respond_to?(:each_pair) end
create_association(key, relationship_type)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 127 def create_association(key, relationship_type) Relationship.new( key: key, relationship_type: relationship_type ) end
filter_associations(fields)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 115 def filter_associations(fields) fields.select { |field| @associations.none? { |a| a.key == field } } end
get_associations(resource)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 95 def get_associations(resource) associations = {} @associations.each do |a| hash_key = key_transform(a.plural_type) associations[hash_key] = a.serialize(resource, @fields[a.record_type]) end associations end
key_transform(input)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 70 def key_transform(input) input.to_s.camelize(:lower).to_sym end
parse_associations(has_many, belongs_to, has_one)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 119 def parse_associations(has_many, belongs_to, has_one) [ *(has_many || []).map { |k| create_association(k, :has_many) }, *(belongs_to || []).map { |k| create_association(k, :belongs_to) }, *(has_one || []).map { |k| create_association(k, :has_one) } ] end
parse_fields(fields)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 106 def parse_fields(fields) if fields.is_a? Array Hash[@record_type, filter_associations(fields)] elsif fields.is_a? Hash fields[@record_type] = filter_associations(fields[@record_type]) fields end end
parse_record_type(kind, resource)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 134 def parse_record_type(kind, resource) return kind.to_sym if kind return resource.class.name.underscore.to_sym unless collection?(resource) resource.first.class.name.underscore.to_sym end
record_hash(resource, record_type)
click to toggle source
# File lib/zuora_connect_ui/serializer.rb, line 80 def record_hash(resource, record_type) hash = @kind.nil? ? {} : { kind: @camel_type } hash[:id] = resource.id @fields[record_type].each do |field| field_key = @cache_keys[field] ||= key_transform(field) hash[field_key] = resource.public_send(field) end @associations.each do |association| field = association.id_method_name camel_key = @cache_keys[field] ||= key_transform(field) hash[camel_key] = resource.public_send(field) end hash end