Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
require_relative "./movie"
class Api

APIKEY="4t6456xa33z8qhcqyuqgnkjh"
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"]
)
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end
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)
if struct.title != nil
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"] )
else

end
end
def self.get_url_as_json(url)
JSON.parse(open(url).read)
end

end

16 changes: 10 additions & 6 deletions lib/movie.rb
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

64 changes: 51 additions & 13 deletions movie_json.rb
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)
Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ruby, you'd generally not write if movie != nil

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
Copy link
Member

Choose a reason for hiding this comment

The 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








46 changes: 29 additions & 17 deletions spec/api_spec.rb
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

2 changes: 2 additions & 0 deletions spec/fixtures/forrest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@








Expand Down
20 changes: 9 additions & 11 deletions spec/movie_spec.rb
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