Class: HttpMimic::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Response

Returns a new instance of Response.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/http_mimic/response.rb', line 26

def initialize(attributes = {})
  @code               = attributes[:code] || 0
  @http_version       = attributes[:http_version]
  @status_message     = attributes[:status_message]
  @headers            = attributes[:headers] || Headers.new
  @cookies            = attributes[:cookies] || Cookies.new
  @body               = attributes[:body] || ''
  @parsed_response    = attributes[:parsed_response]
  @raw_headers        = attributes[:raw_headers] || ''
  @history            = attributes[:history] || []
  @request_url        = attributes[:request_url]
  @stderr             = attributes[:stderr] || ''
  @exit_code          = attributes[:exit_code] || 0
  @command            = attributes[:command]
  @mode_used          = attributes[:mode_used] || :impersonate
  @fallback_triggered = attributes[:fallback_triggered] || false
  @attempts           = attributes[:attempts] || []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



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

def method_missing(method_name, *args, &block)
  if parsed_response.respond_to?(method_name)
    parsed_response.public_send(method_name, *args, &block)
  elsif body.respond_to?(method_name)
    body.public_send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#attemptsObject

Returns the value of attribute attempts.



19
20
21
# File 'lib/http_mimic/response.rb', line 19

def attempts
  @attempts
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly) Also known as: status

Returns the value of attribute code.



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

def code
  @code
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



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

def cookies
  @cookies
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



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

def exit_code
  @exit_code
end

#fallback_triggeredObject

Returns the value of attribute fallback_triggered.



19
20
21
# File 'lib/http_mimic/response.rb', line 19

def fallback_triggered
  @fallback_triggered
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



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

def http_version
  @http_version
end

#mode_usedObject Also known as: strategy_used

Returns the value of attribute mode_used.



19
20
21
# File 'lib/http_mimic/response.rb', line 19

def mode_used
  @mode_used
end

#parsed_responseObject (readonly)

Returns the value of attribute parsed_response.



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

def parsed_response
  @parsed_response
end

#raw_headersObject (readonly)

Returns the value of attribute raw_headers.



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

def raw_headers
  @raw_headers
end

#request_urlObject (readonly)

Returns the value of attribute request_url.



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

def request_url
  @request_url
end

#status_messageObject (readonly)

Returns the value of attribute status_message.



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

def status_message
  @status_message
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  if parsed_response.is_a?(Hash) || parsed_response.is_a?(Array)
    parsed_response[key]
  else
    nil
  end
end

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  body.nil? || body.strip.empty?
end

#client_error?Boolean

Returns:

  • (Boolean)


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

def client_error?
  code >= 400 && code < 500
end

#error?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/http_mimic/response.rb', line 66

def error?
  client_error? || server_error?
end

#fallback_triggered?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/http_mimic/response.rb', line 45

def fallback_triggered?
  !!@fallback_triggered
end

#inspectObject



91
92
93
# File 'lib/http_mimic/response.rb', line 91

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @code=#{code} @status_message=#{status_message.inspect} @headers=#{headers.to_h.inspect} @parsed_response=#{parsed_response.inspect}>"
end

#present?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/http_mimic/response.rb', line 87

def present?
  !blank?
end

#redirect?Boolean

Returns:

  • (Boolean)


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

def redirect?
  code >= 300 && code < 400
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/http_mimic/response.rb', line 105

def respond_to_missing?(method_name, include_private = false)
  parsed_response.respond_to?(method_name, include_private) ||
    body.respond_to?(method_name, include_private) ||
    super
end

#server_error?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/http_mimic/response.rb', line 62

def server_error?
  code >= 500 && code < 600
end

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


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

def success?
  code >= 200 && code < 300
end

#to_sObject Also known as: to_str



78
79
80
# File 'lib/http_mimic/response.rb', line 78

def to_s
  body.to_s
end