metainspector 5.22.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +26 -21
- data/lib/meta_inspector/document.rb +76 -66
- data/lib/meta_inspector/errors.rb +5 -1
- data/lib/meta_inspector/html_encoding.rb +7 -1
- data/lib/meta_inspector/http_session.rb +51 -44
- data/lib/meta_inspector/image_downloader.rb +37 -26
- data/lib/meta_inspector/parser.rb +49 -50
- data/lib/meta_inspector/parsers/base.rb +7 -2
- data/lib/meta_inspector/parsers/head_links.rb +12 -27
- data/lib/meta_inspector/parsers/iframes.rb +2 -3
- data/lib/meta_inspector/parsers/images.rb +119 -70
- data/lib/meta_inspector/parsers/links.rb +10 -6
- data/lib/meta_inspector/parsers/meta_tags.rb +16 -29
- data/lib/meta_inspector/parsers/texts.rb +43 -33
- data/lib/meta_inspector/request.rb +42 -45
- data/lib/meta_inspector/url.rb +13 -16
- data/lib/meta_inspector/url_policy.rb +17 -10
- data/lib/meta_inspector/version.rb +1 -1
- data/lib/meta_inspector.rb +14 -13
- data/lib/metainspector.rb +1 -1
- data/spec/document_spec.rb +48 -68
- data/spec/meta_inspector/images_spec.rb +82 -1
- data/spec/meta_inspector/meta_inspector_spec.rb +3 -25
- data/spec/meta_inspector/texts_spec.rb +67 -12
- data/spec/parser_spec.rb +0 -10
- data/spec/request_errors_spec.rb +1 -1
- metadata +1 -1
|
@@ -5,14 +5,15 @@ module MetaInspector
|
|
|
5
5
|
|
|
6
6
|
# Returns the declared HTML language, or nil when missing or blank
|
|
7
7
|
def language
|
|
8
|
-
|
|
9
|
-
value unless value.to_s.empty?
|
|
8
|
+
stripped_presence(parsed.at_css('html')&.[]('lang'))
|
|
10
9
|
end
|
|
11
10
|
|
|
12
|
-
# Returns the
|
|
13
|
-
#
|
|
11
|
+
# Returns the content of the first <title> tag within the <head> section,
|
|
12
|
+
# without surrounding whitespace, or nil when missing or blank
|
|
14
13
|
def title
|
|
15
|
-
@title
|
|
14
|
+
return @title if defined?(@title)
|
|
15
|
+
|
|
16
|
+
@title = stripped_presence(parsed.at_css('head title')&.inner_text)
|
|
16
17
|
end
|
|
17
18
|
|
|
18
19
|
def best_title
|
|
@@ -30,7 +31,7 @@ module MetaInspector
|
|
|
30
31
|
def h3
|
|
31
32
|
@h3 ||= find_heading('h3')
|
|
32
33
|
end
|
|
33
|
-
|
|
34
|
+
|
|
34
35
|
def h4
|
|
35
36
|
@h4 ||= find_heading('h4')
|
|
36
37
|
end
|
|
@@ -59,12 +60,16 @@ module MetaInspector
|
|
|
59
60
|
@best_author ||= find_best_author
|
|
60
61
|
end
|
|
61
62
|
|
|
62
|
-
# Returns the meta description,
|
|
63
|
+
# Returns the meta description without surrounding whitespace, or nil when
|
|
64
|
+
# missing or blank
|
|
63
65
|
def description
|
|
64
|
-
@description
|
|
66
|
+
return @description if defined?(@description)
|
|
67
|
+
|
|
68
|
+
@description = stripped_presence(meta['description'])
|
|
65
69
|
end
|
|
66
70
|
|
|
67
|
-
# A description getter that returns the first non-blank description
|
|
71
|
+
# A description getter that returns the first non-blank description,
|
|
72
|
+
# without surrounding whitespace,
|
|
68
73
|
# from the following candidates:
|
|
69
74
|
# - the standard meta description
|
|
70
75
|
# - the og:description meta tag
|
|
@@ -76,40 +81,46 @@ module MetaInspector
|
|
|
76
81
|
|
|
77
82
|
private
|
|
78
83
|
|
|
84
|
+
def stripped_presence(text)
|
|
85
|
+
text = text&.strip
|
|
86
|
+
text unless text.nil? || text.empty?
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Strips the text and collapses its inner whitespace into single spaces
|
|
90
|
+
def squish(text)
|
|
91
|
+
text.strip.gsub(/\s+/, ' ')
|
|
92
|
+
end
|
|
93
|
+
|
|
79
94
|
def find_heading(heading)
|
|
80
|
-
parsed.css(heading).map { |tag| tag.inner_text
|
|
95
|
+
parsed.css(heading).map { |tag| squish(tag.inner_text) }.reject(&:empty?)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Returns the first candidate, String or node, with non-blank text, squished
|
|
99
|
+
def first_squished_text(candidates)
|
|
100
|
+
texts = candidates.flatten.compact.map { |c| squish(c.respond_to?(:inner_text) ? c.inner_text : c) }
|
|
101
|
+
texts.find { |text| !text.empty? }
|
|
81
102
|
end
|
|
82
103
|
|
|
83
104
|
# Look for candidates per list of priority
|
|
84
105
|
def find_best_title
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
]
|
|
93
|
-
candidates.flatten!
|
|
94
|
-
candidates.compact!
|
|
95
|
-
candidates.map! { |c| (c.respond_to? :inner_text) ? c.inner_text : c }
|
|
96
|
-
candidates.map! { |c| c.strip.gsub(/\s+/, ' ') }
|
|
97
|
-
candidates.find { |c| !c.to_s.empty? }
|
|
106
|
+
first_squished_text([
|
|
107
|
+
meta['title'],
|
|
108
|
+
meta['og:title'],
|
|
109
|
+
parsed.css('head title'),
|
|
110
|
+
parsed.css('body title'),
|
|
111
|
+
parsed.css('h1').first,
|
|
112
|
+
meta['parsely-title']
|
|
113
|
+
])
|
|
98
114
|
end
|
|
99
115
|
|
|
100
116
|
def find_best_author
|
|
101
|
-
|
|
117
|
+
first_squished_text([
|
|
102
118
|
meta['author'],
|
|
103
119
|
parsed.css('a[rel="author"]').first,
|
|
104
120
|
parsed.css('address').first,
|
|
105
121
|
meta['twitter:creator'],
|
|
106
122
|
meta['parsely-author']
|
|
107
|
-
]
|
|
108
|
-
candidates.flatten!
|
|
109
|
-
candidates.compact!
|
|
110
|
-
candidates.map! { |c| (c.respond_to? :inner_text) ? c.inner_text : c }
|
|
111
|
-
candidates.map! { |c| c.strip.gsub(/\s+/, ' ') }
|
|
112
|
-
candidates.find { |c| !c.to_s.empty? }
|
|
123
|
+
])
|
|
113
124
|
end
|
|
114
125
|
|
|
115
126
|
def find_best_description
|
|
@@ -119,13 +130,12 @@ module MetaInspector
|
|
|
119
130
|
meta['twitter:description'],
|
|
120
131
|
secondary_description
|
|
121
132
|
]
|
|
122
|
-
candidates.
|
|
133
|
+
candidates.filter_map { |candidate| stripped_presence(candidate) }.first
|
|
123
134
|
end
|
|
124
135
|
|
|
125
136
|
# Look for the first <p> block with 120 characters or more
|
|
126
137
|
def secondary_description
|
|
127
|
-
|
|
128
|
-
first_long_paragraph ? first_long_paragraph.text : ''
|
|
138
|
+
parsed.at_xpath('//p[string-length() >= 120]')&.text
|
|
129
139
|
end
|
|
130
140
|
end
|
|
131
141
|
end
|
|
@@ -2,50 +2,37 @@ require_relative 'http_session'
|
|
|
2
2
|
require_relative 'html_encoding'
|
|
3
3
|
|
|
4
4
|
module MetaInspector
|
|
5
|
-
|
|
6
5
|
# Makes the request to the server
|
|
7
6
|
class Request
|
|
8
7
|
def initialize(initial_url, options = {})
|
|
9
|
-
@url
|
|
10
|
-
|
|
8
|
+
@url = initial_url
|
|
11
9
|
URLPolicy.validate!(@url.url)
|
|
12
10
|
|
|
13
|
-
@allow_redirections
|
|
14
|
-
@head_first
|
|
11
|
+
@allow_redirections = options[:allow_redirections]
|
|
12
|
+
@head_first = options[:head_first]
|
|
15
13
|
@allow_non_html_content = options[:allow_non_html_content]
|
|
16
|
-
@connection_timeout
|
|
17
|
-
@read_timeout
|
|
18
|
-
@encoding
|
|
19
|
-
@http_session
|
|
14
|
+
@connection_timeout = options[:connection_timeout]
|
|
15
|
+
@read_timeout = options[:read_timeout]
|
|
16
|
+
@encoding = options[:encoding]
|
|
17
|
+
@http_session = options[:http_session] || HttpSession.new(options)
|
|
20
18
|
|
|
21
|
-
response
|
|
19
|
+
response # request early so we can fail early
|
|
22
20
|
end
|
|
23
21
|
|
|
24
22
|
extend Forwardable
|
|
25
23
|
delegate :url => :@url
|
|
26
24
|
|
|
25
|
+
# Responses without a Content-Type are treated as HTML.
|
|
26
|
+
def self.html_content_type?(content_type)
|
|
27
|
+
content_type.nil? || content_type == 'text/html'
|
|
28
|
+
end
|
|
29
|
+
|
|
27
30
|
def read
|
|
28
31
|
return unless response
|
|
32
|
+
|
|
29
33
|
body = response.body
|
|
30
34
|
check_content_encoding!(body)
|
|
31
|
-
|
|
32
|
-
# The adapter tags the body only with HTTP charsets Ruby knows.
|
|
33
|
-
label = response.headers['content-type'].to_s[/(?:\A|;)\s*charset\s*=\s*["']?([^;\s"']+)/i, 1]
|
|
34
|
-
charset = HtmlEncoding.resolve(label)
|
|
35
|
-
body = body.dup.force_encoding(charset) if charset
|
|
36
|
-
end
|
|
37
|
-
body = body.dup.force_encoding(@encoding) if @encoding
|
|
38
|
-
encoding = HtmlEncoding.byte_order_mark_encoding(body)
|
|
39
|
-
body = body.dup.force_encoding(encoding) if encoding && !Encoding.find(encoding).ascii_compatible?
|
|
40
|
-
body = HtmlEncoding.normalize(body)
|
|
41
|
-
# Replace invalid bytes with U+FFFD, as browsers do, so one bad byte
|
|
42
|
-
# doesn't make the whole page fail. Binary bodies are decoded later by the parser.
|
|
43
|
-
body = if body.encoding.ascii_compatible?
|
|
44
|
-
body.scrub
|
|
45
|
-
else
|
|
46
|
-
body.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
47
|
-
end
|
|
48
|
-
body.tr("\000", '')
|
|
35
|
+
to_utf8(with_declared_encoding(body)).tr("\000", '')
|
|
49
36
|
rescue ArgumentError, EncodingError => e
|
|
50
37
|
raise MetaInspector::RequestError.new(e)
|
|
51
38
|
end
|
|
@@ -67,27 +54,13 @@ module MetaInspector
|
|
|
67
54
|
|
|
68
55
|
private
|
|
69
56
|
|
|
70
|
-
# Once per process: apps fetch many pages with the same options, and a warning
|
|
71
|
-
# per request would flood their logs
|
|
72
|
-
def self.warn_faraday_http_cache_deprecation
|
|
73
|
-
return if @faraday_http_cache_deprecation_warned
|
|
74
|
-
|
|
75
|
-
@faraday_http_cache_deprecation_warned = true
|
|
76
|
-
warn 'DEPRECATION WARNING: the top-level faraday_http_cache option is deprecated and will be removed ' \
|
|
77
|
-
'in the next major version. Pass it inside faraday_options instead: ' \
|
|
78
|
-
'faraday_options: { faraday_http_cache: { store: cache } }'
|
|
79
|
-
end
|
|
80
|
-
|
|
81
57
|
def fetch
|
|
82
58
|
session = @http_session.build(url)
|
|
83
59
|
|
|
84
60
|
check_head_content_type(session) if @head_first
|
|
85
61
|
response = perform_request(session, :get)
|
|
86
62
|
|
|
87
|
-
if @allow_redirections
|
|
88
|
-
@url.url = response.env.url.to_s
|
|
89
|
-
end
|
|
90
|
-
|
|
63
|
+
@url.url = response.env.url.to_s if @allow_redirections
|
|
91
64
|
response
|
|
92
65
|
end
|
|
93
66
|
|
|
@@ -96,8 +69,32 @@ module MetaInspector
|
|
|
96
69
|
return unless head_response.success?
|
|
97
70
|
|
|
98
71
|
type = response_content_type(head_response)
|
|
99
|
-
|
|
100
|
-
|
|
72
|
+
raise NonHtmlError.for(type) unless @allow_non_html_content || self.class.html_content_type?(type)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Tags the body with its encoding, from highest to lowest precedence: a
|
|
76
|
+
# byte-order mark for encodings that aren't ASCII-compatible, the encoding:
|
|
77
|
+
# option, then the HTTP charset. The adapter tags the body only with HTTP
|
|
78
|
+
# charsets Ruby knows, so labels such as "x-sjis" are resolved here.
|
|
79
|
+
def with_declared_encoding(body)
|
|
80
|
+
if body.encoding == Encoding::BINARY
|
|
81
|
+
charset = HtmlEncoding.resolve(HtmlEncoding.charset_param(response.headers['content-type']))
|
|
82
|
+
body = body.dup.force_encoding(charset) if charset
|
|
83
|
+
end
|
|
84
|
+
body = body.dup.force_encoding(@encoding) if @encoding
|
|
85
|
+
bom_encoding = HtmlEncoding.byte_order_mark_encoding(body)
|
|
86
|
+
body = body.dup.force_encoding(bom_encoding) if bom_encoding && !Encoding.find(bom_encoding).ascii_compatible?
|
|
87
|
+
body
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Replaces invalid bytes with U+FFFD, as browsers do, so one bad byte
|
|
91
|
+
# doesn't make the whole page fail. Binary bodies are decoded later by the parser.
|
|
92
|
+
def to_utf8(body)
|
|
93
|
+
body = HtmlEncoding.normalize(body)
|
|
94
|
+
if body.encoding.ascii_compatible?
|
|
95
|
+
body.scrub
|
|
96
|
+
else
|
|
97
|
+
body.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
101
98
|
end
|
|
102
99
|
end
|
|
103
100
|
|
data/lib/meta_inspector/url.rb
CHANGED
|
@@ -2,35 +2,36 @@ require 'addressable/uri'
|
|
|
2
2
|
|
|
3
3
|
module MetaInspector
|
|
4
4
|
class URL
|
|
5
|
-
|
|
5
|
+
WELL_KNOWN_TRACKING_PARAMS = %w[utm_source utm_medium utm_term utm_content utm_campaign]
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
options = self.class.defaults.merge(options)
|
|
7
|
+
attr_reader :url
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
def self.defaults
|
|
10
|
+
{ normalize: true }
|
|
11
|
+
end
|
|
11
12
|
|
|
13
|
+
def initialize(initial_url, options = {})
|
|
14
|
+
@normalize = self.class.defaults.merge(options)[:normalize]
|
|
12
15
|
self.url = initial_url
|
|
13
16
|
end
|
|
14
17
|
|
|
15
18
|
def scheme
|
|
16
|
-
parsed(url)
|
|
19
|
+
parsed(url)&.scheme
|
|
17
20
|
end
|
|
18
21
|
|
|
19
22
|
def host
|
|
20
|
-
parsed(url)
|
|
23
|
+
parsed(url)&.host
|
|
21
24
|
end
|
|
22
25
|
|
|
23
26
|
def root_url
|
|
24
27
|
"#{scheme}://#{host}/"
|
|
25
28
|
end
|
|
26
29
|
|
|
27
|
-
WELL_KNOWN_TRACKING_PARAMS = %w( utm_source utm_medium utm_term utm_content utm_campaign )
|
|
28
|
-
|
|
29
30
|
def tracked?
|
|
30
|
-
|
|
31
|
-
return false unless
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
query_values = parsed(url).query_values
|
|
32
|
+
return false unless query_values
|
|
33
|
+
|
|
34
|
+
WELL_KNOWN_TRACKING_PARAMS.intersect?(query_values.keys)
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
# Removes only the tracking key=value pairs, keeping the rest of the
|
|
@@ -92,10 +93,6 @@ module MetaInspector
|
|
|
92
93
|
|
|
93
94
|
private
|
|
94
95
|
|
|
95
|
-
def self.defaults
|
|
96
|
-
{ :normalize => true }
|
|
97
|
-
end
|
|
98
|
-
|
|
99
96
|
def tracking_param?(param)
|
|
100
97
|
key = Addressable::URI.unencode_component(param.split('=', 2).first)
|
|
101
98
|
WELL_KNOWN_TRACKING_PARAMS.include?(key)
|
|
@@ -34,28 +34,35 @@ module MetaInspector
|
|
|
34
34
|
@allowed_headers = @image_origin ? IMAGE_HEADERS : PAGE_HEADERS
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
+
# Keeps only the PAGE_HEADERS or IMAGE_HEADERS on requests that leave the
|
|
38
|
+
# trusted origin, so credentials and cookies aren't sent elsewhere, sends image lookups that leave the page's origin with a Referer,
|
|
39
|
+
# and applies the url_filter.
|
|
37
40
|
def call(env)
|
|
38
41
|
uri = self.class.validate!(env.url)
|
|
39
42
|
cross_image_origin = @image_origin && origin(uri) != origin(@image_origin)
|
|
40
43
|
if cross_image_origin || (@request_origin && origin(uri) != origin(@request_origin))
|
|
41
44
|
env.request_headers.delete_if { |key, _| !@allowed_headers.include?(key.downcase) }
|
|
42
45
|
end
|
|
43
|
-
if cross_image_origin
|
|
44
|
-
unless @image_origin.scheme == 'https' && uri.scheme == 'http'
|
|
45
|
-
referer = @image_origin.dup
|
|
46
|
-
referer.userinfo = nil
|
|
47
|
-
referer.path = '/'
|
|
48
|
-
referer.query = nil
|
|
49
|
-
referer.fragment = nil
|
|
50
|
-
env.request_headers['Referer'] = referer.to_s
|
|
51
|
-
end
|
|
52
|
-
end
|
|
46
|
+
add_referer(env, uri) if cross_image_origin
|
|
53
47
|
check_filter(uri) if @filter
|
|
54
48
|
@app.call(env)
|
|
55
49
|
end
|
|
56
50
|
|
|
57
51
|
private
|
|
58
52
|
|
|
53
|
+
# Sends the page's origin, as browsers do by default, except on a
|
|
54
|
+
# downgrade from HTTPS to HTTP.
|
|
55
|
+
def add_referer(env, uri)
|
|
56
|
+
return if @image_origin.scheme == 'https' && uri.scheme == 'http'
|
|
57
|
+
|
|
58
|
+
referer = @image_origin.dup
|
|
59
|
+
referer.userinfo = nil
|
|
60
|
+
referer.path = '/'
|
|
61
|
+
referer.query = nil
|
|
62
|
+
referer.fragment = nil
|
|
63
|
+
env.request_headers['Referer'] = referer.to_s
|
|
64
|
+
end
|
|
65
|
+
|
|
59
66
|
def origin(uri)
|
|
60
67
|
[uri.scheme.downcase, uri.hostname.downcase, uri.port]
|
|
61
68
|
end
|
data/lib/meta_inspector.rb
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
require 'forwardable'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
require_relative 'meta_inspector/version'
|
|
3
|
+
require_relative 'meta_inspector/errors'
|
|
4
|
+
require_relative 'meta_inspector/url'
|
|
5
|
+
require_relative 'meta_inspector/request'
|
|
6
|
+
require_relative 'meta_inspector/image_downloader'
|
|
7
|
+
require_relative 'meta_inspector/parser'
|
|
8
|
+
require_relative 'meta_inspector/parsers/base'
|
|
9
|
+
require_relative 'meta_inspector/parsers/images'
|
|
10
|
+
require_relative 'meta_inspector/parsers/iframes'
|
|
11
|
+
require_relative 'meta_inspector/parsers/links'
|
|
12
|
+
require_relative 'meta_inspector/parsers/head_links'
|
|
13
|
+
require_relative 'meta_inspector/parsers/meta_tags'
|
|
14
|
+
require_relative 'meta_inspector/parsers/texts'
|
|
15
|
+
require_relative 'meta_inspector/document'
|
|
15
16
|
|
|
16
17
|
module MetaInspector
|
|
17
18
|
extend self
|
data/lib/metainspector.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative 'meta_inspector'
|
data/spec/document_spec.rb
CHANGED
|
@@ -82,8 +82,9 @@ describe MetaInspector::Document do
|
|
|
82
82
|
end
|
|
83
83
|
let(:page) { MetaInspector.new('https://example.com/', document: html) }
|
|
84
84
|
|
|
85
|
-
it '
|
|
86
|
-
expect(page).
|
|
85
|
+
it 'no longer has the feed and favicon methods removed in 6.0' do
|
|
86
|
+
expect(page).not_to respond_to(:feed)
|
|
87
|
+
expect(page).not_to respond_to(:favicon)
|
|
87
88
|
end
|
|
88
89
|
|
|
89
90
|
it 'exposes feeds through the plural API' do
|
|
@@ -115,41 +116,12 @@ describe MetaInspector::Document do
|
|
|
115
116
|
type: ' application/rss+xml ; charset=utf-8' }
|
|
116
117
|
])
|
|
117
118
|
end
|
|
118
|
-
|
|
119
|
-
it 'returns RSS or Atom feeds with parameters from the deprecated feed method' do
|
|
120
|
-
page = MetaInspector.new('https://example.com/', document: <<~HTML)
|
|
121
|
-
<link rel="alternate" type="application/feed+json" href="/feed.json">
|
|
122
|
-
<link rel="alternate" type="application/atom+xml; charset=utf-8" href="/atom.xml">
|
|
123
|
-
HTML
|
|
124
|
-
|
|
125
|
-
expect { expect(page.feed).to eq('https://example.com/atom.xml') }.to output(/DEPRECATION/).to_stderr
|
|
126
|
-
end
|
|
127
119
|
end
|
|
128
120
|
|
|
129
121
|
it 'exposes the favicon through the images API' do
|
|
130
122
|
expect(page.images.favicon).to eq('https://example.com/icon.png')
|
|
131
123
|
end
|
|
132
124
|
|
|
133
|
-
{ feed: 'feeds', favicon: 'images.favicon' }.each do |method, replacement|
|
|
134
|
-
it "warns on each call to ##{method}, naming its replacement and removal version" do
|
|
135
|
-
2.times do
|
|
136
|
-
expect { page.public_send(method) }.to output(
|
|
137
|
-
"DEPRECATION WARNING: page.#{method} is deprecated and will be removed in 6.0. Use page.#{replacement} instead.\n"
|
|
138
|
-
).to_stderr
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
it "returns nil from ##{method} when the document has no matching metadata" do
|
|
143
|
-
empty_page = MetaInspector.new('https://example.com/', document: '<html></html>')
|
|
144
|
-
|
|
145
|
-
expect { expect(empty_page.public_send(method)).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
it 'returns the favicon URL from the deprecated shortcut' do
|
|
150
|
-
expect { expect(page.favicon).to eq(page.images.favicon) }.to output(/DEPRECATION/).to_stderr
|
|
151
|
-
end
|
|
152
|
-
|
|
153
125
|
it 'keeps the supported APIs and serialization free of deprecation warnings' do
|
|
154
126
|
stub_request(:get, 'https://example.com/').to_return(body: html)
|
|
155
127
|
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
@@ -161,38 +133,6 @@ describe MetaInspector::Document do
|
|
|
161
133
|
end.not_to output.to_stderr
|
|
162
134
|
end
|
|
163
135
|
|
|
164
|
-
describe '#feed' do
|
|
165
|
-
let(:html) do
|
|
166
|
-
<<~HTML
|
|
167
|
-
<html><head>
|
|
168
|
-
<base href="https://example.com/blog/">
|
|
169
|
-
<link rel="alternate" type="application/json" href="feed.json">
|
|
170
|
-
<link rel="alternate" type="#{first_type}" href="first.xml">
|
|
171
|
-
<link rel="alternate" type="application/rss+xml" href="second.xml">
|
|
172
|
-
</head></html>
|
|
173
|
-
HTML
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
['application/rss+xml', 'application/atom+xml'].each do |type|
|
|
177
|
-
context "with #{type} first" do
|
|
178
|
-
let(:first_type) { type }
|
|
179
|
-
|
|
180
|
-
it 'returns the first RSS or Atom URL, skipping JSON and resolving the base URL' do
|
|
181
|
-
expect { expect(page.feed).to eq('https://example.com/blog/first.xml') }
|
|
182
|
-
.to output(/DEPRECATION/).to_stderr
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
context 'with only JSON feeds' do
|
|
188
|
-
let(:html) { '<link rel="alternate" type="application/json" href="/feed.json">' }
|
|
189
|
-
|
|
190
|
-
it 'returns nil' do
|
|
191
|
-
expect { expect(page.feed).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
192
|
-
end
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
|
|
196
136
|
['<html></html>', nil].each do |document|
|
|
197
137
|
context "with #{document ? 'empty' : 'populated'} HTML" do
|
|
198
138
|
before do
|
|
@@ -205,17 +145,57 @@ describe MetaInspector::Document do
|
|
|
205
145
|
it "can call ##{method}" do
|
|
206
146
|
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
207
147
|
|
|
208
|
-
|
|
209
|
-
expect { fetched_page.public_send(method) }.to output(/DEPRECATION/).to_stderr
|
|
210
|
-
else
|
|
211
|
-
expect { fetched_page.public_send(method) }.not_to raise_error
|
|
212
|
-
end
|
|
148
|
+
expect { fetched_page.public_send(method) }.not_to raise_error
|
|
213
149
|
end
|
|
214
150
|
end
|
|
215
151
|
end
|
|
216
152
|
end
|
|
217
153
|
end
|
|
218
154
|
|
|
155
|
+
describe 'unknown options' do
|
|
156
|
+
let(:url) { 'http://unknown-options.example/' }
|
|
157
|
+
|
|
158
|
+
it 'raises ArgumentError naming the unknown options, before any request' do
|
|
159
|
+
expect { MetaInspector.new(url, timeout: 5, allow_redirection: false) }
|
|
160
|
+
.to raise_error(ArgumentError, 'Unknown options: :timeout, :allow_redirection')
|
|
161
|
+
expect(a_request(:any, url)).not_to have_been_made
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it 'uses the singular for a single unknown option' do
|
|
165
|
+
expect { MetaInspector.new(url, timeout: 5) }.to raise_error(ArgumentError, 'Unknown option: :timeout')
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it 'rejects String keys, which would otherwise be ignored and fetch the page' do
|
|
169
|
+
expect { MetaInspector.new(url, 'document' => '<title>Hi</title>') }
|
|
170
|
+
.to raise_error(ArgumentError, 'Unknown option: "document" (option keys must be Symbols)')
|
|
171
|
+
expect(a_request(:any, url)).not_to have_been_made
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
it 'rejects the top-level faraday_http_cache option removed in 6.0, pointing to faraday_options' do
|
|
175
|
+
expect { MetaInspector.new(url, faraday_http_cache: { store: {} }) }
|
|
176
|
+
.to raise_error(ArgumentError, 'Unknown option: :faraday_http_cache ' \
|
|
177
|
+
'(pass it inside faraday_options: { faraday_http_cache: ... })')
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it 'accepts every documented option' do
|
|
181
|
+
options = {
|
|
182
|
+
connection_timeout: 1, read_timeout: 1, retries: 0, allow_redirections: false, head_first: false,
|
|
183
|
+
document: '<title>Hi</title>', headers: {}, encoding: nil, normalize_url: true,
|
|
184
|
+
allow_non_html_content: false, download_images: false, max_image_downloads: 0,
|
|
185
|
+
download_image_options: {}, fetch_all_image_meta: false, inline_image_data: false,
|
|
186
|
+
max_html_bytes: 100, url_filter: ->(_) { true }, faraday_options: {}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
expect(MetaInspector.new(url, options).title).to eq('Hi')
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it 'leaves the options hash it was given unchanged' do
|
|
193
|
+
options = { document: '<title>Hi</title>' }.freeze
|
|
194
|
+
|
|
195
|
+
expect { MetaInspector.new(url, options) }.not_to raise_error
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
219
199
|
describe 'max_html_bytes option' do
|
|
220
200
|
it 'rejects oversized supplied HTML before parsing' do
|
|
221
201
|
expect do
|
|
@@ -78,7 +78,10 @@ describe MetaInspector do
|
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
it 'applies the download limit to responsive candidates in document order' do
|
|
81
|
-
html =
|
|
81
|
+
html = <<~HTML
|
|
82
|
+
<picture><source srcset="/10x10 2x"><img src="/fallback.jpg" width="20" height="20"></picture>
|
|
83
|
+
<img srcset="/100x100 1x">
|
|
84
|
+
HTML
|
|
82
85
|
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
|
|
83
86
|
|
|
84
87
|
expect(page.images.with_size).to eq([
|
|
@@ -89,6 +92,62 @@ describe MetaInspector do
|
|
|
89
92
|
expect(WebMock).not_to have_requested(:get, 'http://example.com/100x100')
|
|
90
93
|
end
|
|
91
94
|
|
|
95
|
+
describe 'ranking responsive images' do
|
|
96
|
+
before do
|
|
97
|
+
%w[small medium fallback].each do |name|
|
|
98
|
+
stub_request(:get, "http://example.com/#{name}.jpg").to_return(fixture_file('10x10.jpg.response'))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it 'downloads only the candidate with the largest width descriptor' do
|
|
103
|
+
html = '<img srcset="/small.jpg 400w, /100x100 1200w, /medium.jpg 800w">'
|
|
104
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
105
|
+
|
|
106
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
107
|
+
expect(page.images.with_size).to eq([['http://example.com/100x100', 100, 100]])
|
|
108
|
+
expect(page.images.to_a.size).to eq(3)
|
|
109
|
+
expect(WebMock).to have_requested(:get, 'http://example.com/100x100').once
|
|
110
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/small.jpg')
|
|
111
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/medium.jpg')
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it 'treats a picture as one image' do
|
|
115
|
+
html = <<~HTML
|
|
116
|
+
<picture>
|
|
117
|
+
<source media="(min-width: 800px)" srcset="/small.jpg 400w, /100x100 800w">
|
|
118
|
+
<img src="/fallback.jpg" srcset="/medium.jpg 2x">
|
|
119
|
+
</picture>
|
|
120
|
+
<img src="/10x10">
|
|
121
|
+
HTML
|
|
122
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
123
|
+
|
|
124
|
+
expect(page.images.with_size).to eq([
|
|
125
|
+
['http://example.com/100x100', 100, 100],
|
|
126
|
+
['http://example.com/10x10', 10, 10]
|
|
127
|
+
])
|
|
128
|
+
%w[small.jpg fallback.jpg medium.jpg].each do |path|
|
|
129
|
+
expect(WebMock).not_to have_requested(:get, "http://example.com/#{path}")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it 'downloads only the highest density candidate, counting src as 1x' do
|
|
134
|
+
html = '<img src="/small.jpg" srcset="/medium.jpg 1.5x, /100x100 2x">'
|
|
135
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
136
|
+
|
|
137
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
138
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/small.jpg')
|
|
139
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/medium.jpg')
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it 'keeps HTML dimensions when src is the ranked candidate' do
|
|
143
|
+
html = '<img src="/fallback.jpg" width="300" height="200" srcset="/fallback.jpg 1x, /small.jpg 0.5x">'
|
|
144
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
145
|
+
|
|
146
|
+
expect(page.images.with_size).to eq([['http://example.com/fallback.jpg', 300, 200]])
|
|
147
|
+
expect(WebMock).not_to have_requested(:get, %r{\Ahttp://example\.com/.+\.jpg\z})
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
92
151
|
describe "returns an Enumerable" do
|
|
93
152
|
let(:page) { MetaInspector.new('https://twitter.com/markupvalidator') }
|
|
94
153
|
|
|
@@ -294,6 +353,28 @@ describe MetaInspector do
|
|
|
294
353
|
stub_request(:get, 'http://example.com/remaining.jpg').to_return(status: 404)
|
|
295
354
|
end
|
|
296
355
|
|
|
356
|
+
describe "default" do
|
|
357
|
+
let(:many_images) { (1..25).map { |i| %(<img src="/many/#{i}.jpg">) }.join }
|
|
358
|
+
|
|
359
|
+
before { stub_request(:get, %r{\Ahttp://example\.com/many/}).to_return(status: 404) }
|
|
360
|
+
|
|
361
|
+
it "should download at most 20 images" do
|
|
362
|
+
page = MetaInspector.new('http://example.com/', document: many_images)
|
|
363
|
+
|
|
364
|
+
expect(page.images.with_size.size).to eq(25)
|
|
365
|
+
expect(WebMock).to have_requested(:get, %r{\Ahttp://example\.com/many/}).times(20)
|
|
366
|
+
expect(WebMock).to have_requested(:get, 'http://example.com/many/20.jpg').once
|
|
367
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/many/21.jpg')
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
it "should download every image when nil is passed explicitly" do
|
|
371
|
+
page = MetaInspector.new('http://example.com/', document: many_images, max_image_downloads: nil)
|
|
372
|
+
|
|
373
|
+
page.images.with_size
|
|
374
|
+
expect(WebMock).to have_requested(:get, %r{\Ahttp://example\.com/many/}).times(25)
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
|
|
297
378
|
it "should download only the first images needing dimensions in document order" do
|
|
298
379
|
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 2)
|
|
299
380
|
|