-
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 + Eagle #7
Open
Meshed
wants to merge
10
commits into
RubyoffRails:master
Choose a base branch
from
Meshed: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 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
155a905
Prevent code from blowing up for search results from unfound movies
Meshed 7bd8276
Handle blank titles and invalid movies
Meshed 81a2fc5
Add movie search history tracking
Meshed d2f4b14
Add average movie score
Meshed 0044fd2
Add average year for scores
Meshed 30f5ef9
Determine ratings trend and provide feedback
Meshed d84240c
Add missing search_history class and spec files
Meshed a56c738
Clean up search history code
Meshed 5bba2c9
Prevent adding an invalid movie to the search history
Meshed 1f6416a
Use nil movie objects
Meshed 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 |
---|---|---|
|
@@ -3,22 +3,47 @@ | |
require "ostruct" | ||
require_relative "./movie" | ||
class Api | ||
@@search_history = [] | ||
|
||
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"] | ||
) | ||
if title != "" then | ||
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 think we could remove some duplication here... if you want to not-search if the user did blank, then maybe you could
|
||
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) | ||
if struct.ratings.nil? == false then | ||
movie = Movie.new(id: struct.id.to_i, | ||
title: struct.title, | ||
year: struct.year, | ||
score: struct.ratings["critics_score"] | ||
) | ||
@@search_history << movie | ||
return movie | ||
else | ||
Movie.new(id: 0, | ||
title: "NOTHINGFOUNDHERE", | ||
year: 0, | ||
score: 0 | ||
) | ||
end | ||
else | ||
Movie.new(id: 0, | ||
title: "NOTHINGFOUNDHERE", | ||
year: 0, | ||
score: 0 | ||
) | ||
end | ||
end | ||
|
||
|
||
def self.get_url_as_json(url) | ||
JSON.parse(open(url).read) | ||
end | ||
|
||
def self.get_search_history() | ||
@@search_history | ||
end | ||
|
||
def self.reset_search_history() | ||
@@search_history = [] | ||
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,68 @@ | ||
require_relative "./movie" | ||
|
||
class Search_History | ||
attr_reader :movies | ||
|
||
def initialize() | ||
@movies = [] | ||
end | ||
|
||
def add_search(movie) | ||
@movies << movie | ||
end | ||
|
||
def average_rating() | ||
average = 0 | ||
@movies.each do |movie| | ||
average += movie.score | ||
end | ||
|
||
return (average/@movies.count) | ||
end | ||
|
||
def average_year_for_ratings() | ||
year = 0 | ||
@movies.each do |movie| | ||
year += movie.year | ||
end | ||
|
||
return (year/@movies.count).ceil | ||
end | ||
|
||
def average_rating_for_year(year) | ||
@rating = 0 | ||
movie_count = 0 | ||
@movies.each do |movie| | ||
if movie.year == year then | ||
@rating += movie.score | ||
movie_count += 1 | ||
end | ||
end | ||
|
||
return (@rating/movie_count) | ||
end | ||
|
||
def average_rating_for_max_year | ||
average_rating_for_year(max_year) | ||
end | ||
|
||
def average_rating_for_min_year | ||
average_rating_for_year(min_year) | ||
end | ||
|
||
def max_year | ||
@movies.max_by(&:year).year | ||
end | ||
|
||
def min_year | ||
@movies.min_by(&:year).year | ||
end | ||
|
||
def ratings_trend | ||
if @movies.count > 1 then | ||
(average_rating_for_max_year - average_rating_for_min_year) / (max_year - min_year) | ||
else | ||
0 | ||
end | ||
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
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,68 @@ | ||
require_relative "../lib/search_history" | ||
describe Search_History do | ||
it "should store a search history" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.movies.count.should eq(1) | ||
end | ||
|
||
it "should store latest seach history" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.movies.last.title.should eq("Forrest Gump") | ||
end | ||
|
||
it "should get average rating for search history" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.movies.count.should eq(2) | ||
search_history.average_rating.should eq(41) | ||
end | ||
|
||
it "should have the average year for the ratings" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.average_year_for_ratings.should eq(1997) | ||
end | ||
|
||
it "should have the average rating for the max year" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.average_rating_for_max_year.should eq(11) | ||
end | ||
|
||
it "should have the average rating for the min year" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.add_search(Api.search_by_title("Ace Ventura")) | ||
search_history.average_rating_for_min_year.should eq(58) | ||
end | ||
|
||
it "should have the max year" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.add_search(Api.search_by_title("Ace Ventura")) | ||
search_history.max_year.should eq(2001) | ||
end | ||
|
||
it "should have the min year" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.add_search(Api.search_by_title("Ace Ventura")) | ||
search_history.min_year.should eq(1994) | ||
end | ||
|
||
it "should have the ratings trend" do | ||
search_history = Search_History.new | ||
search_history.add_search(Api.search_by_title("Forrest Gump")) | ||
search_history.add_search(Api.search_by_title("Joe Dirt")) | ||
search_history.add_search(Api.search_by_title("Ace Ventura")) | ||
search_history.ratings_trend.should eq(-7) | ||
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.
I don't like class variables in most cases... This will prevent you from going multi-threaded in the future (as each thread will overwrite this variable)
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.
Thanks, that is a left over artifact from before I implemented the Search_History class.