onebox 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +476 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +3 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +92 -0
  11. data/Rakefile +22 -0
  12. data/lib/onebox.rb +33 -0
  13. data/lib/onebox/engine.rb +93 -0
  14. data/lib/onebox/engine/amazon_onebox.rb +25 -0
  15. data/lib/onebox/engine/bliptv_onebox.rb +26 -0
  16. data/lib/onebox/engine/clikthrough_onebox.rb +23 -0
  17. data/lib/onebox/engine/college_humor_onebox.rb +26 -0
  18. data/lib/onebox/engine/dailymotion_onebox.rb +26 -0
  19. data/lib/onebox/engine/dotsub_onebox.rb +25 -0
  20. data/lib/onebox/engine/example_onebox.rb +25 -0
  21. data/lib/onebox/engine/flickr_onebox.rb +25 -0
  22. data/lib/onebox/engine/funny_or_die_onebox.rb +26 -0
  23. data/lib/onebox/engine/html.rb +11 -0
  24. data/lib/onebox/engine/hulu_onebox.rb +26 -0
  25. data/lib/onebox/engine/nfb_onebox.rb +25 -0
  26. data/lib/onebox/engine/open_graph.rb +11 -0
  27. data/lib/onebox/engine/qik_onebox.rb +23 -0
  28. data/lib/onebox/engine/revision3_onebox.rb +26 -0
  29. data/lib/onebox/engine/slideshare_onebox.rb +25 -0
  30. data/lib/onebox/engine/sound_cloud_onebox.rb +25 -0
  31. data/lib/onebox/engine/stack_exchange_onebox.rb +23 -0
  32. data/lib/onebox/engine/ted_onebox.rb +25 -0
  33. data/lib/onebox/engine/viddler_onebox.rb +26 -0
  34. data/lib/onebox/engine/vimeo_onebox.rb +26 -0
  35. data/lib/onebox/engine/wikipedia_onebox.rb +24 -0
  36. data/lib/onebox/engine/yfrog_onebox.rb +25 -0
  37. data/lib/onebox/matcher.rb +13 -0
  38. data/lib/onebox/preview.rb +31 -0
  39. data/lib/onebox/version.rb +3 -0
  40. data/onebox.gemspec +36 -0
  41. data/spec/fixtures/amazon.response +3098 -0
  42. data/spec/fixtures/android.response +138 -0
  43. data/spec/fixtures/apple.response +391 -0
  44. data/spec/fixtures/bliptv.response +724 -0
  45. data/spec/fixtures/clickthrough.response +1472 -0
  46. data/spec/fixtures/clikthrough.response +1472 -0
  47. data/spec/fixtures/collegehumor.response +1272 -0
  48. data/spec/fixtures/dailymotion.response +575 -0
  49. data/spec/fixtures/dotsub.response +1257 -0
  50. data/spec/fixtures/example.response +50 -0
  51. data/spec/fixtures/flickr.response +1292 -0
  52. data/spec/fixtures/funnyordie.response +2010 -0
  53. data/spec/fixtures/gist.response +282 -0
  54. data/spec/fixtures/github_blob.response +706 -0
  55. data/spec/fixtures/github_commit.response +881 -0
  56. data/spec/fixtures/github_pullrequest.response +1619 -0
  57. data/spec/fixtures/hulu.response +339 -0
  58. data/spec/fixtures/image.response +0 -0
  59. data/spec/fixtures/imgur.response +892 -0
  60. data/spec/fixtures/kinomap.response +299 -0
  61. data/spec/fixtures/nfb.response +810 -0
  62. data/spec/fixtures/opengraph.response +27 -0
  63. data/spec/fixtures/qik.response +371 -0
  64. data/spec/fixtures/revision3.response +985 -0
  65. data/spec/fixtures/rottentomatoes_fresh.response +3275 -0
  66. data/spec/fixtures/rottentomatoes_incomplete.response +2313 -0
  67. data/spec/fixtures/rottentomatoes_rotten.response +3549 -0
  68. data/spec/fixtures/slideshare.response +1745 -0
  69. data/spec/fixtures/soundcloud.response +1409 -0
  70. data/spec/fixtures/stackexchange.response +1889 -0
  71. data/spec/fixtures/ted.response +1341 -0
  72. data/spec/fixtures/twitter.response +1712 -0
  73. data/spec/fixtures/viddler.response +442 -0
  74. data/spec/fixtures/video.response +0 -0
  75. data/spec/fixtures/vimeo.response +571 -0
  76. data/spec/fixtures/wikipedia.response +1236 -0
  77. data/spec/fixtures/wikipedia_redirected.response +899 -0
  78. data/spec/fixtures/yfrog.response +464 -0
  79. data/spec/lib/onebox/engine/amazon_spec.rb +34 -0
  80. data/spec/lib/onebox/engine/bliptv_spec.rb +34 -0
  81. data/spec/lib/onebox/engine/clikthrough_spec.rb +26 -0
  82. data/spec/lib/onebox/engine/college_humor_spec.rb +34 -0
  83. data/spec/lib/onebox/engine/dailymotion_spec.rb +36 -0
  84. data/spec/lib/onebox/engine/dotsub_spec.rb +36 -0
  85. data/spec/lib/onebox/engine/example_spec.rb +18 -0
  86. data/spec/lib/onebox/engine/flickr_spec.rb +30 -0
  87. data/spec/lib/onebox/engine/funny_or_die_spec.rb +34 -0
  88. data/spec/lib/onebox/engine/hulu_spec.rb +34 -0
  89. data/spec/lib/onebox/engine/nfb_spec.rb +35 -0
  90. data/spec/lib/onebox/engine/qik_spec.rb +35 -0
  91. data/spec/lib/onebox/engine/revision3_spec.rb +36 -0
  92. data/spec/lib/onebox/engine/slideshare_spec.rb +30 -0
  93. data/spec/lib/onebox/engine/sound_cloud_spec.rb +36 -0
  94. data/spec/lib/onebox/engine/stack_exchange_spec.rb +27 -0
  95. data/spec/lib/onebox/engine/ted_spec.rb +30 -0
  96. data/spec/lib/onebox/engine/viddler_spec.rb +36 -0
  97. data/spec/lib/onebox/engine/vimeo_spec.rb +34 -0
  98. data/spec/lib/onebox/engine/wikipedia_spec.rb +30 -0
  99. data/spec/lib/onebox/engine/yfrog_spec.rb +30 -0
  100. data/spec/lib/onebox/engine_spec.rb +78 -0
  101. data/spec/lib/onebox/matcher_spec.rb +20 -0
  102. data/spec/lib/onebox/preview_spec.rb +21 -0
  103. data/spec/lib/onebox_spec.rb +49 -0
  104. data/spec/spec_helper.rb +26 -0
  105. data/spec/support/html_spec_helper.rb +17 -0
  106. data/templates/amazon.handlebars +9 -0
  107. data/templates/bliptv.handlebars +10 -0
  108. data/templates/clickthrough.handlebars +8 -0
  109. data/templates/clikthrough.handlebars +8 -0
  110. data/templates/collegehumor.handlebars +9 -0
  111. data/templates/dailymotion.handlebars +9 -0
  112. data/templates/dotsub.handlebars +9 -0
  113. data/templates/flickr.handlebars +8 -0
  114. data/templates/funnyordie.handlebars +9 -0
  115. data/templates/hulu.handlebars +9 -0
  116. data/templates/nfb.handlebars +8 -0
  117. data/templates/qik.handlebars +7 -0
  118. data/templates/revision3.handlebars +9 -0
  119. data/templates/slideshare.handlebars +8 -0
  120. data/templates/soundcloud.handlebars +9 -0
  121. data/templates/stackexchange.handlebars +7 -0
  122. data/templates/ted.handlebars +8 -0
  123. data/templates/viddler.handlebars +9 -0
  124. data/templates/vimeo.handlebars +9 -0
  125. data/templates/wikipedia.handlebars +8 -0
  126. data/templates/yfrog.handlebars +8 -0
  127. metadata +447 -0
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::SoundCloudOnebox do
4
+ let(:link) { "http://soundcloud.com" }
5
+ before do
6
+ fake(link, response("soundcloud.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns video title" do
15
+ expect(html).to include("Penguin Prison - Worse It Gets (RAC Mix)")
16
+ end
17
+
18
+ it "returns video image" do
19
+ expect(html).to include("artworks-000033643332-vpuznu-t500x500.jpg")
20
+ end
21
+
22
+ it "returns video description" do
23
+ expect(html).to include("Remix by André Allen Anjos.")
24
+ end
25
+
26
+ it "returns video URL" do
27
+ expect(html).to include("Remix by André Allen Anjos.")
28
+ end
29
+
30
+ it "returns video embed code"
31
+
32
+ it "returns URL" do
33
+ expect(html).to include(link)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::StackExchangeOnebox do
4
+ let(:link) { "http://stackexchange.com" }
5
+ before do
6
+ fake(link, response("stackexchange.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns the question title" do
15
+ expect(html).to include("Concept behind these 4 lines of tricky C++ code")
16
+ end
17
+
18
+ it "returns the question" do
19
+ expect(html).to include("Why does this code gives output C++Sucks? Can anyone explain the concept behind it?")
20
+ end
21
+
22
+ it "returns the question URL" do
23
+ expect(html).to include(link)
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::TedOnebox do
4
+ let(:link) { "http://ted.com" }
5
+ before do
6
+ fake(link, response("ted.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns video title" do
15
+ expect(html).to include("Eli Beer: The fastest ambulance? A motorcycle")
16
+ end
17
+
18
+ it "returns video photo" do
19
+ expect(html).to include("aa8d0403aec3466d031e3e1c1605637d84d6a07d_389x292.jpg")
20
+ end
21
+
22
+ it "returns video description" do
23
+ expect(html).to include("As a young EMT on a Jerusalem ambulance")
24
+ end
25
+
26
+ it "returns URL" do
27
+ expect(html).to include(link)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::ViddlerOnebox do
4
+ let(:link) { "http://viddler.com" }
5
+ before do
6
+ fake(link, response("viddler.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns video title" do
15
+ expect(html).to include("Viddler Demo")
16
+ end
17
+
18
+ it "returns video image" do
19
+ expect(html).to include("thumbnail_2_7164f749_v2.jpg")
20
+ end
21
+
22
+ it "returns video description" do
23
+ expect(html).to include("Get familiar with your Viddler account.")
24
+ end
25
+
26
+ it "returns video URL" do
27
+ expect(html).to include("http://www.viddler.com/player/7164f749")
28
+ end
29
+
30
+ it "returns video embed code"
31
+
32
+ it "returns URL" do
33
+ expect(html).to include(link)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::VimeoOnebox do
4
+ let(:link) { "http://vimeo.com" }
5
+ before do
6
+ fake(link, response("vimeo.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns video title" do
15
+ expect(html).to include("108 years of Herman Miller in 108 seconds")
16
+ end
17
+
18
+ it "returns video photo" do
19
+ expect(html).to include("443673159_1280.jpg")
20
+ end
21
+
22
+ it "returns video description" do
23
+ expect(html).to include("To mark the launch of a new website for Hermann Miller furniture")
24
+ end
25
+
26
+ it "returns video URL" do
27
+ expect(html).to include("http://vimeo.com/moogaloop.swf?clip_id=70437049")
28
+ end
29
+
30
+ it "returns URL" do
31
+ expect(html).to include(link)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::WikipediaOnebox do
4
+ let(:link) { "http://wikipedia.com" }
5
+ before do
6
+ fake(link, response("wikipedia.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns the product URL" do
15
+ expect(html).to include(link)
16
+ end
17
+
18
+ it "returns the article title" do
19
+ expect(html).to include("Kevin Bacon")
20
+ end
21
+
22
+ it "returns the article img src" do
23
+ expect(html).to include("225px-Kevin_Bacon_Comic-Con_2012.jpg")
24
+ end
25
+
26
+ it "returns the article summary" do
27
+ expect(html).to include("Kevin Norwood Bacon[1] (born July 8, 1958)")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Engine::YfrogOnebox do
4
+ let(:link) { "http://yfrog.com" }
5
+ before do
6
+ fake(link, response("yfrog.response"))
7
+ end
8
+
9
+ it_behaves_like "engines"
10
+
11
+ describe "#to_html" do
12
+ let(:html) { described_class.new(link).to_html }
13
+
14
+ it "returns photo title" do
15
+ expect(html).to include("Celebrating @questlove Cook4Quest w/WyattCenac")
16
+ end
17
+
18
+ it "returns photo" do
19
+ expect(html).to include("jjdob.jpg")
20
+ end
21
+
22
+ it "returns photo description" do
23
+ expect(html).to include("Click on the photo to comment, share or view other great photos")
24
+ end
25
+
26
+ it "returns URL" do
27
+ expect(html).to include(link)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ class OneboxEngineExample
4
+ include Onebox::Engine
5
+
6
+ def data
7
+ { foo: raw[:key], url: @url }
8
+ end
9
+
10
+ def raw
11
+ { key: "value" }
12
+ end
13
+
14
+ def template
15
+ %|<div class="onebox"><a href="{{url}}"></a></div>|
16
+ end
17
+ end
18
+
19
+ describe Onebox::Engine do
20
+ describe "#to_html" do
21
+ it "returns the onebox wrapper" do
22
+ html = OneboxEngineExample.new("foo").to_html
23
+ expect(html).to include(%|class="onebox"|)
24
+ end
25
+
26
+ it "doesn't allow XSS injection" do
27
+ html = OneboxEngineExample.new(%|http://foo.com" onscript="alert('foo')|).to_html
28
+ expect(html).not_to include(%|onscript="alert('foo')|)
29
+ end
30
+ end
31
+
32
+ describe "#record" do
33
+ class OneboxEngineBar
34
+ include Onebox::Engine
35
+
36
+ def data
37
+ "new content"
38
+ end
39
+ end
40
+
41
+ it "returns cached value for given url if its url is already in cache" do
42
+ cache = { "http://example.com" => "old content" }
43
+ result = OneboxEngineBar.new("http://example.com", cache).send(:record)
44
+ expect(result).to eq("old content")
45
+ end
46
+
47
+ it "stores cache value for given url if cache key doesn't exist" do
48
+ cache = { "http://example.com1" => "old content" }
49
+ result = OneboxEngineBar.new("http://example.com", cache).send(:record)
50
+ expect(result).to eq("new content")
51
+ end
52
+ end
53
+
54
+ describe ".===" do
55
+ it "returns true if argument matches the matcher" do
56
+ class OneboxEngineFoo
57
+ include Onebox::Engine
58
+ @@matcher = /example/
59
+ end
60
+ result = OneboxEngineFoo === "http://www.example.com/product/5?var=foo&bar=5"
61
+ expect(result).to eq(true)
62
+ end
63
+ end
64
+
65
+ describe ".matches" do
66
+ it "sets @@matcher to a regular expression" do
67
+ class OneboxEngineFar
68
+ include Onebox::Engine
69
+
70
+ matches do
71
+ find "foo.com"
72
+ end
73
+ end
74
+ regex = OneboxEngineFar.class_variable_get(:@@matcher)
75
+ expect(regex).to eq(/(?:foo\.com)/i)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
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
+
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
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox::Preview do
4
+ describe "#to_s" do
5
+ it "returns some html if given a valid url" do
6
+ fake("http://www.example.com", response("example.response"))
7
+ preview = described_class.new("http://www.example.com")
8
+ expect(preview.to_s).to include("Example Domain 1")
9
+ end
10
+ it "returns an empty string if the resource is not found"
11
+ it "returns an empty string if the resource fails to load"
12
+ it "returns an empty string if the url is not valid"
13
+ end
14
+
15
+ describe "#engine" do
16
+ it "returns an engine" do
17
+ preview = described_class.new("http://www.example.com")
18
+ expect(preview.send(:engine)).to be_a(Onebox::Engine)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe Onebox do
4
+ describe ".preview" do
5
+ it "creates a cache that responds as expected" do
6
+ url = "http://www.example.com"
7
+ preview = Onebox.preview(url)
8
+ cache = preview.cache
9
+ expect(cache).to respond_to(:key?, :store, :fetch)
10
+ end
11
+
12
+ it "stores the value in cache if it doesn't exist" do
13
+ url = "http://www.example.com"
14
+ preview = Onebox.preview(url)
15
+ preview.to_s
16
+ cache = preview.cache
17
+ expect(cache.key?(url)).to eq(true)
18
+ end
19
+
20
+ 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))
23
+ cache = preview.cache
24
+ expect(cache.fetch(url)).to be(nil)
25
+ end
26
+ end
27
+
28
+ describe "templates" do
29
+ let(:templates) { Dir["templates/*.handlebars"] }
30
+
31
+ def expect_templates_to_not_match(text)
32
+ templates.each do |template|
33
+ expect(File.read(template)).not_to match(text)
34
+ end
35
+ end
36
+
37
+ it "should not contain any triple braces" do
38
+ expect_templates_to_not_match(/\{\{\{/)
39
+ end
40
+
41
+ it "should not contain any script tags" do
42
+ expect_templates_to_not_match(/<script/)
43
+ end
44
+
45
+ it "should not contain any on*" do
46
+ expect_templates_to_not_match(/\s*on.+\s*=/)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ require "coveralls"
2
+ Coveralls.wear! do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require "rspec"
7
+ require "pry"
8
+ require "fakeweb"
9
+ require "onebox"
10
+ require 'mocha/api'
11
+
12
+ require_relative "support/html_spec_helper"
13
+
14
+ RSpec.configure do |config|
15
+ config.include HTMLSpecHelper
16
+ end
17
+
18
+ shared_examples_for "engines" do
19
+ it "should behave like an engine" do
20
+ expect(described_class.private_instance_methods).to include(:data, :record, :raw)
21
+ end
22
+
23
+ it "should have implemented a data method" do
24
+ expect { described_class.new(link).send(:data) }.not_to raise_error
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module HTMLSpecHelper
2
+ def fake(uri, response, verb = :get)
3
+ FakeWeb.register_uri(verb, uri, response: header(response))
4
+ end
5
+
6
+ def header(html)
7
+ "HTTP/1.1 200 OK\n\n#{html}"
8
+ end
9
+
10
+ def onebox_view(html)
11
+ %|<div class="onebox">#{html}</div>|
12
+ end
13
+
14
+ def response(file)
15
+ Nokogiri::HTML(File.read(File.join("spec", "fixtures", file)))
16
+ end
17
+ end