Class: BadFruit::Base
- Inherits:
-
Object
- Object
- BadFruit::Base
- Defined in:
- lib/badfruit/base.rb
Constant Summary collapse
- API_VERSION =
"v1.0"- API_BASE_URL =
"http://api.rottentomatoes.com/api/public/#{API_VERSION}"- MOVIE_DETAIL_BASE_URL =
"#{API_BASE_URL}/movies"- LISTS_DETAIL_BASE_URL =
"#{API_BASE_URL}/lists"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #get(url) ⇒ Object
- #get_lists_action(action) ⇒ Object
- #get_movie_alias_info(alias_id, type) ⇒ Object
- #get_movie_info(movie_id, action) ⇒ Object
-
#initialize(key) ⇒ Base
constructor
A new instance of Base.
- #lists ⇒ Object
- #movies ⇒ Object
- #parse_actors_array(hash) ⇒ Object
- #parse_movie_array(hash) ⇒ Object
-
#parse_movies_array(hash) ⇒ Object
Utility Methods For Parsing.
- #search_movies(name, page_limit, page) ⇒ Object
- #similar_movies(movie_id) ⇒ Object
Constructor Details
#initialize(key) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 17 |
# File 'lib/badfruit/base.rb', line 12 def initialize(key) @api_key = key @@base_api_url = "http://api.rottentomatoes.com/api/public/#{API_VERSION}" @@movies_query_url = "#{@@base_api_url}/movies.json?apikey=#{@api_key}" @@lists_query_url = "#{@@base_api_url}/lists.json?apikey=#{@api_key}" end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
3 4 5 |
# File 'lib/badfruit/base.rb', line 3 def api_key @api_key end |
Instance Method Details
#get(url) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/badfruit/base.rb', line 74 def get(url) data = nil resp = HTTParty.get(url) if resp.code == 200 return resp.body end end |
#get_lists_action(action) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/badfruit/base.rb', line 52 def get_lists_action(action) url = nil case action when "new_releases" url = "#{LISTS_DETAIL_BASE_URL}/dvds/new_releases.json?apikey=#{@api_key}" when "opening" url = "#{LISTS_DETAIL_BASE_URL}/movies/opening.json?apikey=#{@api_key}" when "upcoming" url = "#{LISTS_DETAIL_BASE_URL}/movies/upcoming.json?apikey=#{@api_key}" when "in_theaters" url = "#{LISTS_DETAIL_BASE_URL}/movies/in_theaters.json?apikey=#{@api_key}" when "current_releases" url = "#{LISTS_DETAIL_BASE_URL}/dvds/current_releases.json?apikey=#{@api_key}" when "upcoming_dvds" url = "#{LISTS_DETAIL_BASE_URL}/dvds/upcoming.json?apikey=#{@api_key}" else puts "Not a valid action" return end return get(url) end |
#get_movie_alias_info(alias_id, type) ⇒ Object
47 48 49 50 |
# File 'lib/badfruit/base.rb', line 47 def get_movie_alias_info(alias_id, type) url = "#{API_BASE_URL}/movie_alias.json?id=#{CGI::escape(alias_id)}&type=#{CGI::escape(type)}&apikey=#{@api_key}" return get(url) end |
#get_movie_info(movie_id, action) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/badfruit/base.rb', line 29 def get_movie_info(movie_id, action) url = nil case action when "details" url = "#{MOVIE_DETAIL_BASE_URL}/#{movie_id}.json?apikey=#{@api_key}" when "reviews" url = "#{MOVIE_DETAIL_BASE_URL}/#{movie_id}/reviews.json?apikey=#{@api_key}" when "cast" url = "#{MOVIE_DETAIL_BASE_URL}/#{movie_id}/cast.json?apikey=#{@api_key}" when "main" url = "#{MOVIE_DETAIL_BASE_URL}/#{movie_id}.json?apikey=#{@api_key}" else puts "Not a valid action" return end return get(url) end |
#lists ⇒ Object
10 |
# File 'lib/badfruit/base.rb', line 10 def lists(); @list || BadFruit::Lists.new(self); end |
#movies ⇒ Object
9 |
# File 'lib/badfruit/base.rb', line 9 def movies(); @movie || BadFruit::Movies.new(self); end |
#parse_actors_array(hash) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/badfruit/base.rb', line 96 def parse_actors_array(hash) actorsArray = Array.new hash["cast"].each do |actor| actorsArray.push(Actor.new(actor)) end return actorsArray end |
#parse_movie_array(hash) ⇒ Object
92 93 94 |
# File 'lib/badfruit/base.rb', line 92 def parse_movie_array(hash) Movie.new(hash, self) end |
#parse_movies_array(hash) ⇒ Object
Utility Methods For Parsing
84 85 86 87 88 89 90 |
# File 'lib/badfruit/base.rb', line 84 def parse_movies_array(hash) moviesArray = Array.new hash["movies"].each do |movie| moviesArray.push(Movie.new(movie, self)) end return moviesArray end |
#search_movies(name, page_limit, page) ⇒ Object
19 20 21 22 |
# File 'lib/badfruit/base.rb', line 19 def search_movies(name, page_limit, page) url = "#{@@movies_query_url}&q=#{CGI::escape(name)}&page_limit=#{page_limit}&page=#{page}" return get(url) end |
#similar_movies(movie_id) ⇒ Object
24 25 26 27 |
# File 'lib/badfruit/base.rb', line 24 def similar_movies(movie_id) url = "#{MOVIE_DETAIL_BASE_URL}/#{CGI::escape(movie_id)}/similar.json?apikey=#{@api_key}" return get(url) end |