Module: Zenodo::Resources::Object::Serializers::ClassMethods
- Defined in:
- lib/zenodo/resources/object/serializers.rb
Instance Method Summary collapse
-
#deserialize(response) ⇒ Object?
(also: #parse)
Deserialize a Faraday response.
- #serialize(object) ⇒ Object
-
#serializer_for(type) ⇒ #serialize, #deserialize
Serializer for provided type.
-
#serializers ⇒ Hash
Corresponding serializers for different attributes.
Instance Method Details
#deserialize(response) ⇒ Object? Also known as: parse
Deserialize a Faraday response.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/zenodo/resources/object/serializers.rb', line 61 def deserialize(response) raise ArgumentError, "Response cannot be blank" if response.blank? attributes = response.body begin attributes = JSON.parse(response.body) case attributes when Array return attributes.map { |object| new(object) } when Hash return new(attributes) end rescue JSON::ParserError logger = Logger.new(STDOUT) logger.error("Could not parse: #{response.body}") end nil end |
#serialize(object) ⇒ Object
82 83 84 |
# File 'lib/zenodo/resources/object/serializers.rb', line 82 def serialize(object) object.serialize end |
#serializer_for(type) ⇒ #serialize, #deserialize
Returns serializer for provided type.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zenodo/resources/object/serializers.rb', line 41 def serializer_for(type) serializers[type] ||= begin class_symbol = type.to_s.to_sym if type.respond_to?(:deserialize) && type.respond_to?(:serialize) type elsif Serializers.constants.include?(class_symbol) Serializers.const_get(class_symbol) elsif Resources.constants.include?(class_symbol) Resources.const_get(class_symbol) else Serializers::Object end end end |
#serializers ⇒ Hash
Returns corresponding serializers for different attributes.
35 36 37 |
# File 'lib/zenodo/resources/object/serializers.rb', line 35 def serializers @serializers ||= {} end |