-
Notifications
You must be signed in to change notification settings - Fork 25
I am done: P,T and E #21
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
|
|
||
| class Movie | ||
|
|
||
| attr_reader :id, :title, :year, :score | ||
| attr_reader :id, :title, :year, :score | ||
|
|
||
| def initialize(hash={}) | ||
| @id = hash.fetch(:id) | ||
| @title = hash.fetch(:title) | ||
| @year = hash.fetch(:year) | ||
| @score = hash.fetch(:score) | ||
| end | ||
| @id = hash.fetch(:id) | ||
| @title = hash.fetch(:title) | ||
| @year = hash.fetch(:year) | ||
| @score = hash.fetch(:score) | ||
| end | ||
|
|
||
| end | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,59 @@ | ||
| require_relative "lib/movie" | ||
| require_relative "lib/api" | ||
|
|
||
|
|
||
| def average(movies, keys) | ||
| data = movies.map(&keys) | ||
| average = data.inject { |sum, rating| sum + rating }.to_f/data.size | ||
| end | ||
|
|
||
| def calculate_slope(movies) | ||
| movies = movies.sort_by { |i| i.year } | ||
| slope = (movies.last.score - movies.first.score).to_f / | ||
| (movies.last.year - movies.first.year).to_f | ||
| if slope < 0 | ||
|
Member
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. This method is doing two things, it's calculating something (query), and it's doing something (command). Generally, it's considered better if your command methods return nothing (except self), and your querys have no side effects. So, you'd want wherever is calling "calculate_scope" to handle deciding madder/better. |
||
| puts slope | ||
| puts "You're getting madder" | ||
| elsif slope > 0 | ||
| puts slope | ||
| puts "You're getting better" | ||
| end | ||
| end | ||
|
|
||
| def find_movie | ||
| puts "OH HAI. Search?" | ||
| movie_title = gets | ||
| movie = Api.search_by_title(movie_title) | ||
| puts "Found: #{movie.title}. Score: #{movie.score}" | ||
| puts "Hello name me a movie" | ||
| title = gets | ||
| movie = Api.search_by_title(title) | ||
|
Member
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. The indentation is off here. should be like this (it helps telling the way the decision tree is going to go): title = gets
movie = Api.search_by_title(title)
if movie != nil
puts "Found: #{movie.title}. Score: #{movie.score}"
movie
else
puts "sorry try again"
end
Member
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. In ruby, you'd generally not write if movie was not nil and not false, it would be considered "truthy". so, you could write if movie
puts "..." |
||
| if movie != nil | ||
| puts "Found: #{movie.title}. Score: #{movie.score}" | ||
| movie | ||
| else | ||
| puts "sorry try again" | ||
| end | ||
| end | ||
|
|
||
| find_movie | ||
| movies = [] | ||
|
|
||
| while true | ||
| movies << find_movie | ||
|
Member
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. I'd probably do: while true
movie = find_movie
if movie
puts "Average_Score: #{average(movies, :score)}, Average_Years: #{average(movies, :year)}"
end
puts "Search Again (Y/N)"
#...
end |
||
| if movies.last == nil | ||
| movies.pop | ||
| else | ||
| puts "Average_Score: #{average(movies, :score)}, Average_Years: #{average(movies, :year)}" | ||
| end | ||
|
|
||
| while true do | ||
| puts "Search Again (Y/N)" | ||
| answer = gets.upcase[0] | ||
| if answer == "Y" | ||
| find_movie | ||
| else | ||
| break | ||
| end | ||
| puts "Search Again (Y/N)" | ||
| answer = gets.upcase[0] | ||
| if answer != "Y" | ||
| puts calculate_slope(movies) | ||
| break | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,39 @@ | ||
| require_relative "../lib/api" | ||
| require "ostruct" | ||
|
|
||
| describe Api do | ||
| describe Api do | ||
|
|
||
| let(:movie) { Api.search_by_title("Forrest Gump") } | ||
| 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 | ||
| before do | ||
| Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } | ||
| end | ||
|
|
||
| it "should search for movies" do | ||
| movie.title.should eq("Forrest Gump") | ||
| end | ||
| it "should search for movies" do | ||
| movie = Api.search_by_title("Forrest Gump") | ||
| movie.title.should eq("Forrest Gump") | ||
| end | ||
|
|
||
| it "should return the score" do | ||
| movie.score.should eq(71) | ||
| end | ||
| it "should return the score" do | ||
| movie = Api.search_by_title("Forrest Gump") | ||
| movie.score.should eq(71) | ||
| end | ||
|
|
||
| it "should return the id" do | ||
| movie.id.should eq(10036) | ||
| end | ||
| it "should return the id" do | ||
| movie = Api.search_by_title("Forrest Gump") | ||
| movie.id.should eq(10036) | ||
| end | ||
|
|
||
|
|
||
| it "should return the year" do | ||
| movie = Api.search_by_title("Forrest Gump") | ||
| movie.year.should eq(1994) | ||
| end | ||
|
|
||
| it "should return piggy when wrong movie" do | ||
| movie = Api.search_by_title("wofjewjfwpe") | ||
| movie.should eq(nil) | ||
| end | ||
|
|
||
| it "should return the year" do | ||
| movie.year.should eq(1994) | ||
| end | ||
| end | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| require_relative "../lib/movie" | ||
| 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 | ||
| describe Movie do | ||
| it "should return a movie when searched" 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 |
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.
Minor, but you named this keys (plural). I would keep to "key", since you can't actually pass in multiple entries here.