Class: HttpMimic::Response
- Inherits:
-
Object
- Object
- HttpMimic::Response
- Defined in:
- lib/http_mimic/response.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
(also: #status)
readonly
Returns the value of attribute code.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#fallback_triggered ⇒ Object
Returns the value of attribute fallback_triggered.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#http_version ⇒ Object
readonly
Returns the value of attribute http_version.
-
#mode_used ⇒ Object
(also: #strategy_used)
Returns the value of attribute mode_used.
-
#parsed_response ⇒ Object
readonly
Returns the value of attribute parsed_response.
-
#raw_headers ⇒ Object
readonly
Returns the value of attribute raw_headers.
-
#request_url ⇒ Object
readonly
Returns the value of attribute request_url.
-
#status_message ⇒ Object
readonly
Returns the value of attribute status_message.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #blank? ⇒ Boolean
- #client_error? ⇒ Boolean
- #error? ⇒ Boolean
- #fallback_triggered? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
- #present? ⇒ Boolean
- #redirect? ⇒ Boolean
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #server_error? ⇒ Boolean
- #success? ⇒ Boolean (also: #ok?)
- #to_s ⇒ Object (also: #to_str)
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
#attempts ⇒ Object
Returns the value of attribute attempts.
19 20 21 |
# File 'lib/http_mimic/response.rb', line 19 def attempts @attempts end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def body @body end |
#code ⇒ Object (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 |
#command ⇒ Object (readonly)
Returns the value of attribute command.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def command @command end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def @cookies end |
#exit_code ⇒ Object (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_triggered ⇒ Object
Returns the value of attribute fallback_triggered.
19 20 21 |
# File 'lib/http_mimic/response.rb', line 19 def fallback_triggered @fallback_triggered end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def headers @headers end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def history @history end |
#http_version ⇒ Object (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_used ⇒ Object 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_response ⇒ Object (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_headers ⇒ Object (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_url ⇒ Object (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_message ⇒ Object (readonly)
Returns the value of attribute status_message.
5 6 7 |
# File 'lib/http_mimic/response.rb', line 5 def @status_message end |
#stderr ⇒ Object (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
83 84 85 |
# File 'lib/http_mimic/response.rb', line 83 def blank? body.nil? || body.strip.empty? end |
#client_error? ⇒ Boolean
58 59 60 |
# File 'lib/http_mimic/response.rb', line 58 def client_error? code >= 400 && code < 500 end |
#error? ⇒ Boolean
66 67 68 |
# File 'lib/http_mimic/response.rb', line 66 def error? client_error? || server_error? end |
#fallback_triggered? ⇒ Boolean
45 46 47 |
# File 'lib/http_mimic/response.rb', line 45 def fallback_triggered? !!@fallback_triggered end |
#inspect ⇒ Object
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=#{.inspect} @headers=#{headers.to_h.inspect} @parsed_response=#{parsed_response.inspect}>" end |
#present? ⇒ Boolean
87 88 89 |
# File 'lib/http_mimic/response.rb', line 87 def present? !blank? end |
#redirect? ⇒ 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
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
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?
49 50 51 |
# File 'lib/http_mimic/response.rb', line 49 def success? code >= 200 && code < 300 end |
#to_s ⇒ Object Also known as: to_str
78 79 80 |
# File 'lib/http_mimic/response.rb', line 78 def to_s body.to_s end |