onebox 1.7.7 → 1.7.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/onebox.rb +4 -2
- data/lib/onebox/matcher.rb +2 -0
- data/lib/onebox/preview.rb +32 -35
- data/lib/onebox/version.rb +1 -1
- data/spec/lib/onebox/matcher_spec.rb +22 -2
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4aaa05355640126f6ad6ad266ec44e444f23427
|
4
|
+
data.tar.gz: 1b3e3acc8b0d5a99fe99d5e09488cee2d2f98f7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f287783f7c37eb59b2809170467f52e3104db29afed8e329ac16a60cf73acb1787aafa0dbfee15004b9f4dbca4dca02469d8efda740c8804964c3165f22b7a39
|
7
|
+
data.tar.gz: df5f79f7d6089ac381c50ca7ea6b39364066f85998c4a8b3dc70a0c61f5e35d162dd0d3ac41f768d8c50f5f9f5b9464383a926bae75fd49ff89e90b90961d059
|
data/lib/onebox.rb
CHANGED
@@ -15,8 +15,10 @@ module Onebox
|
|
15
15
|
cache: Moneta.new(:Memory, expires: true, serializer: :json),
|
16
16
|
connect_timeout: 5,
|
17
17
|
timeout: 10,
|
18
|
-
max_download_kb: (10 * 1024),
|
19
|
-
load_paths: [File.join(Gem::Specification.find_by_name("onebox").gem_dir, "templates")]
|
18
|
+
max_download_kb: (10 * 1024), # 10MB
|
19
|
+
load_paths: [File.join(Gem::Specification.find_by_name("onebox").gem_dir, "templates")],
|
20
|
+
allowed_ports: [80, 443],
|
21
|
+
allowed_schemes: ["http", "https"],
|
20
22
|
}
|
21
23
|
|
22
24
|
@@options = DEFAULTS
|
data/lib/onebox/matcher.rb
CHANGED
@@ -12,6 +12,8 @@ module Onebox
|
|
12
12
|
|
13
13
|
def oneboxed
|
14
14
|
uri = URI(@url)
|
15
|
+
return unless uri.port.nil? || Onebox.options.allowed_ports.include?(uri.port)
|
16
|
+
return unless uri.scheme.nil? || Onebox.options.allowed_schemes.include?(uri.scheme)
|
15
17
|
ordered_engines.find { |engine| engine === uri }
|
16
18
|
rescue URI::InvalidURIError
|
17
19
|
nil
|
data/lib/onebox/preview.rb
CHANGED
@@ -5,6 +5,8 @@ module Onebox
|
|
5
5
|
class Preview
|
6
6
|
attr_reader :cache
|
7
7
|
|
8
|
+
WEB_EXCEPTIONS ||= [Net::HTTPServerException, OpenURI::HTTPError, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED]
|
9
|
+
|
8
10
|
def initialize(link, parameters = Onebox.options)
|
9
11
|
@url = link
|
10
12
|
@options = parameters
|
@@ -15,14 +17,14 @@ module Onebox
|
|
15
17
|
def to_s
|
16
18
|
return "" unless engine
|
17
19
|
sanitize process_html engine_html
|
18
|
-
rescue *
|
20
|
+
rescue *WEB_EXCEPTIONS
|
19
21
|
""
|
20
22
|
end
|
21
23
|
|
22
24
|
def placeholder_html
|
23
25
|
return "" unless engine
|
24
26
|
sanitize process_html engine.placeholder_html
|
25
|
-
rescue *
|
27
|
+
rescue *WEB_EXCEPTIONS
|
26
28
|
""
|
27
29
|
end
|
28
30
|
|
@@ -30,51 +32,46 @@ module Onebox
|
|
30
32
|
OpenStruct.new(@options)
|
31
33
|
end
|
32
34
|
|
33
|
-
def self.web_exceptions
|
34
|
-
[Net::HTTPServerException, OpenURI::HTTPError, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED]
|
35
|
-
end
|
36
|
-
|
37
35
|
private
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def engine_html
|
38
|
+
engine.to_html
|
39
|
+
end
|
42
40
|
|
43
|
-
|
44
|
-
|
41
|
+
def process_html(html)
|
42
|
+
return "" unless html
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
if @options[:max_width]
|
45
|
+
doc = Nokogiri::HTML::fragment(html)
|
46
|
+
if doc
|
47
|
+
doc.css('[width]').each do |e|
|
48
|
+
width = e['width'].to_i
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
if width > @options[:max_width]
|
51
|
+
height = e['height'].to_i
|
52
|
+
if (height > 0)
|
53
|
+
ratio = (height.to_f / width.to_f)
|
54
|
+
e['height'] = (@options[:max_width] * ratio).floor
|
55
|
+
end
|
56
|
+
e['width'] = @options[:max_width]
|
57
57
|
end
|
58
|
-
e['width'] = @options[:max_width]
|
59
58
|
end
|
59
|
+
return doc.to_html
|
60
60
|
end
|
61
|
-
return doc.to_html
|
62
61
|
end
|
63
|
-
end
|
64
62
|
|
65
|
-
|
66
|
-
|
63
|
+
html
|
64
|
+
end
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
66
|
+
def sanitize(html)
|
67
|
+
Sanitize.fragment(html, Sanitize::Config::ONEBOX)
|
68
|
+
end
|
71
69
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
70
|
+
def engine
|
71
|
+
return nil unless @engine_class
|
72
|
+
@engine ||= @engine_class.new(@url, cache)
|
73
|
+
end
|
76
74
|
|
77
|
-
|
78
|
-
end
|
75
|
+
class InvalidURI < StandardError; end
|
79
76
|
end
|
80
77
|
end
|
data/lib/onebox/version.rb
CHANGED
@@ -34,7 +34,7 @@ describe Onebox::Matcher do
|
|
34
34
|
let(:url) { "http://party.time.made.up-url.com/?article_id=1234" }
|
35
35
|
let(:matcher) { Onebox::Matcher.new(url) }
|
36
36
|
|
37
|
-
it "
|
37
|
+
it "finds an engine" do
|
38
38
|
matcher.stubs(:ordered_engines).returns([TestEngine])
|
39
39
|
expect(matcher.oneboxed).not_to be_nil
|
40
40
|
end
|
@@ -44,11 +44,31 @@ describe Onebox::Matcher do
|
|
44
44
|
let(:url) { "http://party.time.made.up-url.com/#article_id=1234" }
|
45
45
|
let(:matcher) { Onebox::Matcher.new(url) }
|
46
46
|
|
47
|
-
it "
|
47
|
+
it "finds an engine" do
|
48
48
|
matcher.stubs(:ordered_engines).returns([TestEngine])
|
49
49
|
expect(matcher.oneboxed).not_to be_nil
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
+
describe "with a whitelisted port/scheme" do
|
54
|
+
%w{http://example.com https://example.com http://example.com:80 //example.com}.each do |url|
|
55
|
+
it "finds an engine for '#{url}'" do
|
56
|
+
matcher = Onebox::Matcher.new(url)
|
57
|
+
matcher.stubs(:ordered_engines).returns([TestEngine])
|
58
|
+
expect(matcher.oneboxed).not_to be_nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "without a whitelisted port/scheme" do
|
64
|
+
%w{http://example.com:21 ftp://example.com}.each do |url|
|
65
|
+
it "doesn't find an engine for '#{url}'" do
|
66
|
+
matcher = Onebox::Matcher.new(url)
|
67
|
+
matcher.stubs(:ordered_engines).returns([TestEngine])
|
68
|
+
expect(matcher.oneboxed).to be_nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
53
73
|
end
|
54
74
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onebox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joanna Zeta
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-01-
|
13
|
+
date: 2017-01-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
@@ -486,7 +486,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
486
486
|
version: '0'
|
487
487
|
requirements: []
|
488
488
|
rubyforge_project:
|
489
|
-
rubygems_version: 2.
|
489
|
+
rubygems_version: 2.5.1
|
490
490
|
signing_key:
|
491
491
|
specification_version: 4
|
492
492
|
summary: A gem for turning URLs into previews.
|
@@ -550,4 +550,3 @@ test_files:
|
|
550
550
|
- spec/lib/onebox_spec.rb
|
551
551
|
- spec/spec_helper.rb
|
552
552
|
- spec/support/html_spec_helper.rb
|
553
|
-
has_rdoc:
|