bfi_player_search 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb3485213ce0966d67e48e710b21139558dc9354
4
+ data.tar.gz: 932e718ec4dccb5e6a8f3d571d8d72f727002370
5
+ SHA512:
6
+ metadata.gz: 43b6362b34288678f810c0bae5be57427531788d4e046fc80dc7cbc57e150e1c839af345739552ecd07bca42f9cf796539949e3a7712f49237617d629e20d64d
7
+ data.tar.gz: cc5089280d6dc96eca3bfe0e7ee6b7a2d16e33b83ff15b341b982a92bbe7769a7f3d851c881511bdab8fa3d126058fc58e651f53a19e230d2ab61bf1ed329d57
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Joel Chippindale
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
6
+ rescue LoadError
7
+ # no rspec available
8
+ end
@@ -0,0 +1,48 @@
1
+ module BFIPlayerSearch
2
+ class ResultParser
3
+ def initialize(fragment)
4
+ @fragment = fragment
5
+ end
6
+
7
+ def title
8
+ fragment.css('span.title').first.content.strip
9
+ end
10
+
11
+ def year
12
+ if year_tag = fragment.css('span.film-year').first
13
+ year_tag.content.strip
14
+ end
15
+ end
16
+
17
+ def certificate
18
+ if cert_tag = fragment.css('img.certificate-image').first
19
+ cert_tag.attributes['alt'].to_s.strip
20
+ end
21
+ end
22
+
23
+ def free?
24
+ fragment.css('.price').first.content.include?('free')
25
+ end
26
+
27
+ def url
28
+ path = fragment.css('a').first.attributes['href'].to_s.strip
29
+ convert_to_url(path)
30
+ end
31
+
32
+ def image_url
33
+ path = fragment.css('figure img').first.attributes['src'].to_s.strip
34
+ convert_to_url(path)
35
+ end
36
+
37
+ private
38
+ attr_reader :fragment
39
+
40
+ def convert_to_url(path_or_url)
41
+ u = URI.parse(path_or_url)
42
+ u.host ||= 'player.bfi.org.uk'
43
+ u.scheme ||= 'http'
44
+ u.to_s
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,41 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+ require 'httpclient'
4
+
5
+ module BFIPlayerSearch
6
+ class Search
7
+ def search(query)
8
+ r = response(query)
9
+ films = film_fragments(r.body).map do |f|
10
+ rp = ResultParser.new(f)
11
+ {
12
+ :title => rp.title,
13
+ :url => rp.url,
14
+ :image_url => rp.image_url,
15
+ :year => rp.year,
16
+ :certificate => rp.certificate,
17
+ :free => rp.free?
18
+ }
19
+ end
20
+
21
+ if films.empty? & !no_results_page?(r.body)
22
+ raise BFIPlayerSearch::SearchResultsPageNotRecognised
23
+ else
24
+ films
25
+ end
26
+ end
27
+
28
+ private
29
+ def no_results_page?(page)
30
+ page.include?('returned no results')
31
+ end
32
+
33
+ def response(query)
34
+ HTTPClient.new.get('http://player.bfi.org.uk/search/', { 'q' => query })
35
+ end
36
+
37
+ def film_fragments(page)
38
+ Nokogiri::HTML(page).css('#search-results article.film')
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ module BFIPlayerSearch
2
+ class SearchResultsPageNotRecognised < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'bfi_player_search/search_results_page_not_recognised'
2
+ require 'bfi_player_search/result_parser'
3
+ require 'bfi_player_search/search'
4
+
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'A search' do
4
+ context 'with zero results', :vcr do
5
+ it { expect(BFIPlayerSearch::Search.new.search('qqqqqqqqqqqqqqqqq')).to be_empty }
6
+ end
7
+
8
+ context 'with some results', :vcr do
9
+ subject { BFIPlayerSearch::Search.new.search('girl') }
10
+
11
+ it { expect(subject).to_not be_empty }
12
+ it { expect(subject.first[:title]).to_not be_empty }
13
+ it { expect(subject.first[:url]).to match(%r{^http://player.bfi.org.uk/film/watch-.*}) }
14
+ it { expect(subject.first[:image_url]).to match(%r{^http://player.bfi.org.uk//media/images/stills/film/.*\.jpg}) }
15
+ end
16
+
17
+ context 'with unrecognised page format returned' do
18
+ before do
19
+ VCR.turn_off!
20
+
21
+ stub_request(:get, 'http://player.bfi.org.uk/search/?q=girl').
22
+ to_return(:body => '<html><body><h1>Not what you expected</h1></body></html>')
23
+ end
24
+
25
+ after do
26
+ VCR.turn_on!
27
+ end
28
+
29
+ it do
30
+ expect {
31
+ BFIPlayerSearch::Search.new.search('girl')
32
+ }.to raise_error(BFIPlayerSearch::SearchResultsPageNotRecognised)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe BFIPlayerSearch::ResultParser do
4
+ let(:title) { 'The 39 Steps' }
5
+ let(:year) { '1935' }
6
+ let(:certificate) { 'U' }
7
+ let(:url) { 'http://player.bfi.org.uk/film/watch-the-39-steps-1935/' }
8
+ let(:image_url) { 'http://player.bfi.org.uk//media/images/stills/film/6865/0f6237fc6f0a145a72ffa136a7ad88f6-320x180.jpg' }
9
+ let(:fragment) {
10
+ Nokogiri::HTML::DocumentFragment.parse("<article class='film'>
11
+ <a href='/film/watch-the-39-steps-1935/'>View The 39 Steps </a>
12
+ <figure>
13
+ <img src='//player.bfi.org.uk//media/images/stills/film/6865/0f6237fc6f0a145a72ffa136a7ad88f6-320x180.jpg'>
14
+ </figure>
15
+ <div class='film-preview'>
16
+ <div class='price'></div>
17
+ <h3 class='film-title'>
18
+ <span class='title'>#{title} </span>
19
+ <span class='film-year'>#{year}</span>
20
+ <img class='certificate-image' alt='#{certificate}'>
21
+ </h3>
22
+ </div>
23
+ </article>")
24
+ }
25
+
26
+ subject { BFIPlayerSearch::ResultParser.new(fragment) }
27
+
28
+ describe '#title' do
29
+ it { expect(subject.title).to eq(title) }
30
+ end
31
+
32
+ describe '#url' do
33
+ it { expect(subject.url).to eq(url) }
34
+ end
35
+
36
+ describe '#image_url' do
37
+ it { expect(subject.image_url).to eq(image_url) }
38
+ end
39
+
40
+ describe '#year' do
41
+ it { expect(subject.year).to eq(year) }
42
+
43
+ context 'without year' do
44
+ let(:fragment) {
45
+ Nokogiri::HTML::DocumentFragment.parse("<article class='film'>
46
+ <div class='film-preview'>
47
+ <h3 class='film-title'>
48
+ </h3>
49
+ </div>
50
+ </article>")
51
+ }
52
+
53
+ subject { BFIPlayerSearch::ResultParser.new(fragment) }
54
+
55
+ it { expect(subject.year).to be_nil }
56
+ end
57
+ end
58
+
59
+ describe '#certificate' do
60
+ it { expect(subject.certificate).to eq(certificate) }
61
+
62
+ context 'without certificate' do
63
+ let(:fragment) {
64
+ Nokogiri::HTML::DocumentFragment.parse("<article class='film'>
65
+ <div class='film-preview'>
66
+ <h3 class='film-title'>
67
+ </h3>
68
+ </div>
69
+ </article>")
70
+ }
71
+
72
+ subject { BFIPlayerSearch::ResultParser.new(fragment) }
73
+
74
+ it { expect(subject.certificate).to be_nil }
75
+ end
76
+ end
77
+
78
+ describe '#free?' do
79
+ it { expect(subject).to_not be_free }
80
+
81
+ context 'when free' do
82
+ let(:fragment) {
83
+ Nokogiri::HTML::DocumentFragment.parse("<article class='film'>
84
+ <div class='film-preview'>
85
+ <div class='price'> Watch for free </div>
86
+ <h3 class='film-title'>
87
+ </h3>
88
+ </div>
89
+ </article>")
90
+ }
91
+
92
+ subject { BFIPlayerSearch::ResultParser.new(fragment) }
93
+
94
+ it { expect(subject).to be_free }
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,106 @@
1
+ require 'vcr'
2
+ require 'webmock/rspec'
3
+ require 'bfi_player_search'
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = 'spec/cassettes'
7
+ c.hook_into :webmock
8
+ c.configure_rspec_metadata!
9
+ end
10
+
11
+ # This file was generated by the `rspec --init` command. Conventionally, all
12
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
13
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
14
+ # this file to always be loaded, without a need to explicitly require it in any
15
+ # files.
16
+ #
17
+ # Given that it is always loaded, you are encouraged to keep this file as
18
+ # light-weight as possible. Requiring heavyweight dependencies from this file
19
+ # will add to the boot time of your test suite on EVERY test run, even for an
20
+ # individual file that may not need all of that loaded. Instead, consider making
21
+ # a separate helper file that requires the additional dependencies and performs
22
+ # the additional setup, and require it from the spec files that actually need
23
+ # it.
24
+ #
25
+ # The `.rspec` file also contains a few flags that are not defaults but that
26
+ # users commonly want.
27
+ #
28
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
29
+ RSpec.configure do |config|
30
+ # rspec-expectations config goes here. You can use an alternate
31
+ # assertion/expectation library such as wrong or the stdlib/minitest
32
+ # assertions if you prefer.
33
+ config.expect_with :rspec do |expectations|
34
+ # This option will default to `true` in RSpec 4. It makes the `description`
35
+ # and `failure_message` of custom matchers include text for helper methods
36
+ # defined using `chain`, e.g.:
37
+ # be_bigger_than(2).and_smaller_than(4).description
38
+ # # => "be bigger than 2 and smaller than 4"
39
+ # ...rather than:
40
+ # # => "be bigger than 2"
41
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
42
+ end
43
+
44
+ # rspec-mocks config goes here. You can use an alternate test double
45
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
46
+ config.mock_with :rspec do |mocks|
47
+ # Prevents you from mocking or stubbing a method that does not exist on
48
+ # a real object. This is generally recommended, and will default to
49
+ # `true` in RSpec 4.
50
+ mocks.verify_partial_doubles = true
51
+ end
52
+
53
+ # The settings below are suggested to provide a good initial experience
54
+ # with RSpec, but feel free to customize to your heart's content.
55
+ =begin
56
+ # These two settings work together to allow you to limit a spec run
57
+ # to individual examples or groups you care about by tagging them with
58
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
59
+ # get run.
60
+ config.filter_run :focus
61
+ config.run_all_when_everything_filtered = true
62
+
63
+ # Allows RSpec to persist some state between runs in order to support
64
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
65
+ # you configure your source control system to ignore this file.
66
+ config.example_status_persistence_file_path = "spec/examples.txt"
67
+
68
+ # Limits the available syntax to the non-monkey patched syntax that is
69
+ # recommended. For more details, see:
70
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
71
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
73
+ config.disable_monkey_patching!
74
+
75
+ # This setting enables warnings. It's recommended, but in some cases may
76
+ # be too noisy due to issues in dependencies.
77
+ config.warnings = true
78
+
79
+ # Many RSpec users commonly either run the entire suite or an individual
80
+ # file, and it's useful to allow more verbose output when running an
81
+ # individual spec file.
82
+ if config.files_to_run.one?
83
+ # Use the documentation formatter for detailed output,
84
+ # unless a formatter has already been configured
85
+ # (e.g. via a command-line flag).
86
+ config.default_formatter = 'doc'
87
+ end
88
+
89
+ # Print the 10 slowest examples and example groups at the
90
+ # end of the spec run, to help surface which specs are running
91
+ # particularly slow.
92
+ config.profile_examples = 10
93
+
94
+ # Run specs in random order to surface order dependencies. If you find an
95
+ # order dependency and want to debug it, you can fix the order by providing
96
+ # the seed, which is printed after each run.
97
+ # --seed 1234
98
+ config.order = :random
99
+
100
+ # Seed global randomization in this process using the `--seed` CLI option.
101
+ # Setting this allows you to use `--seed` to deterministically reproduce
102
+ # test failures related to randomization by passing the same `--seed` value
103
+ # as the one that triggered the failure.
104
+ Kernel.srand config.seed
105
+ =end
106
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bfi_player_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joel Chippindale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.22'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.22'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Built with some wonky page scraping
98
+ email: joel@joelchippindale.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE.txt
104
+ - Rakefile
105
+ - lib/bfi_player_search.rb
106
+ - lib/bfi_player_search/result_parser.rb
107
+ - lib/bfi_player_search/search.rb
108
+ - lib/bfi_player_search/search_results_page_not_recognised.rb
109
+ - spec/feature/search_spec.rb
110
+ - spec/lib/bfi_player_search/result_parser_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/mocoso/bfi_player_search
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A simple search API for BFI player
136
+ test_files: []