boxoffice 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2f3d80719f810ee37148c88a06e975b670ca429
4
+ data.tar.gz: 174508e4e785de617926d6f1c32af8e729699555
5
+ SHA512:
6
+ metadata.gz: a86a7c8b422b69d7894a8c0cd253541d3418545b9cd417fa6c9fa76f7cd19791b80dc955618530166523bdb7a2d63f70ae72cea1ffd8aafa9835fb7a1ca5fb59
7
+ data.tar.gz: 34619b4f0afbabbde943585c4aa9c24c479e2d793fe6e428a2e8e3eab82b101a0409928b3ddf125b5026e89e0601d603ccfc9655e95f38f56fdf2243899636ce
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/boxoffice.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'boxoffice/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'boxoffice'
7
+ spec.version = BoxOffice::VERSION
8
+ spec.authors = ['Samuel Garneau']
9
+ spec.email = ['sam@garno.me']
10
+ spec.description = 'BoxOffice gives you the box office grossing through the years.'
11
+ spec.summary = 'BoxOffice gives you the box office grossing through the years.'
12
+ spec.homepage = 'https://github.com/garno/boxoffice'
13
+ spec.license = 'BSD 3-Clause'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.3'
19
+ spec.add_development_dependency 'rake', '~> 10.3'
20
+ spec.add_development_dependency 'rspec', '~> 3.1'
21
+
22
+ spec.add_dependency 'monetize', '1.1.0'
23
+ spec.add_dependency 'mechanize', '2.7.3'
24
+ spec.add_dependency 'activemodel', '4.2.0'
25
+ end
data/lib/boxoffice.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'monetize'
2
+ require 'mechanize'
3
+ require 'active_model'
4
+
5
+ # Core
6
+ require 'boxoffice/base'
7
+ require 'boxoffice/daily'
8
+ require 'boxoffice/weekend'
9
+
10
+ # Models
11
+ require 'boxoffice/models/movie'
12
+
13
+ # Fetchers
14
+ require 'boxoffice/fetchers/page_fetcher'
15
+
16
+ # Parsers
17
+ require 'boxoffice/parsers/movie_parser'
18
+ require 'boxoffice/parsers/page_parser'
19
+
20
+ module BoxOffice
21
+ end
@@ -0,0 +1,25 @@
1
+ module BoxOffice
2
+ class Base
3
+ def initialize(date:)
4
+ @date = date
5
+ end
6
+
7
+ def movies
8
+ PageParser.new(content: content).movies
9
+ end
10
+
11
+ protected
12
+
13
+ def content
14
+ @content ||= PageFetcher.new(path: path).fetch!
15
+ end
16
+
17
+ def formatted_date
18
+ @date.strftime('%Y/%m/%d')
19
+ end
20
+
21
+ def path
22
+ # Overrided by the child class
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module BoxOffice
2
+ class Daily
3
+ def initialize(date:)
4
+ @date = date
5
+ end
6
+
7
+ def movies
8
+ PageParser.new(content: content).movies
9
+ end
10
+
11
+ protected
12
+
13
+ def content
14
+ @content ||= PageFetcher.new(path: path).fetch!
15
+ end
16
+
17
+ def path
18
+ "/daily/#{formatted_date}"
19
+ end
20
+
21
+ def formatted_date
22
+ @date.strftime('%Y/%m/%d')
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ class PageFetcher
2
+ def initialize(path:)
3
+ @path = path
4
+ end
5
+
6
+ def fetch!
7
+ Mechanize.new.get(page_url)
8
+ end
9
+
10
+ protected
11
+
12
+ def page_url
13
+ "http://www.the-numbers.com/box-office-chart#{@path}"
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ class Movie
2
+ include ActiveModel::Model
3
+
4
+ attr_accessor :title, :distributor, :genre, :gross, :total_gross,
5
+ :theater_count, :days
6
+ end
@@ -0,0 +1,51 @@
1
+ class MovieParser
2
+ def initialize(content:)
3
+ @content = content
4
+ end
5
+
6
+ def movie
7
+ Movie.new(
8
+ title: title,
9
+ distributor: distributor,
10
+ genre: genre,
11
+ gross: gross,
12
+ total_gross: total_gross,
13
+ theater_count: theater_count,
14
+ days: days
15
+ )
16
+ end
17
+
18
+ protected
19
+
20
+ def columns
21
+ @columns ||= @content.search('td')
22
+ end
23
+
24
+ def title
25
+ columns[2].search('a').first.content
26
+ end
27
+
28
+ def distributor
29
+ columns[3].search('a').first.content
30
+ end
31
+
32
+ def genre
33
+ columns[4].content
34
+ end
35
+
36
+ def gross
37
+ Monetize.parse(columns[5].content)
38
+ end
39
+
40
+ def total_gross
41
+ Monetize.parse(columns[9].content)
42
+ end
43
+
44
+ def theater_count
45
+ columns[7].content.to_i
46
+ end
47
+
48
+ def days
49
+ columns[10].content.to_i
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ class PageParser
2
+ def initialize(content:)
3
+ @content = content
4
+ end
5
+
6
+ def movies
7
+ rows.map { |row| MovieParser.new(content: row).movie }
8
+ end
9
+
10
+ protected
11
+
12
+ def chart
13
+ @content.search('#page_filling_chart')[1].search('table')
14
+ end
15
+
16
+ def rows
17
+ rows = chart.search('tr')
18
+
19
+ rows.drop(1) # Remove the table header
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module BoxOffice
2
+ VERSION = '0.1.2'
3
+ end
@@ -0,0 +1,19 @@
1
+ module BoxOffice
2
+ class Weekend < Base
3
+ def path
4
+ "/weekend/#{formatted_date}"
5
+ end
6
+
7
+ protected
8
+
9
+ def formatted_date
10
+ end_of_week.strftime('%Y/%m/%d')
11
+ end
12
+
13
+ def end_of_week
14
+ days_to_monday = @date.wday != 0 ? @date.wday - 1 : 6
15
+
16
+ @date - days_to_monday + 4
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxOffice::Base do
4
+ let(:base) { BoxOffice::Base.new(date: date) }
5
+ let(:date) { Date.today }
6
+
7
+ describe :movies do
8
+ let(:content) { '' }
9
+ let(:parser) { double(PageParser) }
10
+
11
+ before do
12
+ expect(base).to receive(:content).and_return(content)
13
+
14
+ expect(PageParser).to receive(:new).with(content: content).and_return(parser)
15
+ expect(parser).to receive(:movies)
16
+ end
17
+
18
+ specify { base.movies }
19
+ end
20
+
21
+ describe :content do
22
+ let(:path) { '/booya' }
23
+ let(:fetcher) { double(PageFetcher) }
24
+
25
+ before do
26
+ expect(base).to receive(:path).and_return(path)
27
+
28
+ expect(PageFetcher).to receive(:new).with(path: path).and_return(fetcher)
29
+ expect(fetcher).to receive(:fetch!)
30
+ end
31
+
32
+ specify { base.send(:content) }
33
+ end
34
+
35
+ describe :formatted_date do
36
+ let(:expected_format) { date.strftime('%Y/%m/%d') }
37
+
38
+ it { expect(base.send(:formatted_date)).to eq(expected_format) }
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxOffice::Daily do
4
+ let(:base) { BoxOffice::Daily.new(date: date) }
5
+ let(:date) { Date.today }
6
+
7
+ describe :path do
8
+ let(:expected_date) { date.strftime('%Y/%m/%d') }
9
+ let(:expected_path) { "/daily/#{expected_date}" }
10
+
11
+ it { expect(base.send(:path)).to eq(expected_path) }
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe BoxOffice::Weekend do
4
+ let(:base) { BoxOffice::Weekend.new(date: date) }
5
+ let(:date) { Date.parse('2014/12/09') }
6
+
7
+ describe :path do
8
+ let(:expected_date) { date.strftime('%Y/%m/%d') }
9
+ let(:expected_path) { "/weekend/#{expected_date}" }
10
+
11
+ before { expect(base).to receive(:formatted_date).and_return(expected_date) }
12
+
13
+ it { expect(base.send(:path)).to eq(expected_path) }
14
+ end
15
+
16
+ describe :end_of_week do
17
+ context 'with a week day' do
18
+ let(:date) { Date.parse('2014/12/09') }
19
+
20
+ it { expect(base.send(:end_of_week)).to eq(Date.parse('2014/12/12')) }
21
+ end
22
+
23
+ context 'with a weekend day' do
24
+ let(:date) { Date.parse('2014/12/13') }
25
+
26
+ it { expect(base.send(:end_of_week)).to eq(Date.parse('2014/12/12')) }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe PageFetcher do
4
+ let(:fetcher) { PageFetcher.new(path: path) }
5
+ let(:path) { '/booya' }
6
+
7
+ describe :fetch! do
8
+ let(:url) { 'http://booya.com' }
9
+ let(:mechanize) { double(Mechanize) }
10
+
11
+ before do
12
+ expect(fetcher).to receive(:page_url).and_return(url)
13
+
14
+ expect(Mechanize).to receive(:new).and_return(mechanize)
15
+ expect(mechanize).to receive(:get).with(url)
16
+ end
17
+
18
+ specify { fetcher.fetch! }
19
+ end
20
+
21
+ describe :page_url do
22
+ let(:expected_url) { "http://www.the-numbers.com/box-office-chart#{path}" }
23
+
24
+ it { expect(fetcher.send(:page_url)).to eq(expected_url) }
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
+
3
+ require 'boxoffice'
4
+
5
+ Dir[File.join(File.expand_path(File.dirname(__FILE__)), 'support/**/*.rb')].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.include MechanizeMacros
9
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boxoffice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Garneau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: monetize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mechanize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.7.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: activemodel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 4.2.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 4.2.0
97
+ description: BoxOffice gives you the box office grossing through the years.
98
+ email:
99
+ - sam@garno.me
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - boxoffice.gemspec
108
+ - lib/boxoffice.rb
109
+ - lib/boxoffice/base.rb
110
+ - lib/boxoffice/daily.rb
111
+ - lib/boxoffice/fetchers/page_fetcher.rb
112
+ - lib/boxoffice/models/movie.rb
113
+ - lib/boxoffice/parsers/movie_parser.rb
114
+ - lib/boxoffice/parsers/page_parser.rb
115
+ - lib/boxoffice/version.rb
116
+ - lib/boxoffice/weekend.rb
117
+ - spec/base/base_spec.rb
118
+ - spec/base/daily_spec.rb
119
+ - spec/base/weekend_spec.rb
120
+ - spec/fetchers/page_fetcher_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: https://github.com/garno/boxoffice
123
+ licenses:
124
+ - BSD 3-Clause
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: BoxOffice gives you the box office grossing through the years.
146
+ test_files: []