onebox 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +7 -452
  4. data/Gemfile +1 -1
  5. data/README.md +23 -45
  6. data/lib/onebox.rb +21 -13
  7. data/lib/onebox/engine.rb +53 -43
  8. data/lib/onebox/engine/amazon_onebox.rb +34 -7
  9. data/lib/onebox/engine/classic_google_maps_onebox.rb +37 -0
  10. data/lib/onebox/engine/github_blob_onebox.rb +27 -7
  11. data/lib/onebox/engine/github_commit_onebox.rb +8 -15
  12. data/lib/onebox/engine/github_gist_onebox.rb +10 -10
  13. data/lib/onebox/engine/github_pullrequest_onebox.rb +7 -15
  14. data/lib/onebox/engine/google_play_app_onebox.rb +30 -0
  15. data/lib/onebox/engine/html.rb +10 -1
  16. data/lib/onebox/engine/image_onebox.rb +13 -0
  17. data/lib/onebox/engine/stack_exchange_onebox.rb +25 -11
  18. data/lib/onebox/engine/standard_embed.rb +48 -0
  19. data/lib/onebox/engine/twitter_status_onebox.rb +100 -0
  20. data/lib/onebox/engine/whitelisted_generic_onebox.rb +223 -0
  21. data/lib/onebox/engine/wikipedia_onebox.rb +27 -5
  22. data/lib/onebox/engine/youtube_onebox.rb +22 -0
  23. data/lib/onebox/helpers.rb +24 -0
  24. data/lib/onebox/layout.rb +67 -0
  25. data/lib/onebox/layout_support.rb +16 -0
  26. data/lib/onebox/matcher.rb +6 -1
  27. data/lib/onebox/preview.rb +19 -6
  28. data/lib/onebox/version.rb +1 -1
  29. data/lib/onebox/view.rb +7 -12
  30. data/onebox.gemspec +7 -5
  31. data/spec/fixtures/amazon.response +1336 -2971
  32. data/spec/fixtures/githubblob.response +49 -921
  33. data/spec/fixtures/{android.response → googleplayapp.response} +1 -1
  34. data/spec/fixtures/stackexchange.response +1 -1889
  35. data/spec/fixtures/twitterstatus.response +3006 -0
  36. data/spec/fixtures/youtube-json.response +1 -0
  37. data/spec/fixtures/youtube.response +1625 -0
  38. data/spec/lib/onebox/engine/amazon_onebox_spec.rb +5 -20
  39. data/spec/lib/onebox/engine/classic_google_maps_onebox_spec.rb +28 -0
  40. data/spec/lib/onebox/engine/github_blob_onebox_spec.rb +6 -15
  41. data/spec/lib/onebox/engine/github_commit_onebox_spec.rb +14 -22
  42. data/spec/lib/onebox/engine/github_gist_onebox_spec.rb +6 -14
  43. data/spec/lib/onebox/engine/github_pullrequest_onebox_spec.rb +14 -26
  44. data/spec/lib/onebox/engine/google_play_app_onebox_spec.rb +28 -0
  45. data/spec/lib/onebox/engine/html_spec.rb +4 -6
  46. data/spec/lib/onebox/engine/json_spec.rb +4 -6
  47. data/spec/lib/onebox/engine/stack_exchange_onebox_spec.rb +4 -17
  48. data/spec/lib/onebox/engine/twitter_status_onebox_spec.rb +103 -0
  49. data/spec/lib/onebox/engine/whitelisted_generic_onebox_spec.rb +51 -0
  50. data/spec/lib/onebox/engine/wikipedia_onebox_spec.rb +4 -17
  51. data/spec/lib/onebox/engine/youtube_onebox_spec.rb +17 -0
  52. data/spec/lib/onebox/engine_spec.rb +35 -32
  53. data/spec/lib/onebox/layout_spec.rb +77 -0
  54. data/spec/lib/onebox/matcher_spec.rb +0 -15
  55. data/spec/lib/onebox/preview_spec.rb +39 -9
  56. data/spec/lib/onebox_spec.rb +15 -11
  57. data/spec/spec_helper.rb +60 -4
  58. data/spec/support/html_spec_helper.rb +2 -1
  59. data/templates/_layout.mustache +11 -4
  60. data/templates/amazon.mustache +4 -3
  61. data/templates/githubblob.mustache +7 -5
  62. data/templates/githubcommit.mustache +20 -11
  63. data/templates/githubpullrequest.mustache +19 -12
  64. data/templates/googleplayapp.mustache +4 -0
  65. data/templates/stackexchange.mustache +19 -3
  66. data/templates/twitterstatus.mustache +12 -0
  67. data/templates/whitelistedgeneric.mustache +5 -0
  68. data/templates/wikipedia.mustache +4 -3
  69. metadata +57 -166
  70. data/lib/onebox/engine/bliptv_onebox.rb +0 -28
  71. data/lib/onebox/engine/clikthrough_onebox.rb +0 -25
  72. data/lib/onebox/engine/college_humor_onebox.rb +0 -27
  73. data/lib/onebox/engine/dailymotion_onebox.rb +0 -28
  74. data/lib/onebox/engine/dotsub_onebox.rb +0 -27
  75. data/lib/onebox/engine/example_onebox.rb +0 -24
  76. data/lib/onebox/engine/flickr_onebox.rb +0 -26
  77. data/lib/onebox/engine/funny_or_die_onebox.rb +0 -28
  78. data/lib/onebox/engine/hulu_onebox.rb +0 -29
  79. data/lib/onebox/engine/imgur_image_onebox.rb +0 -26
  80. data/lib/onebox/engine/itunes_onebox.rb +0 -28
  81. data/lib/onebox/engine/kinomap_onebox.rb +0 -27
  82. data/lib/onebox/engine/nfb_onebox.rb +0 -28
  83. data/lib/onebox/engine/open_graph.rb +0 -11
  84. data/lib/onebox/engine/qik_onebox.rb +0 -25
  85. data/lib/onebox/engine/revision3_onebox.rb +0 -27
  86. data/lib/onebox/engine/slideshare_onebox.rb +0 -27
  87. data/lib/onebox/engine/sound_cloud_onebox.rb +0 -27
  88. data/lib/onebox/engine/spotify_onebox.rb +0 -27
  89. data/lib/onebox/engine/ted_onebox.rb +0 -28
  90. data/lib/onebox/engine/twitter_onebox.rb +0 -31
  91. data/lib/onebox/engine/viddler_onebox.rb +0 -28
  92. data/lib/onebox/engine/vimeo_onebox.rb +0 -27
  93. data/lib/onebox/engine/yfrog_onebox.rb +0 -28
  94. data/spec/fixtures/bliptv.response +0 -724
  95. data/spec/fixtures/clikthrough.response +0 -1472
  96. data/spec/fixtures/collegehumor.response +0 -1272
  97. data/spec/fixtures/dailymotion.response +0 -575
  98. data/spec/fixtures/dotsub.response +0 -1257
  99. data/spec/fixtures/example.response +0 -50
  100. data/spec/fixtures/flickr.response +0 -1292
  101. data/spec/fixtures/funnyordie.response +0 -2010
  102. data/spec/fixtures/githubgist.response +0 -468
  103. data/spec/fixtures/hulu.response +0 -339
  104. data/spec/fixtures/imguralbum.response +0 -941
  105. data/spec/fixtures/imgurimage.response +0 -872
  106. data/spec/fixtures/itunes.response +0 -402
  107. data/spec/fixtures/kinomap.response +0 -299
  108. data/spec/fixtures/nfb.response +0 -810
  109. data/spec/fixtures/opengraph.response +0 -27
  110. data/spec/fixtures/qik.response +0 -371
  111. data/spec/fixtures/revision3.response +0 -985
  112. data/spec/fixtures/rottentomatoesfresh.response +0 -3275
  113. data/spec/fixtures/rottentomatoesincomplete.response +0 -2313
  114. data/spec/fixtures/rottentomatoesrotten.response +0 -3549
  115. data/spec/fixtures/slideshare.response +0 -1745
  116. data/spec/fixtures/soundcloud.response +0 -1409
  117. data/spec/fixtures/spotify.response +0 -250
  118. data/spec/fixtures/ted.response +0 -1341
  119. data/spec/fixtures/twitter.response +0 -1712
  120. data/spec/fixtures/viddler.response +0 -442
  121. data/spec/fixtures/vimeo.response +0 -571
  122. data/spec/fixtures/yfrog.response +0 -464
  123. data/spec/lib/onebox/engine/bliptv_onebox_spec.rb +0 -37
  124. data/spec/lib/onebox/engine/clikthrough_onebox_spec.rb +0 -29
  125. data/spec/lib/onebox/engine/college_humor_onebox_spec.rb +0 -37
  126. data/spec/lib/onebox/engine/dailymotion_onebox_spec.rb +0 -39
  127. data/spec/lib/onebox/engine/dotsub_onebox_spec.rb +0 -39
  128. data/spec/lib/onebox/engine/example_onebox_spec.rb +0 -21
  129. data/spec/lib/onebox/engine/flickr_onebox_spec.rb +0 -33
  130. data/spec/lib/onebox/engine/funny_or_die_onebox_spec.rb +0 -37
  131. data/spec/lib/onebox/engine/hulu_onebox_spec.rb +0 -37
  132. data/spec/lib/onebox/engine/imgur_image_onebox_spec.rb +0 -29
  133. data/spec/lib/onebox/engine/itunes_onebox_spec.rb +0 -29
  134. data/spec/lib/onebox/engine/kinomap_onebox_spec.rb +0 -37
  135. data/spec/lib/onebox/engine/nfb_onebox_spec.rb +0 -38
  136. data/spec/lib/onebox/engine/open_graph_spec.rb +0 -27
  137. data/spec/lib/onebox/engine/qik_onebox_spec.rb +0 -38
  138. data/spec/lib/onebox/engine/revision3_onebox_spec.rb +0 -39
  139. data/spec/lib/onebox/engine/slideshare_onebox_spec.rb +0 -33
  140. data/spec/lib/onebox/engine/sound_cloud_onebox_spec.rb +0 -39
  141. data/spec/lib/onebox/engine/spotify_onebox_spec.rb +0 -36
  142. data/spec/lib/onebox/engine/ted_onebox_spec.rb +0 -33
  143. data/spec/lib/onebox/engine/twitter_onebox_spec.rb +0 -47
  144. data/spec/lib/onebox/engine/viddler_onebox_spec.rb +0 -39
  145. data/spec/lib/onebox/engine/vimeo_onebox_spec.rb +0 -37
  146. data/spec/lib/onebox/engine/yfrog_onebox_spec.rb +0 -33
  147. data/spec/lib/onebox/view_spec.rb +0 -16
  148. data/templates/bliptv.mustache +0 -5
  149. data/templates/clikthrough.mustache +0 -4
  150. data/templates/collegehumor.mustache +0 -5
  151. data/templates/dailymotion.mustache +0 -5
  152. data/templates/dotsub.mustache +0 -5
  153. data/templates/example.mustache +0 -2
  154. data/templates/flickr.mustache +0 -4
  155. data/templates/funnyordie.mustache +0 -6
  156. data/templates/githubgist.mustache +0 -3
  157. data/templates/hulu.mustache +0 -6
  158. data/templates/imgurimage.mustache +0 -3
  159. data/templates/itunes.mustache +0 -4
  160. data/templates/kinomap.mustache +0 -5
  161. data/templates/nfb.mustache +0 -5
  162. data/templates/qik.mustache +0 -4
  163. data/templates/revision3.mustache +0 -5
  164. data/templates/slideshare.mustache +0 -4
  165. data/templates/soundcloud.mustache +0 -5
  166. data/templates/spotify.mustache +0 -4
  167. data/templates/ted.mustache +0 -5
  168. data/templates/twitter.mustache +0 -7
  169. data/templates/viddler.mustache +0 -5
  170. data/templates/vimeo.mustache +0 -5
  171. data/templates/yfrog.mustache +0 -4
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::WhitelistedGenericOnebox do
4
+
5
+ describe ".===" do
6
+ before do
7
+ described_class.whitelist = %w(eviltrout.com discourse.org)
8
+ end
9
+
10
+ it "matches an entire domain" do
11
+ expect(described_class === URI('http://eviltrout.com/resource')).to eq(true)
12
+ end
13
+
14
+ it "matches a subdomain" do
15
+ expect(described_class === URI('http://www.eviltrout.com/resource')).to eq(true)
16
+ end
17
+
18
+ it "doesn't match a different domain" do
19
+ expect(described_class === URI('http://goodtuna.com/resource')).to eq(false)
20
+ end
21
+
22
+ it "doesn't match the period as any character" do
23
+ expect(described_class === URI('http://eviltrouticom/resource')).to eq(false)
24
+ end
25
+
26
+ it "doesn't match a prefixed domain" do
27
+ expect(described_class === URI('http://aneviltrout.com/resource')).to eq(false)
28
+ end
29
+ end
30
+
31
+
32
+ describe 'rewrites' do
33
+ class DummyOnebox < Onebox::Engine::WhitelistedGenericOnebox
34
+ def generic_html
35
+ "<iframe src='https://youtube.com/asdf'></iframe>"
36
+ end
37
+ end
38
+
39
+ it "doesn't rewrite URLs that arent in the list" do
40
+ Onebox::Engine::WhitelistedGenericOnebox.rewrites = []
41
+ DummyOnebox.new("http://youtube.com").to_html.should == "<iframe src='https://youtube.com/asdf'></iframe>"
42
+ end
43
+
44
+ it "rewrites URLs when whitelisted" do
45
+ Onebox::Engine::WhitelistedGenericOnebox.rewrites = %w(youtube.com)
46
+ DummyOnebox.new("http://youtube.com").to_html.should == "<iframe src='//youtube.com/asdf'></iframe>"
47
+ end
48
+ end
49
+
50
+
51
+ end
@@ -3,31 +3,18 @@ require "spec_helper"
3
3
  describe Onebox::Engine::WikipediaOnebox do
