Module: Rotten::Api::ClassMethods

Defined in:
lib/rotten/api.rb

Instance Method Summary collapse

Instance Method Details

#endpointObject



18
19
20
# File 'lib/rotten/api.rb', line 18

def endpoint
  "http://api.rottentomatoes.com/api/public/v#{version}"
end

#get(path, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rotten/api.rb', line 26

def get path, options={}
  if Rotten.api_key.nil?
    raise UndefinedApiKeyError, "Please define your API key with Rotten.api_key=(your_key)"
  end

  url = url_for(path, options)
  open( url ) do |response|
    data = JSON.parse(response.read)
    if block_given?
      yield data
    else
      data
    end
  end
end

#url_for(path, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rotten/api.rb', line 42

def url_for(path, options={})
  path.gsub! /\.json\Z/, ''
  
  params = ''
  if options.keys.any?
    options.each_pair{|k,v| params << "#{k}=#{URI.escape(v)}&" }
  end
  params.chomp! "&" if params

  "#{endpoint}/#{path}.json?apikey=#{Rotten.api_key}&#{params}"
end

#versionObject



22
23
24
# File 'lib/rotten/api.rb', line 22

def version
  '1.0'
end