Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasallan committed Feb 6, 2014
1 parent 971d1a1 commit 53fab4d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
81 changes: 38 additions & 43 deletions lib/google_movies/http_capture.rb
@@ -1,78 +1,73 @@
# encoding: utf-8
$:.unshift File.expand_path('../models', __FILE__)
require 'nokogiri'
require "net/http"
require "uri"
require 'net/http'
require 'uri'
require 'movie'
require 'movie_theater'

module HttpCapture

def movies_theaters(page_url)
@http_address = URI.parse(URI.encode(page_url.strip))
@doc = page_doc(@http_address)
pages = @doc.css('div.n a[@href]')
http_address = URI.parse(URI.encode(page_url.strip))
doc = page_doc(http_address)
pages = doc.css('div.n a[@href]')

unless pages.empty?
pages.each do |p|
http_address = URI.parse(URI.encode("http://google.com#{p.values.first.strip}"))
doc = page_doc(http_address)
get_information(doc)
end
else
get_information(@doc)
get_information(doc)
end
@movies_theater
end

def get_information(doc)
@doc = doc
@movies_theater ||= []

@doc.search('div[@class="theater"]').each do |theater|
movies = []
movies = get_movies(theater)
@movies_theater << create_movie_theater_with(theater, movies)
doc.search('div[@class="theater"]').inject([]) do |array, theater|
array << create_movie_theater_with(theater, get_movies(theater))
array
end
@movies_theater
end

private

def page_doc(uri)
Nokogiri::HTML(Net::HTTP.get_response(uri).body)
end
def page_doc(uri)
Nokogiri::HTML(Net::HTTP.get_response(uri).body)
end

def create_movie_theater_with(theater, movies)
id = theater.search('div[@class="desc"]').first.attr("id").gsub("theater_", "")
name = theater.search('h2[@class="name"]').first.content
info = theater.search('div[@class="info"]').first.content
GoogleMovies::MovieTheater.new(name, info, id, movies)
end
def create_movie_theater_with(theater, movies)
id = theater.search('div[@class="desc"]').first.attr("id").gsub("theater_", "")
name = theater.search('h2[@class="name"]').first.content
info = theater.search('div[@class="info"]').first.content

def create_movie_with(movie)
name = movie.search('div[@class="name"]').first.search('a').first.content
url = movie.search('div[@class="name"]').first.search('a').first.attr("href")
id = url.split("&mid=")[1]
info = movie.search('span[@class="info"]').first.content
times = get_movies_times(movie)
GoogleMovies::Movie.new(name, id, info, times, url)
end
GoogleMovies::MovieTheater.new(name, info, id, movies)
end

def get_movies_times(movie)
times = ""
movie.css(".times").each do |time|
def create_movie_with(movie)
name = movie.search('div[@class="name"]').first.search('a').first.content
url = movie.search('div[@class="name"]').first.search('a').first.attr("href")
id = url.split("&mid=")[1]
info = movie.search('span[@class="info"]').first.content
times = get_movies_times(movie)

GoogleMovies::Movie.new(name, id, info, times, url)
end

def get_movies_times(movie)
movie.css(".times").inject("") do |times, time|
times = time.children.text.split('&nbsp')
times
end
times
end
end

def get_movies(theater)
movies = []
theater.search('div[@class="movie"]').each do |movie_document|
movies << create_movie_with(movie_document)
def get_movies(theater)
theater.search('div[@class="movie"]').inject([]) do |arr, value|
arr << create_movie_with(value)
arr
end
end
movies
end

class Client
include HttpCapture
Expand Down
9 changes: 5 additions & 4 deletions spec/google_movies_spec.rb
Expand Up @@ -2,11 +2,12 @@

describe GoogleMovies do

it "should create a client with the city and return movies theaters count" do
subject { GoogleMovies::Client.new('Joao Pessoa') }

it 'returns movies theaters' do
VCR.use_cassette('google_movies') do
@google_movies = GoogleMovies::Client.new("Joao Pessoa")
@google_movies.movies_theaters.size.should > 0
subject.movies_theaters.size.should > 0
end
end

end
end
39 changes: 19 additions & 20 deletions spec/http_capture_spec.rb
Expand Up @@ -2,42 +2,41 @@
require 'spec_helper'

describe HttpCapture do

class ClientTest
include HttpCapture
end

before(:each) do
@page ||= Nokogiri::HTML(File.read("spec/fixtures/movies.html"))
mock_uri = URI.parse(URI.encode("http://www.google.com/movies?near=Joao%20Pessoa".strip))
ClientTest.any_instance.should_receive(:page_doc).with(mock_uri).and_return(@page)
def page_doc(uri)
Nokogiri::HTML(File.read('spec/fixtures/movies.html'))
end
end

subject { @client = ClientTest.new }
let(:city_url) { 'http://www.google.com/movies?near=Joao%20Pessoa' }
subject { ClientTest.new }

it "should return one movie theater" do
subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa").size.should == 1
it 'returns one movie theater' do
subject.movies_theaters(city_url).size.should == 1
end

it "should return the movies" do
subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa")[0].movies.size.should == 6
it 'returns the movies' do
subject.movies_theaters(city_url)[0].movies.size.should == 6
end

it "should return movie theater with id" do
subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa")[0].id.should == "9897515771859860123"
it 'returns a movie theater with id' do
subject.movies_theaters(city_url)[0].id.should == '9897515771859860123'
end

it "should return movie with id" do
subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa")[0].movies[0].id.should == "9ff9a2dc6776dc6e"
it 'returns a movie with id' do
subject.movies_theaters(city_url)[0].movies[0].id.should == '9ff9a2dc6776dc6e'
end

it "should return movie information" do
movie_information = "‎Rated 12 anos‎‎ - Subtitled in Portuguese‎"
movie = subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa")[0].movies[0]
it 'returns the movie information' do
movie_information = '‎Rated 12 anos‎‎ - Subtitled in Portuguese‎'

movie = subject.movies_theaters(city_url)[0].movies[0]
movie.information.should eql(movie_information)
end

it "should return movie with times" do
subject.movies_theaters("http://www.google.com/movies?near=Joao%20Pessoa")[0].movies[0].times.should be_an_instance_of(Array)
it 'returns movie with times' do
subject.movies_theaters(city_url)[0].movies[0].times.should be_an_instance_of(Array)
end
end

0 comments on commit 53fab4d

Please sign in to comment.