module ScientificProtocols::Resources::Object::Serializers::ClassMethods

Public Instance Methods

deserialize(response) click to toggle source

Deserialize a Faraday response. @param [Faraday::Response] response. @raise [ArgumentError] If the response is blank. @return [Object, nil].

# File lib/scientificprotocols/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
Also aliased as: parse
parse(response)
Alias for: deserialize
serialize(object) click to toggle source
# File lib/scientificprotocols/resources/object/serializers.rb, line 82
def serialize(object)
  object.serialize
end
serializer_for(type) click to toggle source

@param [String, Symbol] type type of attribute to be serialized or deserialized @return [#serialize, deserialize] serializer for provided type

# File lib/scientificprotocols/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() click to toggle source

@return [Hash] corresponding serializers for different attributes

# File lib/scientificprotocols/resources/object/serializers.rb, line 35
def serializers
  @serializers ||= {}
end