class ActivityStreams::Factory
Public Class Methods
new(json)
click to toggle source
# File lib/activity_streams/factory.rb, line 7 def initialize(json) @json = json.freeze end
Public Instance Methods
build()
click to toggle source
# File lib/activity_streams/factory.rb, line 11 def build hash = JSON.parse(@json) obj = deep_initialize(hash) obj.original_json = @json obj end
Private Instance Methods
deep_initialize(object)
click to toggle source
# File lib/activity_streams/factory.rb, line 20 def deep_initialize(object) case object when Hash then object['type'] ? transform_values(object) : object when Array then object.map { |o| deep_initialize(o) } else object end end
find_klass(type)
click to toggle source
# File lib/activity_streams/factory.rb, line 28 def find_klass(type) case type when 'Add' then Activity::Add when 'Collection' then Collection when 'CollectionPage' then Collection::CollectionPage when 'Create' then Activity::Create when 'Link' then Link when 'Note' then Object::Note when 'Image' then Object::Image when 'Person' then Actor::Person when 'RsaSignature2017' then Extensions::LinkedDataSignature else raise UnsupportedType, type end end
transform_values(hash)
click to toggle source
# File lib/activity_streams/factory.rb, line 43 def transform_values(hash) attrs = hash.transform_values { |v| deep_initialize(v) } klass = find_klass(hash['type']) unsupported_properties = unsupported_properties(klass, attrs) obj = klass.new(attrs) obj.unsupported_properties = unsupported_properties obj end
unsupported_properties(klass, attrs)
click to toggle source
# File lib/activity_streams/factory.rb, line 54 def unsupported_properties(klass, attrs) attrs.each.with_object({}) do |(k, _v), unsupported_props| if !klass.attribute_types.keys.include?(k) unsupported_props[k] = attrs.delete(k) end end end