open_graph_reader 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/open_graph_reader.rb +25 -5
- data/lib/open_graph_reader/base.rb +7 -1
- data/lib/open_graph_reader/builder.rb +68 -40
- data/lib/open_graph_reader/configuration.rb +51 -0
- data/lib/open_graph_reader/definitions.rb +1 -1
- data/lib/open_graph_reader/fetcher.rb +21 -3
- data/lib/open_graph_reader/object.rb +6 -3
- data/lib/open_graph_reader/object/dsl.rb +36 -12
- data/lib/open_graph_reader/object/dsl/types.rb +4 -2
- data/lib/open_graph_reader/object/registry.rb +12 -2
- data/lib/open_graph_reader/parser.rb +17 -6
- data/lib/open_graph_reader/version.rb +1 -1
- data/spec/fixtures/real_world/missing_image.html +985 -0
- data/spec/fixtures/real_world/mixed_case_properties.html +1139 -0
- data/spec/fixtures/real_world/mixed_case_type.html +1008 -0
- data/spec/fixtures/real_world/not_a_reference.html +814 -0
- data/spec/fixtures/real_world/undefined_property.html +491 -0
- data/spec/fixtures/real_world/unknown_namespace.html +2033 -0
- data/spec/fixtures/real_world/unknown_type.html +2032 -0
- data/spec/integration/invalid_examples_spec.rb +42 -3
- data/spec/integration/real_world_spec.rb +121 -0
- data/spec/integration/valid_examples_spec.rb +0 -1
- data/spec/spec_helper.rb +9 -1
- metadata +20 -3
@@ -11,8 +11,25 @@ RSpec.describe "invalid examples" do
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
# Most parsers synthesize missing required properties, however
|
15
|
+
# we do not (yet), making this example an invalid object
|
16
|
+
describe "min" do
|
17
|
+
it "has missing required properties" do
|
18
|
+
expect {
|
19
|
+
OpenGraphReader.parse! example_html 'min'
|
20
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /Missing required/
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns what's there if required property validation is disabled" do
|
24
|
+
OpenGraphReader.config.validate_required = false
|
25
|
+
object = OpenGraphReader.parse! example_html 'min'
|
26
|
+
expect(object.og.site_name).to eq "Open Graph protocol examples"
|
27
|
+
expect(object.og.description).to eq "Content not on page"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
14
31
|
describe "filters/xss-image" do
|
15
|
-
it "errors on the invaid URL" do
|
32
|
+
it "errors on the invaid URL in strict mode" do
|
16
33
|
expect {
|
17
34
|
OpenGraphReader.parse! example_html 'filters/xss-image'
|
18
35
|
}.to raise_error OpenGraphReader::InvalidObjectError, /does not start with http/
|
@@ -44,19 +61,41 @@ RSpec.describe "invalid examples" do
|
|
44
61
|
end
|
45
62
|
|
46
63
|
describe "errors/geo" do
|
47
|
-
it "doesn't recognize the old elements" do
|
64
|
+
it "doesn't recognize the old elements in strict mode" do
|
65
|
+
OpenGraphReader.config.strict = true
|
66
|
+
|
48
67
|
expect {
|
49
68
|
OpenGraphReader.parse! example_html 'errors/geo'
|
50
69
|
}.to raise_error OpenGraphReader::InvalidObjectError, /Undefined property/
|
51
70
|
end
|
71
|
+
|
72
|
+
it "parses in default mode" do
|
73
|
+
object = OpenGraphReader.parse! example_html 'errors/geo'
|
74
|
+
|
75
|
+
expect(object.og.type).to eq "website"
|
76
|
+
expect(object.og.title).to eq "Open Graph protocol 1.0 location data"
|
77
|
+
expect(object.og.url).to eq "http://examples.opengraphprotocol.us/errors/geo.html"
|
78
|
+
expect(object.og.image.url).to eq "http://examples.opengraphprotocol.us/media/images/50.png"
|
79
|
+
end
|
52
80
|
end
|
53
81
|
|
54
82
|
describe "errors/type" do
|
55
|
-
it "doesn't handle the
|
83
|
+
it "doesn't handle the unknown type in strict mode" do
|
84
|
+
OpenGraphReader.config.strict = true
|
85
|
+
|
56
86
|
expect {
|
57
87
|
OpenGraphReader.parse! example_html 'errors/type'
|
58
88
|
}.to raise_error OpenGraphReader::InvalidObjectError, /Undefined type/
|
59
89
|
end
|
90
|
+
|
91
|
+
it "parses the known properties" do
|
92
|
+
object = OpenGraphReader.parse! example_html 'errors/type'
|
93
|
+
|
94
|
+
expect(object.og.type).to eq "fubar"
|
95
|
+
expect(object.og.title).to eq "Undefined global type"
|
96
|
+
expect(object.og.url).to eq "http://examples.opengraphprotocol.us/errors/type.html"
|
97
|
+
expect(object.og.image.url).to eq "http://examples.opengraphprotocol.us/media/images/50.png"
|
98
|
+
end
|
60
99
|
end
|
61
100
|
|
62
101
|
describe "errors/video-duration" do
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe "real world examples" do
|
4
|
+
describe "mixed_case_properties" do
|
5
|
+
it "parses" do
|
6
|
+
expect {
|
7
|
+
OpenGraphReader.parse! fixture_html 'real_world/mixed_case_properties'
|
8
|
+
}.to_not raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "assigns the right attributes" do
|
12
|
+
object = OpenGraphReader.parse fixture_html 'real_world/mixed_case_properties'
|
13
|
+
|
14
|
+
expect(object.og.title).to eq "Eine Million Unterschriften gegen TTIP"
|
15
|
+
expect(object.og.type).to eq "website"
|
16
|
+
expect(object.og.locale.to_s).to eq "de_DE"
|
17
|
+
expect(object.og.url).to eq "http://www.heise.de/tp/artikel/43/43516/"
|
18
|
+
expect(object.og.site_name).to eq "Telepolis"
|
19
|
+
expect(object.og.image.url).to eq "http://www.heise.de/tp/artikel/43/43516/43516_1.jpg"
|
20
|
+
expect(object.og.description).to eq "Ungenehmigte Bürgerinitiative will das Paket EU-Kommissionschef Juncker zum Geburtstag schenken"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "missing_image" do
|
25
|
+
it "does not parse" do
|
26
|
+
expect {
|
27
|
+
OpenGraphReader.parse! fixture_html 'real_world/missing_image'
|
28
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /Missing required/
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "mixed_case_type" do
|
33
|
+
it "parses" do
|
34
|
+
expect {
|
35
|
+
OpenGraphReader.parse! fixture_html 'real_world/mixed_case_type'
|
36
|
+
}.to_not raise_error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "not_a_reference" do
|
41
|
+
it "does not parse" do
|
42
|
+
expect {
|
43
|
+
OpenGraphReader.parse! fixture_html 'real_world/not_a_reference'
|
44
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /does not start with/
|
45
|
+
end
|
46
|
+
|
47
|
+
it "parses with reference validation turned of" do
|
48
|
+
OpenGraphReader.config.validate_references = false
|
49
|
+
object = OpenGraphReader.parse! fixture_html 'real_world/not_a_reference'
|
50
|
+
|
51
|
+
expect(object.og.title).to eq "Emergency call system for all new cars by 2018"
|
52
|
+
expect(object.og.type).to eq "article"
|
53
|
+
expect(object.og.description).to eq "The European Parliament and EU member states have agreed that new cars must be fitted with an automated system to alert emergency services in event of a crash."
|
54
|
+
expect(object.og.site_name).to eq "BBC News"
|
55
|
+
expect(object.og.url).to eq "http://www.bbc.co.uk/news/technology-30337272"
|
56
|
+
expect(object.og.image.url).to eq "http://news.bbcimg.co.uk/media/images/79520000/jpg/_79520623_79519885.jpg"
|
57
|
+
expect(object.article.author.to_s).to eq "BBC News"
|
58
|
+
expect(object.article.section).to eq "Technology"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "unknown_type" do
|
63
|
+
it "parses" do
|
64
|
+
object = OpenGraphReader.parse! fixture_html 'real_world/unknown_type'
|
65
|
+
|
66
|
+
expect(object.og.url).to eq "http://www.instructables.com/id/Building-the-Open-Knit-machine/"
|
67
|
+
expect(object.og.title).to eq "Building the OpenKnit machine"
|
68
|
+
expect(object.og.image.url).to eq "http://cdn.instructables.com/FI2/D7XW/I2XTQWFE/FI2D7XWI2XTQWFE.RECTANGLE1.jpg"
|
69
|
+
expect(object.og.description).to eq "The OpenKnit machine is an open-source, low cost, digital fabrication tool developed by Gerard Rubio. The machine affords the user the opportunity to..."
|
70
|
+
end
|
71
|
+
|
72
|
+
it "does not parse in strict mode" do
|
73
|
+
OpenGraphReader.config.strict = true
|
74
|
+
|
75
|
+
expect {
|
76
|
+
OpenGraphReader.parse! fixture_html 'real_world/unknown_type'
|
77
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /Undefined type/
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "undefined_property" do
|
82
|
+
it "parses" do
|
83
|
+
object = OpenGraphReader.parse! fixture_html 'real_world/undefined_property'
|
84
|
+
|
85
|
+
expect(object.og.locale.to_s).to eq "es_ES"
|
86
|
+
expect(object.og.type).to eq "article"
|
87
|
+
expect(object.og.title).to eq "Profesores y campesinos amarran a infiltrados en marcha"
|
88
|
+
expect(object.og.description).to eq "Regeneración, 6 de diciembre de 2014.-Durante la marcha que realizan profesores y organizaciones campesinas sobre avenida Paseo de la Reforma, maestros de la Coordinadora Estatal de Trabajadores de la Educación en Guerrero (CETEG) ubicaron a 12 jóvenes como “infiltrados”, a quienes amarraron de las manos en una cadena humana para evitar que marchen con ellos, informó El …"
|
89
|
+
expect(object.og.url).to eq "http://regeneracion.mx/sociedad/profesores-y-campesinos-amarran-a-infiltrados-en-marcha/"
|
90
|
+
expect(object.og.site_name).to eq "Regeneración"
|
91
|
+
expect(object.og.image.url).to eq "http://regeneracion.mx/wp-content/uploads/2014/12/Infiltrados.jpg"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "does not parse in strict mode" do
|
95
|
+
OpenGraphReader.config.strict = true
|
96
|
+
|
97
|
+
expect {
|
98
|
+
OpenGraphReader.parse! fixture_html 'real_world/undefined_property'
|
99
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /Undefined property/
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "unknown_namespace" do
|
104
|
+
it "parses" do
|
105
|
+
object = OpenGraphReader.parse! fixture_html 'real_world/unknown_namespace'
|
106
|
+
|
107
|
+
expect(object.og.url).to eq "http://www.instructables.com/id/Building-the-Open-Knit-machine/"
|
108
|
+
expect(object.og.title).to eq "Building the OpenKnit machine"
|
109
|
+
expect(object.og.image.url).to eq "http://cdn.instructables.com/FI2/D7XW/I2XTQWFE/FI2D7XWI2XTQWFE.RECTANGLE1.jpg"
|
110
|
+
expect(object.og.description).to eq "The OpenKnit machine is an open-source, low cost, digital fabrication tool developed by Gerard Rubio. The machine affords the user the opportunity to..."
|
111
|
+
end
|
112
|
+
|
113
|
+
it "does not parse in strict mode" do
|
114
|
+
OpenGraphReader.config.strict = true
|
115
|
+
|
116
|
+
expect {
|
117
|
+
OpenGraphReader.parse! fixture_html 'real_world/unknown_namespace'
|
118
|
+
}.to raise_error OpenGraphReader::InvalidObjectError, /is not a registered namespace/
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -27,9 +27,17 @@ RSpec.configure do |config|
|
|
27
27
|
|
28
28
|
config.order = :random
|
29
29
|
Kernel.srand config.seed
|
30
|
+
|
31
|
+
config.after(:each) do
|
32
|
+
OpenGraphReader.config.reset_to_defaults!
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
|
33
37
|
def example_html example
|
34
|
-
|
38
|
+
fixture_html "examples/#{example}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def fixture_html fixture
|
42
|
+
File.read File.expand_path("./fixtures/#{fixture}.html", __dir__)
|
35
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_graph_reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonne Haß
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/open_graph_reader.rb
|
109
109
|
- lib/open_graph_reader/base.rb
|
110
110
|
- lib/open_graph_reader/builder.rb
|
111
|
+
- lib/open_graph_reader/configuration.rb
|
111
112
|
- lib/open_graph_reader/definitions.rb
|
112
113
|
- lib/open_graph_reader/fetcher.rb
|
113
114
|
- lib/open_graph_reader/object.rb
|
@@ -164,11 +165,19 @@ files:
|
|
164
165
|
- spec/fixtures/examples/video-array.html
|
165
166
|
- spec/fixtures/examples/video-movie.html
|
166
167
|
- spec/fixtures/examples/video.html
|
168
|
+
- spec/fixtures/real_world/missing_image.html
|
169
|
+
- spec/fixtures/real_world/mixed_case_properties.html
|
170
|
+
- spec/fixtures/real_world/mixed_case_type.html
|
171
|
+
- spec/fixtures/real_world/not_a_reference.html
|
172
|
+
- spec/fixtures/real_world/undefined_property.html
|
173
|
+
- spec/fixtures/real_world/unknown_namespace.html
|
174
|
+
- spec/fixtures/real_world/unknown_type.html
|
167
175
|
- spec/integration/invalid_examples_spec.rb
|
176
|
+
- spec/integration/real_world_spec.rb
|
168
177
|
- spec/integration/valid_examples_spec.rb
|
169
178
|
- spec/open_graph_reader_spec.rb
|
170
179
|
- spec/spec_helper.rb
|
171
|
-
homepage: https://github.com/jhass/
|
180
|
+
homepage: https://github.com/jhass/open_graph_reader
|
172
181
|
licenses:
|
173
182
|
- MIT
|
174
183
|
metadata: {}
|
@@ -240,7 +249,15 @@ test_files:
|
|
240
249
|
- spec/fixtures/examples/video-array.html
|
241
250
|
- spec/fixtures/examples/video-movie.html
|
242
251
|
- spec/fixtures/examples/video.html
|
252
|
+
- spec/fixtures/real_world/missing_image.html
|
253
|
+
- spec/fixtures/real_world/mixed_case_properties.html
|
254
|
+
- spec/fixtures/real_world/mixed_case_type.html
|
255
|
+
- spec/fixtures/real_world/not_a_reference.html
|
256
|
+
- spec/fixtures/real_world/undefined_property.html
|
257
|
+
- spec/fixtures/real_world/unknown_namespace.html
|
258
|
+
- spec/fixtures/real_world/unknown_type.html
|
243
259
|
- spec/integration/invalid_examples_spec.rb
|
260
|
+
- spec/integration/real_world_spec.rb
|
244
261
|
- spec/integration/valid_examples_spec.rb
|
245
262
|
- spec/open_graph_reader_spec.rb
|
246
263
|
- spec/spec_helper.rb
|