Class: Getclicky::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/getclicky/response.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

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_classObject

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

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/getclicky/response.rb', line 6

def format
  @format
end

#itemObject

Returns the value of attribute item.



6
7
8
# File 'lib/getclicky/response.rb', line 6

def item
  @item
end

Instance Method Details

#dataObject



19
20
21
# File 'lib/getclicky/response.rb', line 19

def data
  @format.nil? ? mashify_data : @item
end

#mashify_dataObject



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