Module: Beamly::Client

Included in:
Buzz, Epg
Defined in:
lib/beamly/client.rb

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/beamly/client.rb', line 4

def get(url)
  response = Curl.get(Beamly.base_uri + self.prefix_url + url) do |http|
    http.headers['zeebox-app-id'] = Beamly.configuration.id
    http.headers['zeebox-app-key'] = Beamly.configuration.key
    http.headers["Accept-Encoding"] = "gzip,deflate"
    http.follow_location = true
  end
  begin
    gz = Zlib::GzipReader.new(StringIO.new(response.body_str))
    json = gz.read
  rescue Zlib::GzipFile::Error
    json = response.body_str
  end
  begin
    result = JSON.parse json
  rescue JSON::ParserError
    if response.response_code == 404
      raise Beamly::NotFound
    else
      result = json
    end
  end
  if result.is_a? Array
    result.collect! { |x| x.is_a?(Hash) ? Hashie::Mash.new(x) : x }
  else
    result = Hashie::Mash.new(result)
  end
end