4
4
  before(:all) do
5
5
  @link = "http://en.wikipedia.org/wiki/Kevin_Bacon"
6
- fake(@link, response(described_class.template_name))
7
6
  end
8
- before(:each) { Onebox.defaults.cache.clear }
9
-
10
- let(:link) { @link }
11
7
 
8
+ include_context "engines"
12
9
  it_behaves_like "an engine"
13
10
 
14
11
  describe "#to_html" do
15
- let(:html) { described_class.new(link).to_html }
16
-
17
- it "returns the product URL" do
18
- expect(html).to include(link)
19
- end
20
-
21
- it "returns the article title" do
22
- expect(html).to include("Kevin Bacon")
23
- end
24
-
25
- it "returns the article img src" do
12
+ it "includes article image" do
26
13
  expect(html).to include("225px-Kevin_Bacon_Comic-Con_2012.jpg")
27
14
  end
28
15
 
29
- it "returns the article summary" do
30
- expect(html).to include("Kevin Norwood Bacon[1] (born July 8, 1958)")
16
+ it "includes summary" do
17
+ expect(html).to include("Kevin Norwood Bacon (born July 8, 1958)")
31
18
  end
32
19
  end
33
20
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Onebox::Engine::YoutubeOnebox do
4
+ before do
5
+ fake("https://www.youtube.com/watch?v=21Lk4YiASMo", response("youtube"))
6
+ fake("http://www.youtube.com/oembed?format=json&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D21Lk4YiASMo", response("youtube-json"))
7
+ end
8
+
9
+ it "should add wmode=opaque" do
10
+ Onebox.preview('https://www.youtube.com/watch?v=21Lk4YiASMo').to_s.should match(/wmode=opaque/)
11
+ end
12
+
13
+ it "should rewrite URLs to be agnostic" do
14
+ Onebox.preview('https://www.youtube.com/watch?v=21Lk4YiASMo').to_s.should match(/"\/\//)
15
+ end
16
+ end
17
+
@@ -1,31 +1,26 @@
1
1
  require "spec_helper"
2
2
 
3
- class OneboxEngineExample
4
- include Onebox::Engine
3
+ describe Onebox::Engine do
4
+ class OneboxEngineExample
5
+ include Onebox::Engine
5
6
 
6
- def data
7
- { foo: raw[:key], url: @url }
8
- end
7
+ def to_html
8
+ "Hello #{link}"
9
+ end
9
10
 
10
- def raw
11
- { key: "value" }
12
- end
11
+ def data
12
+ { foo: raw[:key], url: @url }
13
+ end
13
14
 
14
- def view
15
- @view.tap do |layout|
16
- layout.view.template = %|<div class="onebox"><a href="{{url}}"></a></div>|
15
+ def raw
16
+ { key: "value" }
17
17
  end
18
18
  end
19
- end
20
19
 
21
- describe Onebox::Engine do
22
- describe "#to_html" do
23
- it "returns the onebox wrapper" do
24
- html = OneboxEngineExample.new("foo").to_html
25
- expect(html).to include(%|class="onebox"|)
26
- end
20
+ describe "#link" do
21
+ before { Onebox::View.any_instance.stub(:template) { %|this shold be a template| } }
27
22
 
28
- it "doesn't allow XSS injection" do
23
+ it "escapes `link`" do
29
24
  html = OneboxEngineExample.new(%|http://foo.com" onscript="alert('foo')|).to_html
30
25
  expect(html).not_to include(%|onscript="alert('foo')|)
31
26
  end
@@ -53,13 +48,20 @@ describe Onebox::Engine do
53
48
  end
54
49
  end
55
50
 
51
+ describe '.placeholder_html' do
52
+ let(:onebox) { OneboxEngineExample.new('http://eviltrout.com') }
53
+ it "returns `to_html` by default" do
54
+ expect(onebox.to_html).to eq(onebox.placeholder_html)
55
+ end
56
+ end
57
+
56
58
  describe ".===" do
57
59
  class OneboxEngineTripleEqual
58
60
  include Onebox::Engine
59
61
  @@matcher = /example/
60
62
  end
61
63
  it "returns true if argument matches the matcher" do
62
- result = OneboxEngineTripleEqual === "http://www.example.com/product/5?var=foo&bar=5"
64
+ result = OneboxEngineTripleEqual === URI("http://www.example.com/product/5?var=foo&bar=5")
63
65
  expect(result).to eq(true)
64
66
  end
65
67
  end
@@ -79,21 +81,22 @@ describe Onebox::Engine do
79
81
  end
80
82
  end
81
83
 
82
- describe ".template_name" do
83
- module ScopeForTemplateName
84
- class TemplateNameOnebox
85
- include Onebox::Engine
86
- end
84
+ end
85
+
86
+ describe ".onebox_name" do
87
+ module ScopeForTemplateName
88
+ class TemplateNameOnebox
89
+ include Onebox::Engine
87
90
  end
91
+ end
88
92
 
89
- let(:template_name) { ScopeForTemplateName::TemplateNameOnebox.template_name }
93
+ let(:onebox_name) { ScopeForTemplateName::TemplateNameOnebox.onebox_name }
90
94
 
91
- it "should not include the scope" do
92
- expect(template_name).not_to include("ScopeForTemplateName", "scopefortemplatename")
93
- end
95
+ it "should not include the scope" do
96
+ expect(onebox_name).not_to include("ScopeForTemplateName", "scopefortemplatename")
97
+ end
94
98
 
95
- it "should not include the word Onebox" do
96
- expect(template_name).not_to include("onebox", "Onebox")
97
- end
99
+ it "should not include the word Onebox" do
100
+ expect(onebox_name).not_to include("onebox", "Onebox")
98
101
  end
99
102
  end
@@ -0,0 +1,77 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Layout do
4
+ let(:cache) { Moneta.new(:Memory, expires: true, serializer: :json) }
5
+ let(:record) { {} }
6
+ let(:onebox) { described_class.new("amazon", record, cache) }
7
+ let(:html) { onebox.to_html }
8
+
9
+ describe ".template_path" do
10
+ let(:template_path) { onebox.template_path }
11
+
12
+ before(:each) do
13
+ Onebox.options.load_paths << "directory_a"
14
+ Onebox.options.load_paths << "directory_b"
15
+ end
16
+
17
+ context "when template exists in directory_b" do
18
+ before(:each) do
19
+ allow_any_instance_of(described_class).to receive(:template?) do |path|
20
+ path == "directory_b"
21
+ end
22
+ end
23
+
24
+ it "returns directory_b" do
25
+ expect(template_path).to eq("directory_b")
26
+ end
27
+ end
28
+
29
+ context "when template exists in directory_a" do
30
+ before(:each) do
31
+ allow_any_instance_of(described_class).to receive(:template?) do |path|
32
+ path == "directory_a"
33
+ end
34
+ end
35
+
36
+ it "returns directory_a" do
37
+ expect(template_path).to eq("directory_a")
38
+ end
39
+ end
40
+
41
+ context "when template doesn't exist in directory_a or directory_b" do
42
+ it "returns default path" do
43
+ expect(template_path).to include("template")
44
+ end
45
+ end
46
+
47
+ after(:each) do
48
+ Onebox.options.load_paths.pop(2)
49
+ end
50
+ end
51
+
52
+ describe "#to_html" do
53
+ class OneboxEngineLayout
54
+ include Onebox::Engine
55
+
56
+ def data
57
+ "new content"
58
+ end
59
+ end
60
+
61
+ it "reads from cache if rendered template is cached" do
62
+ described_class.new("amazon", record, cache).to_html
63
+ expect(cache).to receive(:fetch)
64
+ described_class.new("amazon", record, cache).to_html
65
+ end
66
+
67
+ it "contains layout template" do
68
+ expect(html).to include(%|class="onebox|)
69
+ end
70
+
71
+ it "contains the view" do
72
+ record = { link: "foo" }
73
+ html = described_class.new("amazon", record, cache).to_html
74
+ expect(html).to include(%|"foo"|)
75
+ end
76
+ end
77
+ end
@@ -1,20 +1,5 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Onebox::Matcher do
4
- describe "#oneboxed" do
5
- it "returns Example onebox when given example url" do
6
- matcher = described_class.new("http://example.com")
7
- expect(matcher.oneboxed).to be(Onebox::Engine::ExampleOnebox)
8
- end
9
4
 
10
- it "returns Amazon onebox when given amazon url" do
11
- matcher = described_class.new("http://amazon.com")
12
- expect(matcher.oneboxed).to be(Onebox::Engine::AmazonOnebox)
13
- end
14
-
15
- it "returns Flickr onebox when given flickr url" do
16
- matcher = described_class.new("http://flickr.com")
17
- expect(matcher.oneboxed).to be(Onebox::Engine::FlickrOnebox)
18
- end
19
- end
20
5
  end
@@ -1,23 +1,53 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Onebox::Preview do
4
- before(:each) { Onebox.defaults.cache.clear }
4
+
5
+ before do
6
+ fake("http://www.amazon.com", response("amazon"))
7
+ FakeWeb.register_uri(:get, "http://www.amazon.com/404-url", status: 404)
8
+ FakeWeb.register_uri(:get, "http://www.amazon.com/500-url", status: 500)
9
+ FakeWeb.register_uri(:get, "http://www.amazon.com/error-url", status: 500)
10
+ FakeWeb.register_uri(:get, "http://www.amazon.com/timeout-url", exception: Timeout::Error)
11
+ FakeWeb.register_uri(:get, "http://www.amazon.com/http-error", exception: Net::HTTPError)
12
+ FakeWeb.register_uri(:get, "http://www.amazon.com/error-connecting", exception: Errno::ECONNREFUSED)
13
+ end
14
+
15
+ let(:preview) { described_class.new("http://www.amazon.com") }
5
16
 
6
17
  describe "#to_s" do
7
18
  it "returns some html if given a valid url" do
8
- fake("http://www.example.com", response("example"))
9
- preview = described_class.new("http://www.example.com")
10
- expect(preview.to_s).to include("Example Domain 1")
19
+ title = "Knit Noro: Accessories"
20
+ expect(preview.to_s).to include(title)
21
+ end
22
+
23
+ it "returns an empty string if the resource is missing" do
24
+ expect(described_class.new("http://www.amazon.com/404-url").to_s).to eq("")
25
+ end
26
+
27
+ it "returns an empty string if the resource returns an error" do
28
+ expect(described_class.new("http://www.amazon.com/500-url").to_s).to eq("")
29
+ end
30
+
31
+ it "returns an empty string if the resource times out" do
32
+ expect(described_class.new("http://www.amazon.com/timeout-url").to_s).to eq("")
33
+ end
34
+
35
+ it "returns an empty string if there is an http error" do
36
+ expect(described_class.new("http://www.amazon.com/http-error").to_s).to eq("")
37
+ end
38
+
39
+ it "returns an empty string if there is an error connecting" do
40
+ expect(described_class.new("http://www.amazon.com/error-connecting").to_s).to eq("")
41
+ end
42
+
43
+ it "returns an empty string if the url is not valid" do
44
+ expect(described_class.new('not a url').to_s).to eq("")
11
45
  end
12
- it "returns an empty string if the resource is not found"
13
- it "returns an empty string if the resource fails to load"
14
- it "returns an empty string if the url is not valid"
15
46
  end
16
47
 
17
48
  describe "#engine" do
18
49
  it "returns an engine" do
19
- preview = described_class.new("http://www.example.com")
20
- expect(preview.send(:engine)).to be_a(Onebox::Engine)
50
+ expect(preview.send(:engine)).to be_an(Onebox::Engine)
21
51
  end
22
52
  end
23
53
  end
@@ -2,15 +2,14 @@ require "spec_helper"
2
2
 
3
3
  describe Onebox do
4
4
  describe ".preview" do
5
+ let(:url) { "http://www.amazon.com" }
5
6
  it "creates a cache that responds as expected" do
6
- url = "http://www.example.com"
7
7
  preview = Onebox.preview(url)
8
8
  cache = preview.cache
9
9
  expect(cache).to respond_to(:key?, :store, :fetch)
10
10
  end
11
11
 
12
12
  it "stores the value in cache if it doesn't exist" do
13
- url = "http://www.example.com"
14
13
  preview = Onebox.preview(url)
15
14
  preview.to_s
16
15
  cache = preview.cache
@@ -18,15 +17,15 @@ describe Onebox do
18
17
  end
19
18
 
20
19
  it "replaces the cache if the cache is expired" do
21
- url = "http://www.example.com"
22
- preview = Onebox.preview(url, cache: Moneta.new(:Memory, expires: 100000, serializer: :json))
20
+ preview = Onebox.preview(url, cache: Moneta.new(:Memory, expires: 100_000, serializer: :json))
23
21
  cache = preview.cache
24
22
  expect(cache.fetch(url)).to be(nil)
25
23
  end
26
24
  end
27
25
 
28
26
  describe "templates" do
29
- let(:templates) { Dir["templates/*.mustache"] - ["templates/_layout.mustache"] }
27
+ let(:ignored) { ["templates/_layout.mustache", "templates/githubgist.mustache"] }
28
+ let(:templates) { Dir["templates/*.mustache"] - ignored }
30
29
 
31
30
  def expect_templates_to_not_match(text)
32
31
  templates.each do |template|
@@ -34,16 +33,21 @@ describe Onebox do
34
33
  end
35
34
  end
36
35
 
37
- it "should not contain any triple braces" do
38
- expect_templates_to_not_match(/\{\{\{/)
39
- end
40
-
41
36
  it "should not contain any script tags" do
42
37
  expect_templates_to_not_match(/<script/)
43
38
  end
39
+ end
40
+
41
+ describe 'has_matcher?' do
42
+ before do
43
+ Onebox::Engine::WhitelistedGenericOnebox.whitelist = %w(youtube.com)
44
+ end
45
+ it "has no matcher for a made up url" do
46
+ expect(Onebox.has_matcher?("http://wow.com/omg/doge")).to be_false
47
+ end
44
48
 
45
- it "should not contain any on*" do
46
- expect_templates_to_not_match(/\s*on.+\s*=/)
49
+ it "has a matcher for a real site" do
50
+ expect(Onebox.has_matcher?("http://www.youtube.com/watch?v=azaIE6QSMUs")).to be_true
47
51
  end
48
52
  end
49
53
  end
data/spec/spec_helper.rb CHANGED
@@ -12,20 +12,76 @@ require 'mocha/api'
12
12
  require_relative "support/html_spec_helper"
13
13
 
14
14
  RSpec.configure do |config|
15
+ config.before(:all) do
16
+ FakeWeb.allow_net_connect = false
17
+ end
15
18
  config.include HTMLSpecHelper
16
19
  end
17
20
 
21
+ shared_context "engines" do
22
+ before(:each) do
23
+ fake(@uri || @link, response(described_class.onebox_name))
24
+ @onebox = described_class.new(@link)
25
+ @html = @onebox.to_html
26
+ @data = Onebox::Helpers.symbolize_keys(@onebox.send(:data))
27
+ end
28
+ before(:each) { Onebox.options.cache.clear }
29
+
30
+ let(:onebox) { @onebox }
31
+ let(:html) { @html }
32
+ let(:data) { @data }
33
+ let(:link) { @link }
34
+
35
+ def escaped_data(key)
36
+ CGI.escapeHTML(data[key])
37
+ end
38
+ end
39
+
18
40
  shared_examples_for "an engine" do
19
- it "has engine behavior" do
20
- expect(described_class.private_instance_methods).to include(:data, :record, :raw)
41
+ it "responds to data" do
42
+ expect(described_class.private_instance_methods).to include(:data)
21
43
  end
22
44
 
23
- it "has implemented the data functionality" do
24
- expect { described_class.new(link).send(:data) }.not_to raise_error
45
+ it "responds to record" do
46
+ expect(described_class.private_instance_methods).to include(:record)
25
47
  end
26
48
 
27
49
  it "correctly matches the url" do
28
50
  onebox = Onebox::Matcher.new(link).oneboxed
29
51
  expect(onebox).to be(described_class)
30
52
  end
53
+
54
+ describe "#data" do
55
+ it "includes title" do
56
+ expect(data[:title]).not_to be_nil
57
+ end
58
+
59
+ it "includes link" do
60
+ expect(data[:link]).not_to be_nil
61
+ end
62
+ end
63
+ end
64
+
65
+ shared_examples_for "a layout engine" do
66
+ describe "#to_html" do
67
+ it "includes subname" do
68
+ expect(html).to include(%|<aside class="onebox #{described_class.onebox_name}">|)
69
+ end
70
+
71
+ it "includes title" do
72
+ expect(html).to include(escaped_data(:title))
73
+ end
74
+
75
+ it "includes link" do
76
+ expect(html).to include(%|class="link" href="#{data[:link]}|)
77
+ end
78
+
79
+ it "includes badge" do
80
+ expect(html).to include(%|<strong class="name">#{data[:badge]}</strong>|)
81
+ end
82
+
83
+ it "includes domain" do
84
+ expect(html).to include(%|class="domain" href="#{escaped_data(:domain)}|)
85
+ end
86
+ end
31
87
  end