Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
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
27 changes: 19 additions & 8 deletions lib/api.rb
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?)
Copy link
Member

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.

end

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

end
2 changes: 1 addition & 1 deletion lib/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Movie

attr_reader :id, :title, :year, :score

def initialize(hash={})
@id = hash.fetch(:id)
@title = hash.fetch(:title)
Expand Down
45 changes: 40 additions & 5 deletions movie_json.rb
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?)
Copy link
Member

Choose a reason for hiding this comment

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

active_support adds a cool method here:

if movies.blank?

To use it, add this to the top of your file:

require 'activesupport/all'

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
80 changes: 64 additions & 16 deletions spec/api_spec.rb
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
3 changes: 3 additions & 0 deletions spec/fixtures/no_val.json
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}"
}
10 changes: 10 additions & 0 deletions spec/fixtures/not_found.json
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"
}
}
3 changes: 1 addition & 2 deletions spec/movie_spec.rb
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