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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/httparty/response.rb', line 17

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)



125
126
127
128
129
130
131
132
133
# File 'lib/httparty/response.rb', line 125

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.



15
16
17
# File 'lib/httparty/response.rb', line 15

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/httparty/response.rb', line 15

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



15
16
17
# File 'lib/httparty/response.rb', line 15

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



15
16
17
# File 'lib/httparty/response.rb', line 15

def response
  @response
end

Class Method Details

._load(data) ⇒ Object



9
10
11
12
13
# File 'lib/httparty/response.rb', line 9

def self._load(data)
  req, resp, parsed_resp, resp_body = Marshal.load(data)

  new(req, resp, -> { parsed_resp }, body: resp_body)
end

.underscore(string) ⇒ Object



5
6
7
# File 'lib/httparty/response.rb', line 5

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

#_dump(_level) ⇒ Object



119
120
121
# File 'lib/httparty/response.rb', line 119

def _dump(_level)
  Marshal.dump([request, response, parsed_response, body])
end

#codeObject



40
41
42
# File 'lib/httparty/response.rb', line 40

def code
  response.code.to_i
end

#display(port = $>) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/httparty/response.rb', line 104

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

#http_versionObject



44
45
46
# File 'lib/httparty/response.rb', line 44

def http_version
  response.http_version
end

#inspectObject



53
54
55
56
# File 'lib/httparty/response.rb', line 53

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)


83
84
85
86
# File 'lib/httparty/response.rb', line 83

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

#parsed_responseObject



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

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

#pretty_print(pp) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/httparty/response.rb', line 96

def pretty_print(pp)
  if !parsed_response.nil? && parsed_response.respond_to?(:pretty_print)
    parsed_response.pretty_print(pp)
  else
    super
  end
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/httparty/response.rb', line 114

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:



48
49
50
51
# File 'lib/httparty/response.rb', line 48

def tap
  yield self
  self
end

#to_sObject



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

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