Class: HTTParty::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/response/headers.rb,
lib/httparty/response.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Headers

Constant Summary collapse

CODES_TO_OBJ =
::Net::HTTPResponse::CODE_CLASS_TO_OBJ.merge ::Net::HTTPResponse::CODE_TO_OBJ

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response, parsed_block, options = {}) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/httparty/response.rb', line 9

def initialize(request, response, parsed_block, options = {})
  @request      = request
  @response     = response
  @body         = options[:body] || response.body
  @parsed_block = parsed_block
  @headers      = Headers.new(response.to_hash)

  if request.options[:logger]
    logger = ::HTTParty::Logger.build(request.options[:logger], request.options[:log_level], request.options[:log_format])
    logger.format(request, self)
  end

  throw_exception
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



87
88
89
90
91
92
93
94
95
# File 'lib/httparty/response.rb', line 87

def method_missing(name, *args, &block)
  if parsed_response.respond_to?(name)
    parsed_response.send(name, *args, &block)
  elsif response.respond_to?(name)
    response.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/httparty/response.rb', line 7

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/httparty/response.rb', line 7

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/httparty/response.rb', line 7

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/httparty/response.rb', line 7

def response
  @response
end

Class Method Details

.underscore(string) ⇒ Object



3
4
5
# File 'lib/httparty/response.rb', line 3

def self.underscore(string)
  string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

Instance Method Details

#codeObject



28
29
30
# File 'lib/httparty/response.rb', line 28

def code
  response.code.to_i
end

#display(port = $>) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/httparty/response.rb', line 70

def display(port=$>)
  if !parsed_response.nil? && parsed_response.respond_to?(:display)
    parsed_response.display(port)
  elsif !response.nil? && !response.body.nil? && response.body.respond_to?(:display)
    response.body.display(port)
  else 
    port.write(inspect)
  end
end

#inspectObject



37
38
39
40
# File 'lib/httparty/response.rb', line 37

def inspect
  inspect_id = ::Kernel::format "%x", (object_id * 2)
  %(#<#{self.class}:0x#{inspect_id} parsed_response=#{parsed_response.inspect}, @response=#{response.inspect}, @headers=#{headers.inspect}>)
end

#nil?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/httparty/response.rb', line 58

def nil?
  response.nil? || response.body.nil? || response.body.empty?
end

#parsed_responseObject



24
25
26
# File 'lib/httparty/response.rb', line 24

def parsed_response
  @parsed_response ||= @parsed_block.call
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/httparty/response.rb', line 80

def respond_to_missing?(name, *args)
  return true if super
  parsed_response.respond_to?(name) || response.respond_to?(name)
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
# File 'lib/httparty/response.rb', line 32

def tap
  yield self
  self
end

#to_sObject



62
63
64
65
66
67
68
# File 'lib/httparty/response.rb', line 62

def to_s      
  if !response.nil? && !response.body.nil? && response.body.respond_to?(:to_s)
    response.body.to_s
  else 
    inspect
  end
end