open_graph_fetcher 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/open_graph_fetcher/errors.rb +1 -0
- data/lib/open_graph_fetcher/fetcher.rb +15 -6
- data/lib/open_graph_fetcher/version.rb +1 -1
- data/spec/fetcher_spec.rb +22 -0
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48a6853f8f4665764d603a012713c1e8a5cd44c9ad649efc7626b2e210bf0931
|
4
|
+
data.tar.gz: 53ef6ecb11ab043f4d905a659a834254169721d6018b3a7428f535ceaad635fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa81fcbfff70cb71a13c599ceaf4be687e97e38d2889424290fd0b6c7487c967575fae28864d47cc46988d164ed18894df257c2a6838a2c55e7d40d7b07c3318
|
7
|
+
data.tar.gz: 75e7edea3e478392eccfd6fdde4f25e8b33d711d539746f09dd63543a15faaaae1dd4fbb621348e3cd7ec4ad7d1a5042dd6b7a88cda35e118203e19adeb25460
|
@@ -5,7 +5,7 @@ require 'ipaddr'
|
|
5
5
|
|
6
6
|
module OpenGraphFetcher
|
7
7
|
class Fetcher
|
8
|
-
OG_PROPERTIES = %w[title type image url description].freeze
|
8
|
+
OG_PROPERTIES = %w[title type image url description site_name].freeze
|
9
9
|
|
10
10
|
DNS_TIMEOUT = 3
|
11
11
|
OPEN_TIMEOUT = 3
|
@@ -20,9 +20,9 @@ module OpenGraphFetcher
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def fetch
|
23
|
-
uri =
|
23
|
+
uri = parse_uri(@url)
|
24
24
|
raise InvalidSchemeError, "Only HTTPS URLs are allowed" unless uri.scheme == "https"
|
25
|
-
raise InvalidPortError, "Only the default HTTPS port (443) is allowed"
|
25
|
+
raise InvalidPortError, "Only the default HTTPS port (443) is allowed" unless uri.port == 443
|
26
26
|
raise InvalidHostError, "Using an IP as host is not allowed" if ip_address?(uri.hostname)
|
27
27
|
|
28
28
|
ip_address = resolve_ip(uri)
|
@@ -36,6 +36,12 @@ module OpenGraphFetcher
|
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
39
|
+
|
40
|
+
def parse_uri(url)
|
41
|
+
URI.parse(url)
|
42
|
+
rescue URI::InvalidURIError => e
|
43
|
+
raise InvalidURIError, "Could not parse URI: #{e.message}"
|
44
|
+
end
|
39
45
|
|
40
46
|
def resolve_ip(uri)
|
41
47
|
Resolv::DNS.open do |dns|
|
@@ -57,9 +63,12 @@ module OpenGraphFetcher
|
|
57
63
|
|
58
64
|
def fetch_data(uri, ip)
|
59
65
|
request = Net::HTTP::Get.new(uri.request_uri)
|
60
|
-
Net::HTTP.
|
61
|
-
|
62
|
-
|
66
|
+
http = Net::HTTP.new(uri.hostname, 443)
|
67
|
+
http.ipaddr = ip
|
68
|
+
http.use_ssl = true
|
69
|
+
http.open_timeout = OPEN_TIMEOUT
|
70
|
+
http.read_timeout = READ_TIMEOUT
|
71
|
+
http.request(request)
|
63
72
|
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
64
73
|
raise FetchError, "Request timed out: #{e.message}"
|
65
74
|
rescue StandardError => e
|
data/spec/fetcher_spec.rb
CHANGED
@@ -38,6 +38,28 @@ RSpec.describe OpenGraphFetcher::Fetcher do
|
|
38
38
|
"description" => "Example description"
|
39
39
|
})
|
40
40
|
end
|
41
|
+
|
42
|
+
it 'sets the ipaddr and other options on the Net::HTTP instance' do
|
43
|
+
http_instance = instance_double(Net::HTTP)
|
44
|
+
expect(Net::HTTP).to receive(:new).and_return(http_instance)
|
45
|
+
|
46
|
+
expect(http_instance).to receive(:ipaddr=).with("203.0.113.0")
|
47
|
+
expect(http_instance).to receive(:use_ssl=).with(true)
|
48
|
+
expect(http_instance).to receive(:open_timeout=).with(3)
|
49
|
+
expect(http_instance).to receive(:read_timeout=).with(3)
|
50
|
+
|
51
|
+
response_double = double("Net::HTTPResponse", code: "200", body: "<!DOCTYPE html><title>OK</title>")
|
52
|
+
expect(response_double).to receive(:[]).with("Content-Type").and_return("text/html")
|
53
|
+
expect(http_instance).to receive(:request).and_return(response_double)
|
54
|
+
|
55
|
+
OpenGraphFetcher::Fetcher.fetch("https://example.com")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when given an invalid URL" do
|
60
|
+
it "raises an InvalidURIError" do
|
61
|
+
expect { OpenGraphFetcher::Fetcher.fetch("# test") }.to raise_error(OpenGraphFetcher::InvalidURIError)
|
62
|
+
end
|
41
63
|
end
|
42
64
|
|
43
65
|
context "when given an HTTP URL" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_graph_fetcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marco Colli
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -52,8 +52,8 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
56
|
-
email:
|
55
|
+
description:
|
56
|
+
email:
|
57
57
|
executables: []
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
@@ -74,7 +74,7 @@ homepage: https://github.com/collimarco/open_graph_fetcher
|
|
74
74
|
licenses:
|
75
75
|
- MIT
|
76
76
|
metadata: {}
|
77
|
-
post_install_message:
|
77
|
+
post_install_message:
|
78
78
|
rdoc_options: []
|
79
79
|
require_paths:
|
80
80
|
- lib
|
@@ -89,8 +89,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
|
-
rubygems_version: 3.
|
93
|
-
signing_key:
|
92
|
+
rubygems_version: 3.0.3.1
|
93
|
+
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: Fetch Open Graph metadata in a safer way.
|
96
96
|
test_files: []
|