Class: RottenTomatoes::Rotten
- Inherits:
-
Object
- Object
- RottenTomatoes::Rotten
- Defined in:
- lib/rottentomatoes/rottentomatoes.rb
Constant Summary collapse
- @@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
.api_call(method, options) ⇒ Object
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, ) 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") ? : base_api_url + method if (method == "movies" && ![:id].nil?) url += "/" + [:id].to_s end if (method == "lists") url += ([:type] == "new_releases") ? "/dvds/" : "/movies/" url += [: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?) 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_key ⇒ Object
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_url ⇒ Object
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, ) 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 |