Class: Getclicky::Response
- Inherits:
-
Object
- Object
- Getclicky::Response
- Defined in:
- lib/getclicky/response.rb
Class Attribute Summary collapse
-
.mash_class ⇒ Object
Returns the value of attribute mash_class.
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#item ⇒ Object
Returns the value of attribute item.
Instance Method Summary collapse
- #data ⇒ Object
-
#initialize(item, format = nil) ⇒ Response
constructor
A new instance of Response.
- #mashify_data ⇒ Object
- #parse(body) ⇒ Object
Constructor Details
#initialize(item, format = nil) ⇒ Response
Returns a new instance of Response.
14 15 16 17 |
# File 'lib/getclicky/response.rb', line 14 def initialize(item, format = nil) @item = item @format = format end |
Class Attribute Details
.mash_class ⇒ Object
Returns the value of attribute mash_class.
9 10 11 |
# File 'lib/getclicky/response.rb', line 9 def mash_class @mash_class end |
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/getclicky/response.rb', line 6 def format @format end |
#item ⇒ Object
Returns the value of attribute item.
6 7 8 |
# File 'lib/getclicky/response.rb', line 6 def item @item end |
Instance Method Details
#data ⇒ Object
19 20 21 |
# File 'lib/getclicky/response.rb', line 19 def data @format.nil? ? mashify_data : @item end |
#mashify_data ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/getclicky/response.rb', line 23 def mashify_data json = @item.is_a?(String) ? JSON.parse(@item) : @item if json.size.eql?(1) parse(json.first['dates']) elsif json.size > 1 {}.tap do |results| json.collect { |r| results[r['type'].gsub('-','_').intern] = parse(r['dates']) } end else @item end end |
#parse(body) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/getclicky/response.rb', line 36 def parse(body) case body when Hash self.class.mash_class.new(body) when Array body.map { |item| item.is_a?(Hash) ? self.class.mash_class.new(item) : item } else body end end |