Class: Nimbu::Response::Wrapper
- Inherits:
-
Object
- Object
- Nimbu::Response::Wrapper
- Extended by:
- Forwardable
- Includes:
- Enumerable, Pagination
- Defined in:
- lib/nimbu-api/response/wrapper.rb
Overview
A class responsible for proxing to faraday response
Constant Summary
Constants included from Utils::Constants
Utils::Constants::ACCEPT, Utils::Constants::ACCEPTED_OAUTH_SCOPES, Utils::Constants::ACCEPT_CHARSET, Utils::Constants::CACHE_CONTROL, Utils::Constants::CONTENT_LENGTH, Utils::Constants::CONTENT_TYPE, Utils::Constants::DATE, Utils::Constants::ETAG, Utils::Constants::HEADER_LAST, Utils::Constants::HEADER_LINK, Utils::Constants::HEADER_NEXT, Utils::Constants::LOCATION, Utils::Constants::META_FIRST, Utils::Constants::META_LAST, Utils::Constants::META_NEXT, Utils::Constants::META_PREV, Utils::Constants::META_REL, Utils::Constants::NIMBU_SITE, Utils::Constants::OAUTH_SCOPES, Utils::Constants::PARAM_PAGE, Utils::Constants::PARAM_PER_PAGE, Utils::Constants::PARAM_START_PAGE, Utils::Constants::RATELIMIT_LIMIT, Utils::Constants::RATELIMIT_REMAINING, Utils::Constants::SERVER, Utils::Constants::USER_AGENT
Instance Attribute Summary collapse
-
#current_api ⇒ Object
readonly
Returns the value of attribute current_api.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare the wrapper with other wrapper for equality.
-
#[](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
-
#each ⇒ Object
Iterate over each resource inside the body.
-
#has_key?(key) ⇒ Boolean
Check if body has an attribute.
-
#headers ⇒ Object
Return response headers.
-
#initialize(response, current_api) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#inspect ⇒ Object
Print only response body.
- #last ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes.
- #redirect? ⇒ Boolean
-
#respond_to?(method_name) ⇒ Boolean
Check if method is defined on the body.
- #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.
Methods included from Pagination
#auto_paginate, #count_pages, #each_page, #first_page, #has_next_page?, #last_page, #links, #next_page, #page, #prev_page
Constructor Details
#initialize(response, current_api) ⇒ Wrapper
Returns a new instance of Wrapper.
17 18 19 20 21 |
# File 'lib/nimbu-api/response/wrapper.rb', line 17 def initialize(response, current_api) @response = response @current_api = current_api @env = response.env end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes
117 118 119 120 121 122 123 |
# File 'lib/nimbu-api/response/wrapper.rb', line 117 def method_missing(method_name, *args, &block) if self.has_key?(method_name.to_s) self.[](method_name, &block) else super end end |
Instance Attribute Details
#current_api ⇒ Object (readonly)
Returns the value of attribute current_api.
12 13 14 |
# File 'lib/nimbu-api/response/wrapper.rb', line 12 def current_api @current_api end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
13 14 15 |
# File 'lib/nimbu-api/response/wrapper.rb', line 13 def env @env end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/nimbu-api/response/wrapper.rb', line 11 def response @response end |
Instance Method Details
#==(other) ⇒ Object
Compare the wrapper with other wrapper for equality
143 144 145 146 |
# File 'lib/nimbu-api/response/wrapper.rb', line 143 def ==(other) self.env == other.env && self.body == other.body end |
#[](key) ⇒ Object
Lookup an attribute from the body if hash, otherwise behave like array index. Convert any key to string before calling.
71 72 73 74 75 76 77 |
# File 'lib/nimbu-api/response/wrapper.rb', line 71 def [](key) if self.body.is_a?(Array) self.body[key] else self.body.send(:"#{key}") end end |
#body ⇒ Object
Response raw body
36 37 38 |
# File 'lib/nimbu-api/response/wrapper.rb', line 36 def body @body ? @body : response.body end |
#body=(value) ⇒ Object
29 30 31 32 |
# File 'lib/nimbu-api/response/wrapper.rb', line 29 def body=(value) @body = value @env[:body] = value end |
#client_error? ⇒ Boolean
54 55 56 |
# File 'lib/nimbu-api/response/wrapper.rb', line 54 def client_error? status.to_i >= 400 && status.to_i < 500 end |
#each ⇒ Object
Iterate over each resource inside the body
103 104 105 106 107 |
# File 'lib/nimbu-api/response/wrapper.rb', line 103 def each body_parts = self.body.respond_to?(:each) ? self.body : [self.body] return body_parts.to_enum unless block_given? body_parts.each { |part| yield(part) } end |
#has_key?(key) ⇒ Boolean
Check if body has an attribute
111 112 113 |
# File 'lib/nimbu-api/response/wrapper.rb', line 111 def has_key?(key) self.body.is_a?(Hash) && self.body.has_key?(key) end |
#headers ⇒ Object
Return response headers
64 65 66 |
# File 'lib/nimbu-api/response/wrapper.rb', line 64 def headers Nimbu::Response::Header.new(env) end |
#inspect ⇒ Object
Print only response body
137 138 139 |
# File 'lib/nimbu-api/response/wrapper.rb', line 137 def inspect "#<#{self.class.name} @body=\"#{self.body}\">" end |
#last ⇒ Object
79 80 81 |
# File 'lib/nimbu-api/response/wrapper.rb', line 79 def last self[self.length-1] if self.length.to_i > 0 end |
#redirect? ⇒ Boolean
50 51 52 |
# File 'lib/nimbu-api/response/wrapper.rb', line 50 def redirect? status.to_i >= 300 && status.to_i < 400 end |
#respond_to?(method_name) ⇒ Boolean
Check if method is defined on the body
127 128 129 130 131 132 133 |
# File 'lib/nimbu-api/response/wrapper.rb', line 127 def respond_to?(method_name) if self.has_key?(method_name.to_s) true else super end end |
#server_error? ⇒ Boolean
58 59 60 |
# File 'lib/nimbu-api/response/wrapper.rb', line 58 def server_error? status.to_i >= 500 && status.to_i < 600 end |
#status ⇒ Object
Response status
42 43 44 |
# File 'lib/nimbu-api/response/wrapper.rb', line 42 def status response.status end |
#success? ⇒ Boolean
46 47 48 |
# File 'lib/nimbu-api/response/wrapper.rb', line 46 def success? response.success? end |
#to_ary ⇒ Object
Convert the ResponseWrapper into an Array
97 98 99 |
# File 'lib/nimbu-api/response/wrapper.rb', line 97 def to_ary body.to_ary end |
#to_hash ⇒ Object
Convert the ResponseWrapper into a Hash
91 92 93 |
# File 'lib/nimbu-api/response/wrapper.rb', line 91 def to_hash body.to_hash end |
#to_s ⇒ Object
Return response body as string
85 86 87 |
# File 'lib/nimbu-api/response/wrapper.rb', line 85 def to_s body.to_s end |
#url ⇒ Object
Request url
25 26 27 |
# File 'lib/nimbu-api/response/wrapper.rb', line 25 def url response.env[:url].to_s end |