Class: RottenTomatoes::Rotten

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

Constant Summary collapse

@@api_key =
""
@@api_response =
{}

Class Method Summary collapse

Class Method Details

.api_call(method, options) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rottentomatoes/rottentomatoes.rb', line 26

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', 'lists', or 'direct' as the method" if (method != "movies" && method != "direct" && method != "lists")

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

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

  if (method == "lists")
    url += (options[:type] == "new_releases") ? "/dvds/" : "/movies/"
    url += options[: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?)
  
  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

.api_keyObject



14
15
16
# File 'lib/rottentomatoes/rottentomatoes.rb', line 14

def self.api_key
  @@api_key
end

.api_key=(key) ⇒ Object



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

def self.api_key=(key)
  @@api_key = key
end

.base_api_urlObject



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

def self.base_api_url
  "http://api.rottentomatoes.com/api/public/v1.0/"
end

.data_to_object(data) ⇒ Object



92
93
94
95
96
# File 'lib/rottentomatoes/rottentomatoes.rb', line 92

def self.data_to_object(data)
  object = DeepOpenStruct.load(data)
  object.raw_data = data
  return object
end

.get_url(uri_str, limit = 10) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rottentomatoes/rottentomatoes.rb', line 75

def self.get_url(uri_str, limit = 10)
  return false if limit == 0
  begin 
    response = Net::HTTP.get_response(URI.parse(uri_str))
  rescue SocketError, Errno::ENETDOWN
    response = Net::HTTPBadRequest.new( '404', 404, "Not Found" )
    return response
  end 
  case response
    when Net::HTTPSuccess     then response
    when Net::HTTPRedirection then get_url(response['location'], limit - 1)
    when Net::HTTPForbidden   then get_url(uri_str, limit - 1)
  else
    Net::HTTPBadRequest.new( '404', 404, "Not Found" )
  end
end

.process_results(results, options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rottentomatoes/rottentomatoes.rb', line 57

def self.process_results(results, options)
  results.flatten!
  results.compact!

  unless (options[:limit].nil?)
    raise ArgumentError, "Limit must be an integer greater than 0" if (!options[:limit].is_a?(Fixnum) || !(options[:limit] > 0))
    results = results.slice(0, options[:limit])
  end

  results.map!{|m| RottenMovie.new(m, options[:expand_results])}

  if (results.length == 1)
    return results[0]
  else
    return results
  end   
end