Method: RottenTomatoes::Rotten.api_call

Defined in:
lib/rottentomatoes/rottentomatoes.rb

.api_call(method, options) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rottentomatoes/rottentomatoes.rb', line 37

def self.api_call(method, options)
  raise ArgumentError, "Rotten.api_key must be set before you can use the API" if(@@api_key.nil? || @@api_key.empty?)
  raise ArgumentError, "You must specify 'movies', 'movie_alias', 'lists' or 'direct' as the method" if (method != "movies" && method != "direct" && method != "lists" && method != "movie_alias")

  url = (method == "direct") ? options : base_api_url + method

  if (method == "movies" && !options[:id].nil?)
    url += "/" + options[:id].to_s
  end

  if (method == "lists")
    type = options[:type]
    section = options[:section] || TYPE_TO_SECTION[type] || 'movies'
    url += "/#{section}/#{type}"
  end

  url += ".json" if (url[-5, 5] != ".json")
  url += "?apikey=" + @@api_key
  url += "&q=" + CGI::escape(options[:title].to_s) if (method == "movies" && !options[:title].nil? && options[:id].nil?)
  url += "&type=imdb&id=%07d" % options[:imdb].to_i if (method == "movie_alias")

  response = get_url(url)
  return nil if(response.code.to_i != 200)
  body = JSON(response.body)

  if (body["total"] == 0 && body["title"].nil?)
    return nil
  else
    return body["movies"] if !body["movies"].nil?
    return body
  end
end