Class: ActionKitRest::Response::Wrapper
- Inherits:
-
Object
- Object
- ActionKitRest::Response::Wrapper
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/action_kit_rest/response/wrapper.rb
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup an attribute from the body if hash, otherwise behave like array index.
-
#body ⇒ Object
Response raw body.
- #body=(value) ⇒ Object
- #client_error? ⇒ Boolean
- #collection? ⇒ Boolean
-
#each(&block) ⇒ Object
Iterate over each resource inside the collection.
-
#initialize(response) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#method_missing(method_name, *args, &block) ⇒ Object
if a raw object, just delegate.
- #object? ⇒ Boolean
- #redirect? ⇒ Boolean
- #server_error? ⇒ Boolean
-
#status ⇒ Object
Response status.
- #success? ⇒ Boolean
-
#to_ary ⇒ Object
Convert the ResponseWrapper into an Array.
-
#to_hash ⇒ Object
Convert the ResponseWrapper into a Hash.
-
#to_s ⇒ Object
Return response body as string.
-
#url ⇒ Object
Request url.
Constructor Details
#initialize(response) ⇒ Wrapper
Returns a new instance of Wrapper.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 13 def initialize(response) @response = response if response.body.respond_to?(:meta) && response.body. @kind = :collection @obj = ActionKitRest::Response::Collection.new(response.body., response.body.objects) else @kind = :object @obj = response.body end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
if a raw object, just delegate
99 100 101 102 103 104 105 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 99 def method_missing(method_name, *args, &block) if object? obj.send(method_name, &block) else super end end |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
8 9 10 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 8 def kind @kind end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
9 10 11 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 9 def obj @obj end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 7 def response @response end |
Instance Method Details
#[](key) ⇒ Object
Lookup an attribute from the body if hash, otherwise behave like array index. Convert any key to string before calling.
72 73 74 75 76 77 78 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 72 def [](key) if self.body.is_a?(Array) self.body[key] else self.body.send(:"#{key}") end end |
#body ⇒ Object
Response raw body
44 45 46 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 44 def body @body ? @body : response.body end |
#body=(value) ⇒ Object
38 39 40 41 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 38 def body=(value) @body = value @env[:body] = value end |
#client_error? ⇒ Boolean
61 62 63 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 61 def client_error? status.to_i >= 400 && status.to_i < 500 end |
#collection? ⇒ Boolean
25 26 27 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 25 def collection? kind == :collection end |
#each(&block) ⇒ Object
Iterate over each resource inside the collection
109 110 111 112 113 114 115 116 117 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 109 def each(&block) if collection? obj.each do |o| block.call(o) end else raise("can only iterate over collections") end end |
#object? ⇒ Boolean
29 30 31 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 29 def object? kind == :object end |
#redirect? ⇒ Boolean
57 58 59 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 57 def redirect? status.to_i >= 300 && status.to_i < 400 end |
#server_error? ⇒ Boolean
65 66 67 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 65 def server_error? status.to_i >= 500 && status.to_i < 600 end |
#status ⇒ Object
Response status
49 50 51 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 49 def status response.status end |
#success? ⇒ Boolean
53 54 55 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 53 def success? response.success? end |
#to_ary ⇒ Object
Convert the ResponseWrapper into an Array
94 95 96 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 94 def to_ary body.to_ary end |
#to_hash ⇒ Object
Convert the ResponseWrapper into a Hash
88 89 90 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 88 def to_hash body.to_hash end |
#to_s ⇒ Object
Return response body as string
82 83 84 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 82 def to_s body.to_s end |
#url ⇒ Object
Request url
34 35 36 |
# File 'lib/action_kit_rest/response/wrapper.rb', line 34 def url response.env[:url].to_s end |