-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
panda+tiger level tasks #11
Open
ellesuzuki
wants to merge
3
commits into
RubyoffRails:master
Choose a base branch
from
ellesuzuki:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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?) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. active_support adds a cool method here:
To use it, add this to the top of your file:
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
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") | ||
movie.title.should eq("the-title") | ||
movie.year.should eq(1998) | ||
movie.score.should eq(50) | ||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 awesome job extracting this out. Love it.