Sha256: 4e713c26cf57b6755ed61a39d819042e733245e42c0b378e9b55a9a02d499545

Contents?: true

Size: 663 Bytes

Versions: 6

Compression:

Stored size: 663 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
      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

6 entries across 6 versions & 1 rubygems

Version Path
rest-in-peace-1.3.0 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.2.1 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.2.0 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.1.1 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.1.0 lib/rest_in_peace/response_converter.rb
rest-in-peace-1.0.0 lib/rest_in_peace/response_converter.rb