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,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class ExampleOnebox
4
+ include Engine
5
+ include HTML
6
+
7
+ matches do
8
+ find "example.com"
9
+ end
10
+
11
+ private
12
+
13
+ def data
14
+ {
15
+ header: raw.css("h1").inner_text
16
+ }
17
+ end
18
+
19
+ def template
20
+ %|<div class="onebox">{{header}}</div>|
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class FlickrOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?\:\/\/.*\.flickr\.com\/.*$/
9
+ find "flickr.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class FunnyOrDieOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?\:\/\/(www\.)?funnyordie\.com\/videos\/.*$/$/
9
+ find "funnyordie.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video].first[:url].first[:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,11 @@
1
+ module Onebox
2
+ module Engine
3
+ module HTML
4
+ private
5
+
6
+ def raw
7
+ @raw ||= Nokogiri::HTML(open(@url))
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class HuluOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ #/^https?\:\/\/www\.hulu\.com\/watch\/.*$/
9
+ find "hulu.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video][1][:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class NFBOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www\.)?nfb\.ca\/film\/[-\w]+\/?/
9
+ find "nfb.ca"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ description: raw.description,
19
+ video: raw.metadata[:video].first[:_value]
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,11 @@
1
+ module Onebox
2
+ module Engine
3
+ module OpenGraph
4
+ private
5
+
6
+ def raw
7
+ @raw ||= ::OpenGraph.new(@url)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Onebox
2
+ module Engine
3
+ class QikOnebox
4
+ include Engine
5
+ include HTML
6
+
7
+ matches do
8
+ # /^https?\:\/\/qik\.com\/video\/.*$/
9
+ find "qik.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.css(".info h2").inner_text,
18
+ image: raw.css(".userphoto").first["src"]
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class Revision3Onebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^http\:\/\/(.*\.)?revision3\.com\/.*$/
9
+ find "revision3.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video].first[:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class SlideshareOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?\:\/\/(www\.)?slideshare\.net\/*\/.*$/
9
+ find "slideshare.net"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class SoundCloudOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www\.)?soundcloud\.com\/.+$/
9
+ find "soundcloud.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video][1][:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module Onebox
2
+ module Engine
3
+ class StackExchangeOnebox
4
+ include Engine
5
+ include HTML
6
+
7
+ matches do
8
+ # /^http:\/\/(?:(?:(?<subsubdomain>\w*)\.)?(?<subdomain>\w*)\.)?(?<domain>#{DOMAINS.join('|')})\.com\/(?:questions|q)\/(?<question>\d*)/
9
+ find "stackexchange.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.css(".question-hyperlink").inner_text,
18
+ question: raw.css(".question .post-text p").first.inner_text
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class TedOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?\:\/\/(www\.)?ted\.com\/talks\/.*$/
9
+ find "ted.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class ViddlerOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www\.)?viddler\.com\/.+$/
9
+ find "viddler.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video].first[:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class VimeoOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?\:\/\/vimeo\.com\/.*$/
9
+ find "vimeo.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description,
20
+ video: raw.metadata[:video].first[:_value]
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,24 @@
1
+ module Onebox
2
+ module Engine
3
+ class WikipediaOnebox
4
+ include Engine
5
+ include HTML
6
+
7
+ matches do
8
+ # /^https?:\/\/.*wikipedia\.(com|org)\/.*$/
9
+ find "wikipedia.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ name: raw.css("html body h1").inner_text,
18
+ image: raw.css(".infobox .image img").first["src"],
19
+ description: raw.css("html body p").inner_text
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class YfrogOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www\.)?yfrog\.(com|ru|com\.tr|it|fr|co\.il|co\.uk|com\.pl|pl|eu|us)\/[a-zA-Z0-9]+/
9
+ find "yfrog.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ image: raw.images.first,
19
+ description: raw.description
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,13 @@
1
+ module Onebox
2
+ class Matcher
3
+ def initialize(link)
4
+ @url = link
5
+ end
6
+
7
+ def oneboxed
8
+ Engine.engines.select do |engine|
9
+ engine === @url
10
+ end.first
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module Onebox
2
+ class Preview
3
+ attr_reader :cache
4
+
5
+ def initialize(link, parameters = Onebox.defaults)
6
+ @url = link
7
+ @options = parameters
8
+ @cache = options.cache
9
+ @engine = Matcher.new(@url).oneboxed
10
+ end
11
+
12
+ def to_s
13
+ engine.to_html
14
+ end
15
+
16
+ def options
17
+ OpenStruct.new(@options)
18
+ end
19
+
20
+ private
21
+
22
+ def engine
23
+ @engine.new(@url, cache)
24
+ end
25
+
26
+ class InvalidURI < StandardError
27
+
28
+ end
29
+ end
30
+ end
31
+