Class: Whatser::Response
- Inherits:
-
Object
- Object
- Whatser::Response
- Includes:
- Enumerable
- Defined in:
- lib/whatser/api/response.rb
Constant Summary collapse
- API_RESPONSE_KEYS =
['data','http_status','version','scope','page','per_page','more','error','error_description','error_uri']
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#data ⇒ Object
Returns the value of attribute data.
-
#error ⇒ Object
Returns the value of attribute error.
-
#error_description ⇒ Object
Returns the value of attribute error_description.
-
#error_uri ⇒ Object
Returns the value of attribute error_uri.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#http_status ⇒ Object
Returns the value of attribute http_status.
-
#more ⇒ Object
Returns the value of attribute more.
-
#page ⇒ Object
Returns the value of attribute page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #confict? ⇒ Boolean
- #data_enum ⇒ Object
- #each(&block) ⇒ Object
- #ensure_status_code(code) ⇒ Object
- #forbidden? ⇒ Boolean
-
#initialize(json_or_hash, opts = {}) ⇒ Response
constructor
A new instance of Response.
- #load_from_hash(hash, keys = nil) ⇒ Object
- #not_acceptable? ⇒ Boolean
- #not_allowed? ⇒ Boolean
- #not_found? ⇒ Boolean
- #server_error? ⇒ Boolean
- #succeeded? ⇒ Boolean
- #unauthorized? ⇒ Boolean
- #unprocessable_entity? ⇒ Boolean
Constructor Details
#initialize(json_or_hash, opts = {}) ⇒ Response
Returns a new instance of Response.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/whatser/api/response.rb', line 15 def initialize(json_or_hash, opts={}) return if json_or_hash.blank? begin hash = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash rescue JSON::ParserError hash = {} end load_from_hash(hash, opts[:keys]) ensure_status_code( opts[:code] ) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
11 12 13 |
# File 'lib/whatser/api/response.rb', line 11 def access_token @access_token end |
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/whatser/api/response.rb', line 7 def data @data end |
#error ⇒ Object
Returns the value of attribute error.
9 10 11 |
# File 'lib/whatser/api/response.rb', line 9 def error @error end |
#error_description ⇒ Object
Returns the value of attribute error_description.
9 10 11 |
# File 'lib/whatser/api/response.rb', line 9 def error_description @error_description end |
#error_uri ⇒ Object
Returns the value of attribute error_uri.
9 10 11 |
# File 'lib/whatser/api/response.rb', line 9 def error_uri @error_uri end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
11 12 13 |
# File 'lib/whatser/api/response.rb', line 11 def expires_in @expires_in end |
#http_status ⇒ Object
Returns the value of attribute http_status.
7 8 9 |
# File 'lib/whatser/api/response.rb', line 7 def http_status @http_status end |
#more ⇒ Object
Returns the value of attribute more.
8 9 10 |
# File 'lib/whatser/api/response.rb', line 8 def more @more end |
#page ⇒ Object
Returns the value of attribute page.
8 9 10 |
# File 'lib/whatser/api/response.rb', line 8 def page @page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
8 9 10 |
# File 'lib/whatser/api/response.rb', line 8 def per_page @per_page end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
11 12 13 |
# File 'lib/whatser/api/response.rb', line 11 def refresh_token @refresh_token end |
#scope ⇒ Object
Returns the value of attribute scope.
7 8 9 |
# File 'lib/whatser/api/response.rb', line 7 def scope @scope end |
#version ⇒ Object
Returns the value of attribute version.
7 8 9 |
# File 'lib/whatser/api/response.rb', line 7 def version @version end |
Instance Method Details
#confict? ⇒ Boolean
72 73 74 |
# File 'lib/whatser/api/response.rb', line 72 def confict? http_status == 409 end |
#data_enum ⇒ Object
40 41 42 |
# File 'lib/whatser/api/response.rb', line 40 def data_enum data.blank? ? [] : (data.is_a?(Array) ? data : [data]) end |
#each(&block) ⇒ Object
44 45 46 |
# File 'lib/whatser/api/response.rb', line 44 def each(&block) data_enum.each(&block) end |
#ensure_status_code(code) ⇒ Object
28 29 30 |
# File 'lib/whatser/api/response.rb', line 28 def ensure_status_code(code) self.http_status ||= code end |
#forbidden? ⇒ Boolean
56 57 58 |
# File 'lib/whatser/api/response.rb', line 56 def forbidden? http_status == 403 end |
#load_from_hash(hash, keys = nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/whatser/api/response.rb', line 32 def load_from_hash(hash,keys=nil) return unless hash.is_a?(Hash) keys ||= API_RESPONSE_KEYS keys.each do |a| send("#{a}=", hash[a]) if hash.has_key?(a) end end |
#not_acceptable? ⇒ Boolean
68 69 70 |
# File 'lib/whatser/api/response.rb', line 68 def not_acceptable? http_status == 406 end |
#not_allowed? ⇒ Boolean
64 65 66 |
# File 'lib/whatser/api/response.rb', line 64 def not_allowed? http_status == 405 end |
#not_found? ⇒ Boolean
60 61 62 |
# File 'lib/whatser/api/response.rb', line 60 def not_found? http_status == 404 end |
#server_error? ⇒ Boolean
80 81 82 |
# File 'lib/whatser/api/response.rb', line 80 def server_error? http_status == 500 end |
#succeeded? ⇒ Boolean
48 49 50 |
# File 'lib/whatser/api/response.rb', line 48 def succeeded? http_status == 200 end |
#unauthorized? ⇒ Boolean
52 53 54 |
# File 'lib/whatser/api/response.rb', line 52 def http_status == 401 end |
#unprocessable_entity? ⇒ Boolean
76 77 78 |
# File 'lib/whatser/api/response.rb', line 76 def unprocessable_entity? http_status == 422 end |