Class: RottenTomatoes::Rotten
- Inherits:
-
Object
- Object
- RottenTomatoes::Rotten
- Defined in:
- lib/rottentomatoes/rottentomatoes.rb
Constant Summary collapse
- REQUEST_SLEEP_SEC =
ENV['ROTTENTOMATOES_REQUEST_SLEEP_SEC'].to_f || 0
- TYPE_TO_SECTION =
{ :box_office => 'movies', :in_theaters => 'movies', :opening => 'movies', :top_rentals => 'dvds', :current_releases => 'dvds', :new_releases => 'dvds' }
- @@api_key =
""
- @@api_response =
{}
Class Method Summary collapse
- .api_call(method, options) ⇒ Object
- .api_key ⇒ Object
- .api_key=(key) ⇒ Object
- .base_api_url ⇒ Object
- .data_to_object(data) ⇒ Object
- .get_url(uri_str, limit = 10) ⇒ Object
- .process_results(results, options) ⇒ Object
Class Method Details
permalink .api_call(method, options) ⇒ Object
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, ) 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") ? : base_api_url + method if (method == "movies" && ![:id].nil?) url += "/" + [:id].to_s end if (method == "lists") type = [:type] section = [: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([:title].to_s) if (method == "movies" && ![:title].nil? && [:id].nil?) url += "&type=imdb&id=%07d" % [: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 |
permalink .api_key ⇒ Object
[View source]
25 26 27 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 25 def self.api_key @@api_key end |
permalink .api_key=(key) ⇒ Object
[View source]
29 30 31 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 29 def self.api_key=(key) @@api_key = key end |
permalink .base_api_url ⇒ Object
[View source]
33 34 35 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 33 def self.base_api_url "http://api.rottentomatoes.com/api/public/v1.0/" end |
permalink .data_to_object(data) ⇒ Object
[View source]
109 110 111 112 113 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 109 def self.data_to_object(data) object = DeepOpenStruct.load(data) object.raw_data = data return object end |
permalink .get_url(uri_str, limit = 10) ⇒ Object
[View source]
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 88 def self.get_url(uri_str, limit = 10) return false if limit == 0 uri = URI.parse(uri_str) begin response = Net::HTTP.start(uri.host, uri.port) do |http| http.get((uri.path.empty? ? '/' : uri.path) + (uri.query ? '?' + uri.query : '')) end sleep(REQUEST_SLEEP_SEC) if REQUEST_SLEEP_SEC != 0 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 |
permalink .process_results(results, options) ⇒ Object
[View source]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rottentomatoes/rottentomatoes.rb', line 70 def self.process_results(results, ) results.flatten! results.compact! unless ([:limit].nil?) raise ArgumentError, "Limit must be an integer greater than 0" if (![:limit].is_a?(Fixnum) || !([:limit] > 0)) results = results.slice(0, [:limit]) end results.map!{|m| RottenMovie.new(m, [:expand_results])} if (results.length == 1) return results[0] else return results end end |