Class: Patronage::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/patronage/response.rb

Constant Summary collapse

Parsers =
{
  /^application\/xml/ => lambda { |body| Hash.from_xml(body) },
  /^text\/xml/ => lambda { |body| Hash.from_xml(body) },
}

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



8
9
10
# File 'lib/patronage/response.rb', line 8

def initialize(response)
  @response = response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_name, *args) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/patronage/response.rb', line 12

def method_missing(meth_name, *args)
  if @response.public_methods.include?(meth_name.to_s)
    @response.send(meth_name, *args)
  else
    super
  end
end

Instance Method Details

#dataObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/patronage/response.rb', line 20

def data
  unless @data
    mime_type = @response.headers["Content-Type"] || @response.headers["Content-type"] || @response.headers["content-Type"]
    parser    = Parsers.keys.find { |matcher| matcher === mime_type }
    
    if parser
      @data = Parsers[parser].call(@response.body)
    else
      raise ArgumentError, "unknown parser for #{mime_type}"
    end
  end
  @data
end