Class: Response::Resource

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/response/resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, resource = nil) ⇒ Resource

Returns a new instance of Resource.



14
15
16
17
18
# File 'lib/faraday/response/resource.rb', line 14

def initialize(app, resource=nil)
  super app
  @resource = resource
  @parser = nil
end

Instance Method Details

#on_complete(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/faraday/response/resource.rb', line 3

def on_complete(env)
  if @resource
    response_body = env[:body]
    if response_body.is_a?(Hash)
      env[:body] = @resource.new(response_body)
    elsif response_body.is_a?(Array)
      env[:body] = response_body.map{|item| item.is_a?(Hash) ? @resource.new(item) : item}
    end
  end
end