Class: Wrappi::Response
- Inherits:
-
Object
show all
- Defined in:
- lib/wrappi/response.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(endpoint_klass, &block) ⇒ Response
Returns a new instance of Response.
8
9
10
11
|
# File 'lib/wrappi/response.rb', line 8
def initialize(endpoint_klass, &block)
@endpoint_klass = endpoint_klass
@block = block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/wrappi/response.rb', line 69
def method_missing(method_name, *arguments, &block)
if request.respond_to?(method_name)
request.send(method_name, *arguments, &block)
else
super
end
end
|
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
7
8
9
|
# File 'lib/wrappi/response.rb', line 7
def block
@block
end
|
#endpoint_klass ⇒ Object
Returns the value of attribute endpoint_klass.
7
8
9
|
# File 'lib/wrappi/response.rb', line 7
def endpoint_klass
@endpoint_klass
end
|
Instance Method Details
#body ⇒ Object
23
24
25
|
# File 'lib/wrappi/response.rb', line 23
def body
@body ||= parse
end
|
#called? ⇒ Boolean
19
20
21
|
# File 'lib/wrappi/response.rb', line 19
def called?
!!@request
end
|
#error? ⇒ Boolean
31
32
33
|
# File 'lib/wrappi/response.rb', line 31
def error?
!success?
end
|
#json_parse ⇒ Object
63
64
65
66
67
|
# File 'lib/wrappi/response.rb', line 63
def json_parse
JSON.parse(raw_body)
rescue
raw_body
end
|
#parse ⇒ Object
57
58
59
60
61
|
# File 'lib/wrappi/response.rb', line 57
def parse
request.parse
rescue
json_parse
end
|
#raw_body ⇒ Object
35
36
37
|
# File 'lib/wrappi/response.rb', line 35
def raw_body
@raw_body ||= request.to_s
end
|
#request ⇒ Object
Also known as:
call
13
14
15
16
|
# File 'lib/wrappi/response.rb', line 13
def request
@request ||= block.call
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
77
78
79
|
# File 'lib/wrappi/response.rb', line 77
def respond_to_missing?(method_name, include_private = false)
request.respond_to?(method_name) || super
end
|
#status ⇒ Object
Also known as:
status_code
43
44
45
|
# File 'lib/wrappi/response.rb', line 43
def status
request.code
end
|
#success? ⇒ Boolean
27
28
29
|
# File 'lib/wrappi/response.rb', line 27
def success?
@success ||= endpoint_klass.success?(request)
end
|
#to_h ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/wrappi/response.rb', line 48
def to_h
@to_h ||= {
raw_body: raw_body,
code: code,
uri: uri,
success: success?
}
end
|
#uri ⇒ Object
39
40
41
|
# File 'lib/wrappi/response.rb', line 39
def uri
request.uri.to_s
end
|