Class: Zensana::Response
- Inherits:
-
Object
- Object
- Zensana::Response
- Includes:
- Enumerable
- Defined in:
- lib/zensana/services/response.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each(&block) ⇒ Object
- #has_more_pages? ⇒ Boolean
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
- #next_page ⇒ Object
- #ok? ⇒ Boolean
- #pretty ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
11 12 13 14 15 |
# File 'lib/zensana/services/response.rb', line 11 def initialize(http_response) @ok = http_response.success? body = JSON.parse(http_response.body) rescue {} @data = body['data'] || body end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/zensana/services/response.rb', line 29 def [](key) @data.is_a?(Hash) ? @data[key.to_s] : key.is_a?(Integer) ? @data[key] : nil end |
#each(&block) ⇒ Object
7 8 9 |
# File 'lib/zensana/services/response.rb', line 7 def each(&block) @data.each(&block) end |
#has_more_pages? ⇒ Boolean
21 22 23 |
# File 'lib/zensana/services/response.rb', line 21 def has_more_pages? !! next_page end |
#next_page ⇒ Object
25 26 27 |
# File 'lib/zensana/services/response.rb', line 25 def next_page @data['next_page'] end |
#ok? ⇒ Boolean
17 18 19 |
# File 'lib/zensana/services/response.rb', line 17 def ok? @ok end |
#pretty ⇒ Object
45 46 47 |
# File 'lib/zensana/services/response.rb', line 45 def pretty ap @data end |
#to_a ⇒ Object
37 38 39 |
# File 'lib/zensana/services/response.rb', line 37 def to_a @data.respond_to?('to_a') ? @data.to_a : @data end |
#to_h ⇒ Object
33 34 35 |
# File 'lib/zensana/services/response.rb', line 33 def to_h @data.respond_to?('to_h') ? @data.to_h : @data end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/zensana/services/response.rb', line 41 def to_s @data.respond_to?('to_s') ? @data.to_s : @data end |