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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/http_mimic/response.rb', line 21

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]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



83
84
85
86
87
88
89
90
91
# File 'lib/http_mimic/response.rb', line 83

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

#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

#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

#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



58
59
60
61
62
63
64
# File 'lib/http_mimic/response.rb', line 58

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

#blank?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/http_mimic/response.rb', line 71

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

#client_error?Boolean

Returns:

  • (Boolean)


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

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

#error?Boolean

Returns:

  • (Boolean)


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

def error?
  client_error? || server_error?
end

#inspectObject



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

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)


75
76
77
# File 'lib/http_mimic/response.rb', line 75

def present?
  !blank?
end

#redirect?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/http_mimic/response.rb', line 42

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

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

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/http_mimic/response.rb', line 93

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)


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

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

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


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

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

#to_sObject Also known as: to_str



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

def to_s
  body.to_s
end