open_graphy 1.0.0

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.
@@ -0,0 +1,22 @@
1
+ RSpec.describe OpenGraphy do
2
+
3
+ describe OpenGraphy::Configuration do
4
+ before do
5
+ OpenGraphy.configure do |config|
6
+ config.metatags = ['og:', 'onthebeach:deal:', 'onthebeach:hotel:']
7
+ config.user_agent = 'OpenGraphyBot'
8
+ end
9
+ end
10
+
11
+ describe '#metatags' do
12
+ let(:expected_tags){ ['og:', 'onthebeach:deal:', 'onthebeach:hotel:'] }
13
+ it{ expect(OpenGraphy.configuration.metatags).to eq(expected_tags) }
14
+ end
15
+
16
+ describe '#user_agent' do
17
+ it 'returns the user agent' do
18
+ expect(OpenGraphy.configuration.user_agent).to eq('OpenGraphyBot')
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ RSpec.describe OpenGraphy::Uri::Fetcher do
2
+ describe '#fetch' do
3
+ let(:fetcher) { OpenGraphy::Uri::Fetcher.new(uri) }
4
+ let(:uri) { 'https://www.onthebeach.co.uk/test' }
5
+ let(:port) { 443 }
6
+
7
+ let(:http) { double('HTTP') }
8
+ let(:request) {
9
+ double(
10
+ 'Request',
11
+ value: 'test',
12
+ )
13
+ }
14
+
15
+ before do
16
+ allow(Net::HTTP).to receive(:new).
17
+ with('www.onthebeach.co.uk', port).
18
+ and_return(http)
19
+
20
+ allow(Net::HTTP::Get).to receive(:new).
21
+ with('/test', { 'User-Agent' => 'OpenGraphyBot' }).
22
+ and_return(request)
23
+
24
+ allow(request).to receive(:initialize_http_header)
25
+
26
+ allow(http).to receive(:request).
27
+ with(request).
28
+ and_return(request)
29
+ end
30
+
31
+ it 'uses ssl when the scheme is https' do
32
+ expect(http).to receive(:use_ssl=).with(true)
33
+
34
+ fetcher.fetch
35
+ end
36
+
37
+ context 'with non ssl url' do
38
+ let(:uri) { 'http://www.onthebeach.co.uk/test' }
39
+ let(:port) { 80 }
40
+
41
+ it 'uses ssl when the scheme is https' do
42
+ expect(http).to receive(:use_ssl=).with(false)
43
+
44
+ fetcher.fetch
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ module OpenGraphy
2
+ RSpec.describe MetaTag do
3
+ let(:meta_tag) { MetaTag.new(doc, meta_tag_element) }
4
+ let(:doc) { double('MokogiriDoc') }
5
+ let(:meta_tag_element) { double('MokogiriElement') }
6
+
7
+ describe '#valid?' do
8
+ subject { meta_tag.valid? }
9
+
10
+ context 'when the tag exists in the metatag list' do
11
+ before do
12
+ allow(meta_tag_element).to receive(:attr).
13
+ with('property').and_return('og:title')
14
+ end
15
+
16
+ it { should be_truthy }
17
+ end
18
+
19
+ context 'tag is not in the metatag list' do
20
+ before do
21
+ allow(meta_tag_element).to receive(:attr).
22
+ with('property').and_return('unidentified_tag_name')
23
+ end
24
+
25
+ it { should be_falsy }
26
+ end
27
+ end
28
+
29
+ describe '#name' do
30
+ before do
31
+ allow(meta_tag_element).to receive(:attr).
32
+ with('property').and_return('og:title')
33
+ end
34
+
35
+ it 'returns the tag name withouth the prefix' do
36
+ expect(meta_tag.name).to eql('title')
37
+ end
38
+ end
39
+
40
+ describe '#value' do
41
+ before do
42
+ allow(meta_tag_element).to receive(:attr).
43
+ with('content').and_return('Open Graph protocol is great!')
44
+ end
45
+
46
+ it 'returns the tag value' do
47
+ expect(meta_tag.value).to eql('Open Graph protocol is great!')
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,63 @@
1
+ RSpec.describe OpenGraphy::MetaTags do
2
+ let(:meta_tags) { OpenGraphy::MetaTags.new }
3
+
4
+ describe '#add' do
5
+ context 'with a title' do
6
+ before do
7
+ meta_tags.add('title', 'Laguna Park II, Tenerife')
8
+ end
9
+
10
+ it 'has a title' do
11
+ expect(meta_tags.title?).to be(true)
12
+ end
13
+
14
+ it 'returns the title' do
15
+ expect(meta_tags.title).to eql('Laguna Park II, Tenerife')
16
+ end
17
+ end
18
+
19
+ context 'without a title but with a page title' do
20
+ before do
21
+ meta_tags.add('__html_title_tag', 'Laguna Park Hotel')
22
+ end
23
+
24
+ it 'has a title' do
25
+ expect(meta_tags.title?).to be(true)
26
+ end
27
+
28
+ it 'sets the title as the page title' do
29
+ expect(meta_tags.title).to eql('Laguna Park Hotel')
30
+ end
31
+ end
32
+
33
+ context 'with a good image url' do
34
+ before do
35
+ meta_tags.add('image', 'http://www.foobar.com/foo.jpg')
36
+ end
37
+
38
+ it 'has an image' do
39
+ expect(meta_tags.image?).to be(true)
40
+ end
41
+
42
+ it 'returns the image url' do
43
+ expect(meta_tags.image).to eql('http://www.foobar.com/foo.jpg')
44
+ end
45
+ end
46
+
47
+ context 'with a bad image url' do
48
+ before do
49
+ meta_tags.add('image', '123')
50
+ end
51
+
52
+ it 'does not have an image' do
53
+ expect(meta_tags.image?).to be(false)
54
+ end
55
+ end
56
+
57
+ context 'without a description' do
58
+ it 'has no description' do
59
+ expect(meta_tags.description?).to be(false)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,55 @@
1
+ module OpenGraphy
2
+ RSpec.describe TagNamespace do
3
+ let(:tag_namespace) { TagNamespace.new(namespace) }
4
+ let(:namespace) { [] }
5
+
6
+ describe '#any?' do
7
+ context 'without any namespaces' do
8
+ let(:namespace) { [] }
9
+
10
+ it 'returns false' do
11
+ expect(tag_namespace.any?).to eql(false)
12
+ end
13
+ end
14
+
15
+ context 'with the default og namespace' do
16
+ let(:namespace) { ['og'] }
17
+
18
+ it 'returns false to avoid namespacing default tags' do
19
+ expect(tag_namespace.any?).to eql(false)
20
+ end
21
+ end
22
+
23
+ context 'with some custom namespaces' do
24
+ let(:namespace) { ['product', 'sku'] }
25
+
26
+ it 'returns true' do
27
+ expect(tag_namespace.any?).to eql(true)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '#next' do
33
+ it 'returns an instance of TagNamspace' do
34
+ expect(tag_namespace.next).to be_an_instance_of(TagNamespace)
35
+ end
36
+
37
+ context 'when there is no additional namespace' do
38
+ let(:namespace) { [] }
39
+
40
+ it 'returns a TagNamespace without any items' do
41
+ expect(tag_namespace.next.any?).to eql(false)
42
+ end
43
+ end
44
+
45
+ context 'with additional namespaces' do
46
+ let(:namespace) { ['product', 'sku'] }
47
+
48
+ it 'returns a TagNamespace with the addtional items' do
49
+ expect(tag_namespace.next.any?).to eql(true)
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ RSpec.describe OpenGraphy do
2
+ subject {
3
+ VCR.use_cassette('pinterest/pin') do
4
+ OpenGraphy::Uri.open('http://uk.pinterest.com/pin/384213411933800344/')
5
+ end
6
+ }
7
+
8
+ describe '#open' do
9
+ context 'with a good url' do
10
+ it 'should redirect and return a response' do
11
+ expect(subject).to include('<meta property="og:site_name" content="Pinterest">')
12
+ end
13
+ end
14
+
15
+ context 'with a bad url' do
16
+ it 'should throw a BadUriError' do
17
+ expect{OpenGraphy::Uri.fetch('/title/test/')}.to raise_error(OpenGraphy::Uri::BadUriError)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module OpenGraphy
2
+ RSpec.describe Url do
3
+ describe '.fetch' do
4
+ let(:og_data) { Url.fetch(uri) }
5
+ let(:uri) { 'http://www.imdb.com/title/tt0107048/' }
6
+ let(:meta_tags) { [] }
7
+ let(:file) { File.new('spec/support/fixtures/groundhog_day.html') }
8
+
9
+ before do
10
+ expect(Uri).to receive(:open).with(uri).
11
+ and_return(file)
12
+ end
13
+
14
+ it 'has a url' do
15
+ expect(og_data.url).to eql('http://www.imdb.com/title/tt0107048/')
16
+ end
17
+
18
+ it 'has an image' do
19
+ expect(og_data.image).to eql('http://ia.media-imdb.com/images/M/MV5BMTU0MzQyNTExMV5BMl5BanBnXkFtZTgwMjA0Njk1MDE@._V1_.jpg')
20
+ end
21
+
22
+ it 'has a title' do
23
+ expect(og_data.title).to eql('Groundhog Day (1993)')
24
+ end
25
+
26
+ it 'thinks it has an image' do
27
+ expect(og_data.image?).to eql(true)
28
+ end
29
+
30
+ it 'does not have a cat' do
31
+ expect(og_data.cat?).to eql(false)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ module OpenGraphy
2
+ RSpec.describe UrlValidator do
3
+
4
+ describe '#fetch' do
5
+ context 'with a valid url' do
6
+ let (:url) { "http://www.onthebeach.co.uk" }
7
+
8
+ it 'is a valid' do
9
+ expect(UrlValidator.new(url).valid?).to be(true)
10
+ end
11
+ end
12
+
13
+ context 'with an invalid url' do
14
+ let(:url) { "abcde12345" }
15
+
16
+ it 'is not valid' do
17
+ expect(UrlValidator.new(url).valid?).to be(false)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenGraphy do
4
+ subject {
5
+ VCR.use_cassette('imdb/tt2084970') do
6
+ OpenGraphy.fetch(url)
7
+ end
8
+ }
9
+ let(:url) { 'http://www.imdb.com/title/tt2084970/?ref_=inth_ov_tt' }
10
+
11
+ describe '.fetch' do
12
+ it 'should return an object with the opengraph data' do
13
+ expect(subject).to be_kind_of(OpenGraphy::MetaTags)
14
+ end
15
+ end
16
+
17
+ describe 'custom metatags' do
18
+ let(:open_graphy) {
19
+ VCR.use_cassette('onthebeach/deals') do
20
+ OpenGraphy.fetch(url)
21
+ end
22
+ }
23
+ let(:url) { 'https://www.onthebeach.co.uk/deals/53ee67c676036401a67eab73026a97f9/e01a07efddb6e124da373b31222c162f/80507aab0fb81591a992fcc5b77d93a4' }
24
+
25
+ before do
26
+ OpenGraphy.configure do |config|
27
+ config.metatags = ["og:", "onthebeach:deal:", "onthebeach:hotel:"]
28
+ end
29
+ end
30
+
31
+ it { expect(open_graphy.title).to eq('Vilamoura Golf (Vilamoura, Costa de Algarve, Portugal)') }
32
+ it { expect(open_graphy.description).to include('With nearly 200 km of unrivalled coastline') }
33
+ it { expect(open_graphy.image).to eq('http://hotels.onthebeach.co.uk/assets/hotel_images/000/345/599/original/vilamoura-golf.jpg') }
34
+ it { expect(open_graphy.type).to eq('website') }
35
+ it { expect(open_graphy.url).to eq('https://www.onthebeach.co.uk/deals/53ee67c676036401a67eab73026a97f9/e01a07efddb6e124da373b31222c162f/80507aab0fb81591a992fcc5b77d93a4') }
36
+ it { expect(open_graphy.site_name).to eq('On The Beach') }
37
+ it { expect(open_graphy.onthebeach.deal.id).to eq('53ee67c676036401a67eab73026a97f9') }
38
+ it { expect(open_graphy.onthebeach.deal.hotel_id).to eq('281399') }
39
+ it { expect(open_graphy.onthebeach.deal.board_code).to eq('SC') }
40
+ it { expect(open_graphy.onthebeach.deal.price).to eq('287.40') }
41
+ it { expect(open_graphy.onthebeach.deal.hotel_result_id).to eq('53ee67c676036401a67eab73026a97f9') }
42
+ it { expect(open_graphy.onthebeach.deal.board_result_id).to eq('b736a3ddafc484424470a056445671d4') }
43
+ it { expect(open_graphy.onthebeach.deal.flight_result_id).to eq('80507aab0fb81591a992fcc5b77d93a4') }
44
+ end
45
+
46
+ describe 'try to fetch a webpage that does not exist' do
47
+ let(:open_graphy_data) {
48
+ VCR.use_cassette('google/404') do
49
+ OpenGraphy.fetch('http://google.com/404.html')
50
+ end
51
+ }
52
+
53
+ it "return data class with url" do
54
+ expect(open_graphy_data.url).to eql('http://google.com/404.html')
55
+ end
56
+ end
57
+
58
+ describe 'try to fetch a webpage that has no opengraph url' do
59
+ let(:url) { 'http://www.tripadvisor.co.uk/Hotel_Review-g198832-d236315-Reviews-Grand_Hotel_Kronenhof-Pontresina_Engadin_St_Moritz_Canton_of_Graubunden_Swiss_Alps.html'}
60
+ let(:open_graphy_data) {
61
+ VCR.use_cassette('tripadvisor/hotel') do
62
+ OpenGraphy.fetch(url)
63
+ end
64
+ }
65
+
66
+ it 'should return the url passed in' do
67
+ expect(open_graphy_data.url?).to be(true)
68
+ expect(open_graphy_data.url).to eql('http://www.tripadvisor.co.uk/Hotel_Review-g198832-d236315-Reviews-Grand_Hotel_Kronenhof-Pontresina_Engadin_St_Moritz_Canton_of_Graubunden_Swiss_Alps.html')
69
+ end
70
+ end
71
+
72
+
73
+ describe 'try and fetch a webpage that redirects' do
74
+ let(:url) { 'http://uk.pinterest.com/pin/384213411933800344/' }
75
+ let(:open_graphy_data) {
76
+ VCR.use_cassette('pinterest/pin') do
77
+ OpenGraphy.fetch(url)
78
+ end
79
+ }
80
+
81
+ it 'should follow the redirect and return data class' do
82
+ expect(open_graphy_data.url?).to be(true)
83
+ expect(open_graphy_data.url).to eql('https://www.pinterest.com/pin/384213411933800344/')
84
+ expect(open_graphy_data.title?).to be(true)
85
+ expect(open_graphy_data.title).to eql('car')
86
+ expect(open_graphy_data.image?).to be(true)
87
+ expect(open_graphy_data.image).to eql('https://s-media-cache-ak0.pinimg.com/736x/7c/e6/fe/7ce6fea0a4f281573a1c7f2c68d13d5a.jpg')
88
+ expect(open_graphy_data.type?).to be(true)
89
+ expect(open_graphy_data.type).to eql('pinterestapp:pin')
90
+ expect(open_graphy_data.description?).to be(true)
91
+ expect(open_graphy_data.description).to eql('Mercedes SLS | Luxury | Sport | Car | http://amazingsportcarcollections.blogspot.com')
92
+ expect(open_graphy_data.site_name?).to be(true)
93
+ expect(open_graphy_data.site_name).to eql('Pinterest')
94
+ expect(open_graphy_data.see_also?).to be(true)
95
+ expect(open_graphy_data.see_also).to eql('http://amazingsportcarcollections.blogspot.com/2013/09/recaro-performance-sport-car-seat-back.html')
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,98 @@
1
+ require 'vcr'
2
+ require 'open_graphy'
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ #
20
+ VCR.configure do |c|
21
+ c.cassette_library_dir = 'spec/cassettes'
22
+ c.hook_into :webmock
23
+ c.configure_rspec_metadata!
24
+ end
25
+
26
+ RSpec.configure do |config|
27
+ # rspec-expectations config goes here. You can use an alternate
28
+ # assertion/expectation library such as wrong or the stdlib/minitest
29
+ # assertions if you prefer.
30
+ config.expect_with :rspec do |expectations|
31
+ # This option will default to `true` in RSpec 4. It makes the `description`
32
+ # and `failure_message` of custom matchers include text for helper methods
33
+ # defined using `chain`, e.g.:
34
+ # be_bigger_than(2).and_smaller_than(4).description
35
+ # # => "be bigger than 2 and smaller than 4"
36
+ # ...rather than:
37
+ # # => "be bigger than 2"
38
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
39
+ end
40
+
41
+ # rspec-mocks config goes here. You can use an alternate test double
42
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
43
+ config.mock_with :rspec do |mocks|
44
+ # Prevents you from mocking or stubbing a method that does not exist on
45
+ # a real object. This is generally recommended, and will default to
46
+ # `true` in RSpec 4.
47
+ mocks.verify_partial_doubles = true
48
+ end
49
+
50
+ # The settings below are suggested to provide a good initial experience
51
+ # with RSpec, but feel free to customize to your heart's content.
52
+ =begin
53
+ # These two settings work together to allow you to limit a spec run
54
+ # to individual examples or groups you care about by tagging them with
55
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
56
+ # get run.
57
+ config.filter_run :focus
58
+ config.run_all_when_everything_filtered = true
59
+
60
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
61
+ # For more details, see:
62
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = 'doc'
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end