meta_hari 0.0.1 → 0.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bff4312d86ff4bee5be187b150ef238f404369f5
4
- data.tar.gz: 06b08dbea9f2720e174f67914a208d3c7ae7ede2
3
+ metadata.gz: 7ca6a8f17d9a5f3c73f162378b707d4acdc2aebe
4
+ data.tar.gz: b22862f8ea3b987d2953b4a1d3e457f91d107e9c
5
5
  SHA512:
6
- metadata.gz: 735398073cd3179c1cb9e7319fcf4cc050448ca9d187b7834eb4115f8a2f8dafc5d97ea0ec580d7feec967a35aaa38db8503a677a264cb8392291998133c8a52
7
- data.tar.gz: 919c189e56767d3ec3f469a9e8083e65d0601634eda0e0c189e0f3a51726f2deaa9921e62268cd9f94dded4db89daf8c554352431c0443a0cbf725ff26ff45b1
6
+ metadata.gz: 9dc4fecc168a0d788f247db9c5fc8dab2f46099aad3a2c13bad90ac3cff321972b7a1af8c517f0623c96bd51009d1bedbb9081771b3be8feaad2e86a205ffe19
7
+ data.tar.gz: 3c09350683a98c691cc5b58b1c546ea6596223462c0ef5ff95b9bf867b1cf80c56526ea41557590629f3ef8f0506719c762de48afdf355c98bf0c3ab75443f5e
data/README.md CHANGED
@@ -42,6 +42,7 @@ custom shops which can not be spyed by the generic spyglass
42
42
  * Amazon DE
43
43
  * Generic
