Sha256: b249ea141c5362d34f457ee4c34d2184cd54a0116e02cd0d170e68b71f986ab9

Contents?: true

Size: 706 Bytes

Versions: 2

Compression:

Stored size: 706 Bytes

Contents

module RESTinPeace
  class ResponseConverter
    def initialize(response, klass)
      @response = response
      @klass = klass
    end

    def result
      case @response.body.class.to_s
      when 'Array'
        convert_from_array
      when 'Hash'
        convert_from_hash
      when 'String'
        @response.body
      else
        raise "Don't know how to convert #{@response.body.class}"
      end
    end

    def convert_from_array
      @response.body.map do |entity|
        convert_from_hash(entity)
      end
    end

    def convert_from_hash(entity = @response.body)
      klass.new entity
    end

    def klass
      @klass.respond_to?(:new) ? @klass : @klass.class
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-in-peace-1.4.0 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.3.1 lib/rest_in_peace/response_converter.rb