diff --git a/lib/api.rb b/lib/api.rb index a8d499c..cf0ccef 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,24 +1,35 @@ require "open-uri" require "json" require "ostruct" + require_relative "./movie" -class Api +class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(title)}&page_limit=1" - struct = OpenStruct.new(get_url_as_json(url).fetch("movies").first) - Movie.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) + + result = get_url_as_json(url) + + if found?(result) + struct = OpenStruct.new(result.fetch('movies').first) + + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + else + nil + end end + def self.found?(result) + result.has_key?('movies') && !(result.fetch('movies').empty?) + end def self.get_url_as_json(url) JSON.parse(open(url).read) end - end diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..6293b0a 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,6 +1,6 @@ class Movie - attr_reader :id, :title, :year, :score + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..01c1d68 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,21 +1,56 @@ require_relative "lib/movie" require_relative "lib/api" +# function definitions def find_movie - puts "OH HAI. Search?" + puts "OH HAI. Add a movie you really like:" movie_title = gets movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + + if !movie.nil? + puts "Found: #{movie.title}. Score: #{movie.score}" + else + puts 'Movie not found' + end + + movie +end + +def get_average(movies) + scores = [] + movies.each do |movie| + scores << movie.score if !movie.nil? + end + total = scores.inject { |sum,n| sum+n } + total.to_f/scores.size end -find_movie + + +# main +movies = [] + +movie = find_movie +movies << movie if !movie.nil? while true do - puts "Search Again (Y/N)" + puts "Add another? (Y/N)" answer = gets.upcase[0] if answer == "Y" - find_movie + movie = find_movie + movies << movie if !movie.nil? else + puts + # assuming movies that were actually found + puts 'Movies searched and found: ' + if (movies.nil? || movies.empty?) + puts 'None' + else + movies.each { |movie| puts movie.title } + puts + puts 'The average score of movies searched and found: ' + puts get_average(movies) + end break end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..5908681 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -1,27 +1,75 @@ require_relative "../lib/api" + require "ostruct" describe Api do + context '#movie not found' do + let(:movie) { Api.search_by_title('abcdkdjfakdj') } + + before do + Api.stub(:get_url_as_json) { + JSON.parse(File.read('spec/fixtures/not_found.json')) + } + end + + it 'should search for movie and return nothing' do + movie.should be_nil + end + + it 'should have found nothing' do + result = {'movies' => []} + Api.found?(result).should be_false + end + end + + + context '#movie not found when no search term' do + let(:movie) { Api.search_by_title('') } + + before do + Api.stub(:get_url_as_json) { + JSON.parse(File.read('spec/fixtures/no_val.json')) + } + end + + it 'should search for movie and return nothing' do + movie.should be_nil + end + + it 'should have found nothing' do + result = {} + Api.found?(result).should be_false + end + end + + + context '#movie found' do + let(:movie) { Api.search_by_title("Forrest Gump") } + + before do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + end - let(:movie) { Api.search_by_title("Forrest Gump") } + it "should search for movies" do + movie.title.should eq("Forrest Gump") + end - before do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } - end + it "should return the score" do + movie.score.should eq(71) + end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + it "should return the id" do + movie.id.should eq(10036) + end - it "should return the score" do - movie.score.should eq(71) - end + it "should return the year" do + movie.year.should eq(1994) + end - it "should return the id" do - movie.id.should eq(10036) - end + it 'should have found a movie' do + result = {'movies' => [{"id"=>"470554171", "title"=>"Ratatouille", "year"=>2007,"ratings"=>{"critics_rating"=>"Certified Fresh", "critics_score"=>96}}]} + Api.found?(result).should be_true + end + end - it "should return the year" do - movie.year.should eq(1994) - end end diff --git a/spec/fixtures/no_val.json b/spec/fixtures/no_val.json new file mode 100644 index 0000000..4e8aecd --- /dev/null +++ b/spec/fixtures/no_val.json @@ -0,0 +1,3 @@ +{ + "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}" +} diff --git a/spec/fixtures/not_found.json b/spec/fixtures/not_found.json new file mode 100644 index 0000000..4077be7 --- /dev/null +++ b/spec/fixtures/not_found.json @@ -0,0 +1,10 @@ +{ + "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}", + "total": 0, + "movies": [ + + ], + "links": { + "self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=ABDCadskfjask&page_limit=1&page=1" + } +} diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..ec485d7 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -1,6 +1,6 @@ require_relative "../lib/movie" -describe Movie do +describe Movie do it "should store the title, year, and score" do movie = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) movie.id.should eq("the-id") @@ -8,5 +8,4 @@ movie.year.should eq(1998) movie.score.should eq(50) end - end