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
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in discourse-onebox.gemspec
3
+ # Specify your gem's dependencies in onebox.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -10,6 +10,14 @@ onebox
10
10
 
11
11
  Onebox is a library for turning media URLs into simple HTML previews of the resource.
12
12
 
13
+ Onebox currently has support for page, image, and video URLs for many popular sites.
14
+
15
+ It's great if you want users to input URLs and have your application convert them into
16
+ rich previews for display. For example, a link to a YouTube video would be automatically
17
+ converted into a video player.
18
+
19
+ It was originally created for [Discourse](http://discourse.org) but has since been
20
+ extracted into this convenient gem for all to use!
13
21
 
14
22
  Usage
15
23
  =====
@@ -51,12 +59,12 @@ preview = Onebox.preview(url, cache: Rails.cache)
51
59
  "#{preview}" == preview.to_s #=> true
52
60
  ```
53
61
 
54
- In addition you can set your own defaults with this handy interface:
62
+ In addition you can set your own options with this handy interface:
55
63
 
56
64
  ``` ruby
57
65
  require "onebox"
58
66
 
59
- Onebox.defaults = {
67
+ Onebox.options = {
60
68
  cache: Rails.cache
61
69
  }
62
70
 
@@ -66,10 +74,14 @@ preview = Onebox.preview(url)
66
74
  ```
67
75
 
68
76
 
69
- Setup
70
- =====
77
+ Adding Support for a new URL
78
+ ============================
79
+
80
+ 1. Check if the site supports [oEmbed](http://oembed.com/) or [Open Graph](https://developers.facebook.com/docs/opengraph/).
81
+ If it does, you can probably get away with just whitelisting the URL in `Onebox::Engine::WhitelistedGenericOnebox`.
82
+ If the site does not support open standards, you can create a new engine.
71
83
 
72
- 1. Create new onebox engine
84
+ 2. Create new onebox engine
73
85
 
74
86
  ``` ruby
75
87
  # in lib/onebox/engine/name_onebox.rb
@@ -77,7 +89,7 @@ Setup
77
89
  module Onebox
78
90
  module Engine
79
91
  class NameOnebox
80
- include Engine
92
+ include LayoutSupport
81
93
  include HTML
82
94
 
83
95
  private
@@ -95,7 +107,7 @@ Setup
95
107
  end
96
108
  ```
97
109
 
98
- 2. Create new onebox spec
110
+ 3. Create new onebox spec
99
111
 
100
112
  ``` ruby
101
113
  # in spec/lib/onebox/engine/name_spec.rb
@@ -127,7 +139,7 @@ Setup
127
139
  end
128
140
  ```
129
141
 
130
- 3. Create new handlebars template
142
+ 4. Create new handlebars template
131
143
 
132
144
  ``` html
133
145
  # in templates/name.handlebars
@@ -141,52 +153,18 @@ Setup
141
153
  </div>
142
154
  ```
143
155
 
144
- 4. Create new fixture from HTML response
156
+ 5. Create new fixture from HTML response
145
157
 
146
158
  ``` bash
147
159
  curl --output spec/fixtures/oneboxname.response -L -X -GET http://example.com
148
160
  ```
149
161
 
150
- 5. Require in Engine module
162
+ 6. Require in Engine module
151
163
 
152
164
  ``` ruby
153
165
  # in lib/onebox/engine/engine.rb
154
166
  require_relative "engine/name_onebox"
155
167
  ```
156
-
157
- Onebox currently has support for page, image, and video URLs from these sites:
158
-
159
- - Amazon
160
- - Android App Store
161
- - Apple Store
162
- - BlipTV
163
- - Clikthrough
164
- - College Humor
165
- - Dailymotion
166
- - Dotsub
167
- - Flickr
168
- - Funny or Die
169
- - GitHub
170
- - Blob
171
- - Commit
172
- - Gist
173
- - Pull Request
174
- - Hulu
175
- - Imgur
176
- - Kinomap
177
- - NFB
178
- - Open Graph
179
- - Qik
180
- - Revision
181
- - Rotten Tomatoes
182
- - Slideshare
183
- - SmugMug
184
- - SoundCloud
185
- - Stack Exchange
186
- - TED
187
- - Twitter
188
- - Wikipedia
189
- - yFrog
190
168
 
191
169
 
192
170
  Installing
@@ -194,7 +172,7 @@ Installing
194
172
 
195
173
  Add this line to your application's Gemfile:
196
174
 
197
- gem "onebox". "~> 1.0"
175
+ gem "onebox". "~> 1.2"
198
176
 
199
177
  And then execute:
200
178
 
data/lib/onebox.rb CHANGED
@@ -7,30 +7,38 @@ require "hexpress"
7
7
  require "hexpress/web"
8
8
  require "ostruct"
9
9
  require "moneta"
10
-
11
- require_relative "onebox/version"
12
- require_relative "onebox/preview"
13
- require_relative "onebox/matcher"
14
- require_relative "onebox/engine"
15
- require_relative "onebox/view"
10
+ require "cgi"
16
11
 
17
12
  module Onebox
18
13
  DEFAULTS = {
19
14
  cache: Moneta.new(:Memory, expires: true, serializer: :json),
20
- timeout: 10
15
+ timeout: 10,
16
+ load_paths: [File.join(Gem::Specification.find_by_name("onebox").gem_dir, "templates")]
21
17
  }
22
18
 
23
- @@defaults = DEFAULTS
19
+ @@options = DEFAULTS
24
20
 
25
- def self.preview(url, options = Onebox.defaults)
21
+ def self.preview(url, options = Onebox.options)
26
22
  Preview.new(url, options)
27
23
  end
28
24
 
29
- def self.defaults
30
- OpenStruct.new(@@defaults)
25
+ def self.options
26
+ OpenStruct.new(@@options)
27
+ end
28
+
29
+ def self.has_matcher?(url)
30
+ result = Matcher.new(url).oneboxed
31
+ !!result
31
32
  end
32
33
 
33
- def self.defaults=(options)
34
- @@defaults = DEFAULTS.merge(options)
34
+ def self.options=(options)
35
+ @@options = DEFAULTS.merge(options)
35
36
  end
36
37
  end
38
+
39
+ require_relative "onebox/version"
40
+ require_relative "onebox/preview"
41
+ require_relative "onebox/matcher"
42
+ require_relative "onebox/engine"
43
+ require_relative "onebox/layout"
44
+ require_relative "onebox/view"
data/lib/onebox/engine.rb CHANGED
@@ -13,92 +13,102 @@ module Onebox
13
13
  attr_reader :url
14
14
  attr_reader :cache
15
15
  attr_reader :timeout
16
- attr_reader :view
17
16
 
18
17
  def initialize(link, cache = nil, timeout = nil)
19
18
  @url = link
20
- @cache = cache || Onebox.defaults.cache
21
- @timeout = timeout || Onebox.defaults.timeout
22
- @view = View.new(self.class.template_name, true)
19
+ @cache = cache || Onebox.options.cache
20
+ @timeout = timeout || Onebox.options.timeout
23
21
  end
24
22
 
23
+ # raises error if not defined in onebox engine. This is the output method for
24
+ # an engine.
25
25
  def to_html
26
- view.to_html(record)
26
+ fail NoMethodError, "Engines need to implement this method"
27
+ end
28
+
29
+ # Some oneboxes create iframes or other complicated controls. If you're using
30
+ # a live editor with HTML preview, rendering those complicated controls can
31
+ # be slow or cause flickering.
32
+ #
33
+ # This method allows engines to produce a placeholder such as static image
34
+ # frame of a video.
35
+ #
36
+ # By default it just calls `to_html` unless implemented.
37
+ def placeholder_html
38
+ to_html
27
39
  end
28
40
 
29
41
  private
30
42
 
31
43
  def record
32
- if cache.key?(url)
33
- cache.fetch(url)
34
- else
35
- cache.store(url, data)
36
- end
44
+ result = cache.fetch(url) { data }
45
+ cache[url] = result if cache.respond_to?(:key?)
46
+ result
37
47
  end
38
48
 
39
49
  # raises error if not defined in onebox engine
40
- # in each onebox, uses either Nokogiri or OpenGraph to get raw HTML from url
50
+ # in each onebox, uses either Nokogiri or StandardEmbed to get raw HTML from url
41
51
  def raw
42
- raise NoMethodError, "Engines need to implement this method"
52
+ fail NoMethodError, "Engines need to implement this method"
43
53
  end
44
54
 
45
55
  # raises error if not defined in onebox engine
46
56
  # in each onebox, returns hash of desired onebox content
47
57
  def data
48
- raise NoMethodError, "Engines need this method defined"
58
+ fail NoMethodError, "Engines need this method defined"
59
+ end
60
+
61
+ def link
62
+ @url.gsub(/['\"<>]/, {
63
+ "'" => '&#39;',
64
+ '&' => '&amp;',
65
+ '"' => '&quot;',
66
+ '<' => '&lt;',
67
+ '>' => '&gt;',
68
+ })
49
69
  end
50
70
 
51
71
  module ClassMethods
52
- def ===(object)
53
- if object.kind_of?(String)
54
- !!(object =~ class_variable_get(:@@matcher))
72
+ def ===(other)
73
+ if other.kind_of?(URI)
74
+ !!(other.to_s =~ class_variable_get(:@@matcher))
55
75
  else
56
76
  super
57
77
  end
58
78
  end
59
79
 
60
80
  def matches(&block)
61
- class_variable_set :@@matcher, Hexpress.new(&block).to_r
81
+ matches_regexp(Hexpress.new(&block).to_r)
82
+ end
83
+
84
+ def matches_regexp(r)
85
+ class_variable_set :@@matcher, r
62
86
  end
63
87
 
64
- # calculates handlebars template name for onebox using name of engine
65
- def template_name
88
+ # calculates a name for onebox using the class name of engine
89
+ def onebox_name
66
90
  name.split("::").last.downcase.gsub(/onebox/, "")
67
91
  end
92
+
68
93
  end
69
94
  end
70
95
  end
71
96
 
72
- require_relative "engine/open_graph"
97
+ require_relative "helpers"
98
+ require_relative "layout_support"
99
+ require_relative "engine/standard_embed"
73
100
  require_relative "engine/html"
74
101
  require_relative "engine/json"
75
- require_relative "engine/example_onebox"
76
102
  require_relative "engine/amazon_onebox"
77
- require_relative "engine/bliptv_onebox"
78
- require_relative "engine/clikthrough_onebox"
79
- require_relative "engine/college_humor_onebox"
80
- require_relative "engine/dailymotion_onebox"
81
- require_relative "engine/dotsub_onebox"
82
- require_relative "engine/flickr_onebox"
83
- require_relative "engine/funny_or_die_onebox"
103
+ require_relative "engine/classic_google_maps_onebox"
84
104
  require_relative "engine/github_blob_onebox"
85
105
  require_relative "engine/github_commit_onebox"
86
106
  require_relative "engine/github_gist_onebox"
87
107
  require_relative "engine/github_pullrequest_onebox"
88
- require_relative "engine/hulu_onebox"
89
- require_relative "engine/imgur_image_onebox"
90
- require_relative "engine/itunes_onebox"
91
- require_relative "engine/kinomap_onebox"
92
- require_relative "engine/nfb_onebox"
93
- require_relative "engine/qik_onebox"
94
- require_relative "engine/revision3_onebox"
95
- require_relative "engine/slideshare_onebox"
96
- require_relative "engine/sound_cloud_onebox"
97
- require_relative "engine/spotify_onebox"
108
+ require_relative "engine/google_play_app_onebox"
109
+ require_relative "engine/image_onebox"
98
110
  require_relative "engine/stack_exchange_onebox"
99
- require_relative "engine/ted_onebox"
100
- require_relative "engine/twitter_onebox"
101
- require_relative "engine/viddler_onebox"
102
- require_relative "engine/vimeo_onebox"
111
+ require_relative "engine/twitter_status_onebox"
103
112
  require_relative "engine/wikipedia_onebox"
104
- require_relative "engine/yfrog_onebox"
113
+ require_relative "engine/youtube_onebox"
114
+ require_relative "engine/whitelisted_generic_onebox"
@@ -2,6 +2,7 @@ module Onebox
2
2
  module Engine
3
3
  class AmazonOnebox
4
4
  include Engine
5
+ include LayoutSupport
5
6
  include HTML
6
7
 
7
8
  matches do
@@ -11,16 +12,42 @@ module Onebox
11
12
  has(".").either("com", "ca").maybe("/")
12
13
  end
13
14
 
15
+ def url
16
+ return "http://www.amazon.com/gp/aw/d/" + URI::encode(match[:id]) if match && match[:id]
17
+ @url
18
+ end
19
+
20
+ def http_params
21
+ {'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3'}
22
+ end
23
+
14
24
  private
15
25
 
26
+ def match
27
+ @match ||= @url.match(/(?:d|g)p\/(?:product\/)?(?<id>[^\/]+)(?:\/|$)/mi)
28
+ end
29
+
30
+ def image
31
+ case
32
+ when raw.css("#main-image").any?
33
+ raw.css("#main-image").first["src"]
34
+ when raw.css("#landingImage").any?
35
+ raw.css("#landingImage").first["src"]
36
+ end
37
+ end
38
+
16
39
  def data
17
- {
18
- url: @url,
19
- name: raw.css("h1").inner_text,
20
- image: raw.css("#main-image").first["src"],
21
- description: raw.css("#postBodyPS").inner_text,
22
- price: raw.css(".priceLarge").inner_text
23
- }
40
+ result = { link: link,
41
+ title: raw.css("h1").inner_text,
42
+ image: image,
43
+ price: raw.css(".priceLarge").inner_text }
44
+
45
+ result[:by_info] = raw.at("#by-line")
46
+ result[:by_info] = Onebox::Helpers.clean(result[:by_info].inner_html) if result[:by_info]
47
+
48
+ summary = raw.at("#about-item span")
49
+ result[:description] = summary.inner_html if summary
50
+ result
24
51
  end
25
52
  end
26
53
  end
@@ -0,0 +1,37 @@
1
+ module Onebox
2
+ module Engine
3
+ class ClassicGoogleMapsOnebox
4
+ include Engine
5
+ include LayoutSupport
6
+
7
+ matches_regexp /^(https?:)?\/\/(maps\.google\.[\w.]{2,}|goo\.gl)\/maps?.+$/
8
+
9
+ def url
10
+ @url = get_long_url if @url.include?("//goo.gl/maps/")
11
+ @url
12
+ end
13
+
14
+ def to_html
15
+ "<iframe src='#{url}&output=embed' width='690px' height='400px' frameborder='0' style='border:0'></iframe>"
16
+ end
17
+
18
+ private
19
+
20
+ def data
21
+ {link: url, title: url}
22
+ end
23
+
24
+ def get_long_url
25
+ uri = URI(@url)
26
+ http = Net::HTTP.start(uri.host, uri.port)
27
+ http.open_timeout = timeout
28
+ http.read_timeout = timeout
29
+ response = http.head(uri.path)
30
+ response["Location"] if response.code == "301"
31
+ rescue
32
+ @url
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -2,7 +2,7 @@ module Onebox
2
2
  module Engine
3
3
  class GithubBlobOnebox
4
4
  include Engine
5
- include HTML
5
+ include LayoutSupport
6
6
 
7
7
  matches do
8
8
  http
@@ -15,13 +15,33 @@ module Onebox
15
15
 
16
16
  private
17
17
 
18
+ def raw
19
+ return @raw if @raw
20
+ m = @url.match(/github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi)
21
+ if m
22
+ from = (m[:from] || -1).to_i
23
+ to = (m[:to] || -1).to_i
24
+ @file = m[:file]
25
+ contents = open("https://raw.github.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}", read_timeout: timeout).read
26
+ if from > 0
27
+ if to < 0
28
+ from = from - 10
29
+ to = from + 20
30
+ end
31
+ if to > from
32
+ contents = contents.split("\n")[from..to].join("\n")
33
+ end
34
+ end
35
+ if contents.length > 5000
36
+ contents = contents[0..5000]
37
+ @truncated = true
38
+ end
39
+ @raw = contents
40
+ end
41
+ end
42
+
18
43
  def data
19
- {
20
- url: @url,
21
- filename: raw.css(".final-path").inner_text,
22
- lines: raw.css("#files .file .info .mode + span").inner_text,
23
- file: raw.css("#files .file .blob-wrapper").inner_text
24
- }
44
+ @data ||= {title: link, link: link, content: raw, truncated: @truncated}
25
45
  end
26
46
  end
27
47
  end