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
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ onebox
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in discourse-onebox.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 jzeta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ onebox
2
+ ----------
3
+
4
+ - TODO: register to rubygems.org
5
+ - [![Code Climate](https://codeclimate.com/github/dysania/onebox.png)](https://codeclimate.com/github/dysania/onebox)
6
+ - [![Build Status](https://travis-ci.org/dysania/onebox.png)](https://travis-ci.org/dysania/onebox)
7
+ - [![Dependency Status](https://gemnasium.com/dysania/onebox.png)](https://gemnasium.com/dysania/onebox)
8
+ - [![Coverage Status](https://coveralls.io/repos/dysania/onebox/badge.png)](https://coveralls.io/r/dysania/onebox)
9
+
10
+
11
+ Onebox is a library for turning media URLs into previews.
12
+
13
+ Onebox currently has support for page, image, and video URLs from these sites:
14
+ - Amazon
15
+ - Android App Store
16
+ - Apple Store
17
+ - BlipTV
18
+ - Clikthrough
19
+ - College Humor
20
+ - Dailymotion
21
+ - Dotsub
22
+ - Flickr
23
+ - Funny or Die
24
+ - Gist
25
+ - Github
26
+ - Blob
27
+ - Commit
28
+ - Pull Request
29
+ - Hulu
30
+ - Imgur
31
+ - Kinomap
32
+ - NFB
33
+ - Open Graph
34
+ - Qik
35
+ - Revision
36
+ - Rotten Tomatoes
37
+ - Slideshare
38
+ - Smugmug
39
+ - Soundcloud
40
+ - Stack Exchange
41
+ - TED
42
+ - Twitter
43
+ - Wikipedia
44
+ - yFrog
45
+
46
+
47
+ Using onebox
48
+ ===============
49
+
50
+ You can include onebox modules into a class like so:
51
+
52
+ ``` ruby
53
+ require "onebox"
54
+ ```
55
+ TODO: write example
56
+
57
+
58
+
59
+ The `Gemfile` file would look like this:
60
+
61
+ ``` ruby
62
+ # source/Gemfile
63
+ source "https://rubygems.org"
64
+
65
+ gem "onebox", "~> <%= version %>"
66
+ ```
67
+
68
+
69
+ Installing onebox
70
+ ==================
71
+
72
+ Add this line to your application's Gemfile:
73
+
74
+ gem 'onebox'
75
+
76
+ And then execute:
77
+
78
+ $ bundle
79
+
80
+ Or install it yourself as:
81
+
82
+ $ gem install onebox
83
+
84
+
85
+ Contributing
86
+ ============
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ require "rspec/core/rake_task"
3
+ require "yard"
4
+
5
+ begin
6
+ Bundler.setup :default, :development
7
+ Bundler::GemHelper.install_tasks
8
+ rescue Bundler::BundlerError => error
9
+ $stderr.puts error.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit error.status_code
12
+ end
13
+
14
+ RSpec::Core::RakeTask.new(:spec)
15
+
16
+ desc "Generate all of the docs"
17
+ YARD::Rake::YardocTask.new do |config|
18
+ config.files = Dir["lib/**/*.rb"]
19
+ end
20
+
21
+ desc "Default: run tests and generate docs"
22
+ task :default => [ :spec, :yard ]
data/lib/onebox.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "open-uri"
2
+ require "multi_json"
3
+ require "nokogiri"
4
+ require "mustache"
5
+ require "opengraph_parser"
6
+ require "verbal_expressions"
7
+ require "ostruct"
8
+ require "moneta"
9
+
10
+ require_relative "onebox/version"
11
+ require_relative "onebox/preview"
12
+ require_relative "onebox/matcher"
13
+ require_relative "onebox/engine"
14
+
15
+ module Onebox
16
+ DEFAULTS = {
17
+ cache: Moneta.new(:Memory, expires: true, serializer: :json)
18
+ }
19
+
20
+ @@defaults = DEFAULTS
21
+
22
+ def self.preview(url, options = Onebox.defaults)
23
+ Preview.new(url, options)
24
+ end
25
+
26
+ def self.defaults
27
+ @@defaults
28
+ end
29
+
30
+ def self.defaults=(options)
31
+ @@defaults = DEFAULTS.merge(options)
32
+ end
33
+ end
@@ -0,0 +1,93 @@
1
+ module Onebox
2
+ module Engine
3
+ def self.included(object)
4
+ object.extend(ClassMethods)
5
+ end
6
+
7
+ def self.engines
8
+ constants.select do |constant|
9
+ constant.to_s =~ /Onebox$/
10
+ end.map(&method(:const_get))
11
+ end
12
+
13
+ attr_reader :cache
14
+
15
+ def initialize(link, cache = Onebox.defaults)
16
+ @url = link
17
+ @cache = cache
18
+ end
19
+
20
+ def to_html
21
+ Mustache.render(template, record)
22
+ end
23
+
24
+ private
25
+
26
+ def record
27
+ if cache.key?(@url)
28
+ cache.fetch(@url)
29
+ else
30
+ cache.store(@url, data)
31
+ end
32
+ end
33
+
34
+ # raises error if not defined in onebox engine
35
+ # in each onebox, uses either Nokogiri or OpenGraph to get raw HTML from url
36
+ def raw
37
+ raise NoMethodError, "Engines need to implement this method"
38
+ end
39
+
40
+ def template
41
+ File.read(File.join("templates", "#{template_name}.handlebars"))
42
+ end
43
+
44
+ # calculates handlebars template name for onebox using name of engine
45
+ def template_name
46
+ self.class.name.split("::").last.downcase.gsub(/onebox/, "")
47
+ end
48
+
49
+ # raises error if not defined in onebox engine
50
+ # in each onebox, returns hash of desired onebox content
51
+ def data
52
+ raise NoMethodError, "Engines need this method defined"
53
+ end
54
+
55
+ module ClassMethods
56
+ def ===(object)
57
+ if object.kind_of?(String)
58
+ !!(object =~ class_variable_get(:@@matcher))
59
+ else
60
+ super
61
+ end
62
+ end
63
+
64
+ def matches(&block)
65
+ class_variable_set :@@matcher, VerEx.new(&block)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ require_relative "engine/open_graph"
72
+ require_relative "engine/html"
73
+ require_relative "engine/example_onebox"
74
+ require_relative "engine/amazon_onebox"
75
+ require_relative "engine/bliptv_onebox"
76
+ require_relative "engine/clikthrough_onebox"
77
+ require_relative "engine/college_humor_onebox"
78
+ require_relative "engine/dailymotion_onebox"
79
+ require_relative "engine/dotsub_onebox"
80
+ require_relative "engine/flickr_onebox"
81
+ require_relative "engine/funny_or_die_onebox"
82
+ require_relative "engine/hulu_onebox"
83
+ require_relative "engine/nfb_onebox"
84
+ require_relative "engine/sound_cloud_onebox"
85
+ require_relative "engine/qik_onebox"
86
+ require_relative "engine/revision3_onebox"
87
+ require_relative "engine/slideshare_onebox"
88
+ require_relative "engine/stack_exchange_onebox"
89
+ require_relative "engine/ted_onebox"
90
+ require_relative "engine/viddler_onebox"
91
+ require_relative "engine/vimeo_onebox"
92
+ require_relative "engine/wikipedia_onebox"
93
+ require_relative "engine/yfrog_onebox"
@@ -0,0 +1,25 @@
1
+ module Onebox
2
+ module Engine
3
+ class AmazonOnebox
4
+ include Engine
5
+ include HTML
6
+
7
+ matches do
8
+ #/^https?:\/\/(?:www\.)?amazon.(com|ca)\/.*$/
9
+ find "amazon.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ name: raw.css("h1").inner_text,
18
+ image: raw.css("#main-image").first["src"],
19
+ description: raw.css("#postBodyPS").inner_text,
20
+ price: raw.css(".priceLarge").inner_text
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class BliptvOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/blip.tv\/.+$/
9
+ find "blip.tv"
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,23 @@
1
+ module Onebox
2
+ module Engine
3
+ class ClikThroughOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www.)?clikthrough.com\/theater\/video\/\d+$/
9
+ find "clikthrough.com"
10
+ end
11
+
12
+ private
13
+
14
+ def data
15
+ {
16
+ url: @url,
17
+ title: raw.title,
18
+ description: raw.description
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Onebox
2
+ module Engine
3
+ class CollegeHumorOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/www.collegehumor.com\/video\/.*$/
9
+ find "collegehumor.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 DailymotionOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # /^https?:\/\/(?:www\.)?dailymotion\.com\/.+$/
9
+ find "dailymotion.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 DotsubOnebox
4
+ include Engine
5
+ include OpenGraph
6
+
7
+ matches do
8
+ # matcher /^https?:\/\/(?:www\.)?dotsub\.com\/.+$/
9
+ find "dotsub.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