44
44
  * Shops using [JSON-LD](https://developers.google.com/structured-data/rich-snippets/products)
45
+ * Shops using [Microdata](https://developers.google.com/structured-data/rich-snippets/products)
45
46
 
46
47
  ### Creating a spyglass
47
48
 
@@ -0,0 +1,42 @@
1
+ module MetaHari
2
+ module Helpers
3
+ class Microdata
4
+ attr_reader :document
5
+ attr_reader :url
6
+
7
+ def initialize(document, url)
8
+ @document = document
9
+ @url = url
10
+ end
11
+
12
+ def data(type = 'http://schema.org/Product')
13
+ result = array.find { |hash| hash[:type].include? type }
14
+ result && format(result) || {}
15
+ end
16
+
17
+ protected
18
+
19
+ def format(result)
20
+ data = result[:properties]
21
+ {
22
+ '@type' => result[:type] && result[:type].first,
23
+ 'name' => data['name'] && data['name'].first,
24
+ 'image' => data['image'] && data['image'].first,
25
+ 'description' => data['description'] && data['description'].first
26
+ }
27
+ end
28
+
29
+ def array
30
+ @hash ||= items.map(&:to_hash)
31
+ end
32
+
33
+ def items
34
+ itemscopes = document.search('//*[@itemscope and not(@itemprop)]')
35
+ return [] unless itemscopes
36
+ itemscopes.collect do |itemscope|
37
+ ::Microdata::Item.new(itemscope, url)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,5 @@
1
1
  require 'meta_hari/helpers/json_ld'
2
+ require 'meta_hari/helpers/microdata'
2
3
 
3
4
  module MetaHari
4
5
  module Helpers
@@ -15,7 +15,8 @@ module MetaHari
15
15
 
16
16
  def spy
17
17
  OpenStruct.new [
18
- spy_json_ld
18
+ spy_json_ld,
19
+ spy_microdata
19
20
  ].inject({}) { |a, e| a.merge e }
20
21
  end
21
22
 
@@ -58,6 +59,11 @@ module MetaHari
58
59
  json_ld = MetaHari::Helpers::JsonLd.new(document)
59
60
  json_ld.data
60
61
  end
62
+
63
+ def spy_microdata
64
+ microdata = MetaHari::Helpers::Microdata.new(document, uri.to_s)
65
+ microdata.data
66
+ end
61
67
  end
62
68
  end
63
69
  end
@@ -1,4 +1,4 @@
1
1
  # MetaHari version
2
2
  module MetaHari
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
data/lib/meta_hari.rb CHANGED
@@ -2,6 +2,7 @@ require 'nokogiri'
2
2
  require 'json'
3
3
  require 'uri'
4
4
  require 'net/http'
5
+ require 'microdata'
5
6
  require 'meta_hari/version'
6
7
  require 'meta_hari/helpers'
7
8
  require 'meta_hari/spyglass'
data/meta_hari.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'guard-rubocop', '~> 1.2.0'
27
27
  spec.add_development_dependency 'pry', '~> 0.10.1'
28
28
  spec.add_development_dependency 'nokogiri', '~> 1.6.6'
29
+ spec.add_development_dependency 'microdata', '~> 0.0.3'
29
30
  end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetaHari::Helpers::Microdata do
4
+ let(:url) do
5
+ [
6
+ 'http://www.baby-markt.de/markenshop-big',
7
+ 'big-bobby-car/big-bobby-car-classic-rot-a006497.html'
8
+ ].join('/')
9
+ end
10
+ let(:html) { resource_content('baby_markt_de.html') }
11
+ let(:document) { Nokogiri::HTML html }
12
+ let(:instance) { described_class.new document, url }
13
+ subject { instance }
14
+
15
+ describe '#initialize' do
16
+ it 'assigns the document to the reader' do
17
+ expect(subject.document).to be document
18
+ end
19
+ end
20
+
21
+ context 'when containing Microdata' do
22
+ describe '#hash' do
23
+ subject { instance.send :array }
24
+
25
+ it { should be_an Array }
26
+ end
27
+
28
+ describe '#data' do
29
+ it 'uses #array' do
30
+ expect(subject).to receive(:array).and_return([])
31
+ subject.data
32
+ end
33
+
34
+ context 'when type matches' do
35
+ subject { instance.data }
36
+
37
+ it { should be_a Hash }
38
+ it { should have_key '@type' }
39
+ it { should have_key 'name' }
40
+ it { should have_key 'image' }
41
+ it { should have_key 'description' }
42
+
43
+ it 'is a product' do
44
+ expect(subject['@type']).to eql 'http://schema.org/Product'
45
+ end
46
+
47
+ it 'has matching name' do
48
+ expect(subject['name']).to eql 'BIG Bobby Car Classic rot'
49
+ end
50
+ end
51
+
52
+ context 'when type does not match' do
53
+ subject { instance.data('http://schema.org/Foo') }
54
+
55
+ it { should be_a Hash }
56
+ it { should_not have_key '@type' }
57
+ it { should_not have_key 'name' }
58
+ it { should_not have_key 'image' }
59
+ it { should_not have_key 'description' }
60
+ end
61
+ end
62
+ end
63
+
64
+ context 'when not containing Microdata' do
65
+ let(:html) { '' }
66
+
67
+ describe '#array' do
68
+ subject { instance.send :array }
69
+
70
+ it { should be_an Array }
71
+ end
72
+ end
73
+ end
@@ -77,5 +77,44 @@ describe MetaHari::Spyglass::Base do
77
77
  expect(subject.description).to eql expected_value
78
78
  end
79
79
  end
80
+
81
+ context 'with Microdata document' do
82
+ let(:uri) { URI.parse 'http://baby-markt.de/' }
83
+ let(:instance) { described_class.new(uri) }
84
+ let(:html) { resource_content 'baby_markt_de.html' }
85
+ subject { instance.spy }
86
+
87
+ before :each do
88
+ allow(instance).to receive(:fetch_data).and_return(html)
89
+ end
90
+
91
+ it { should be_an OpenStruct }
92
+ it { should respond_to :name }
93
+ it { should respond_to :image }
94
+ it { should respond_to :description }
95
+
96
+ it 'has the correct name' do
97
+ expect(subject.name).to eql 'BIG Bobby Car Classic rot'
98
+ end
99
+
100
+ it 'has the correct image url' do
101
+ expected_value = [
102
+ 'http://www.baby-markt.de/out/pictures/generated/product/1',
103
+ '390_390_80/n_a006497_001.jpg?20150511132615'
104
+ ].join('/')
105
+ expect(subject.image).to eql expected_value
106
+ end
107
+
108
+ it 'has the correct description' do
109
+ expected_value = [
110
+ 'BIG Bobby Car ClassicArtikelnummer: 800001303 Dank des',
111
+ 'kindgerechten Designs, der unverwüstlichen Konstruktion und der',
112
+ 'vielseitigen Funktionalität iist das BIG Bobby Car Classic das',
113
+ 'meistgekaufte Kinderrutsch-fahrzeug der Welt. Und dabei macht BIG',
114
+ 'Bobby...'
115
+ ].join(' ')
116
+ expect(subject.description).to eql expected_value
117
+ end
118
+ end
80
119
  end
81
120
  end