open_graph_fetcher 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c8d1c67970ce91969c7caa437fba256fc1527f46006a10b8576bcd2f52fc774
4
- data.tar.gz: 8e994a0fd3b384367c42ab93b73bd9185fa506e1f2af6a702a125bc26314b2a6
3
+ metadata.gz: 48a6853f8f4665764d603a012713c1e8a5cd44c9ad649efc7626b2e210bf0931
4
+ data.tar.gz: 53ef6ecb11ab043f4d905a659a834254169721d6018b3a7428f535ceaad635fe
5
5
  SHA512:
6
- metadata.gz: b6d51a0157d6b3088a8f6a8f4bb31243736300fc6bc891ecaafb6b4aa2907236b0b1e45f4e1a22c40c935a4287e3b9ada4509e40c37bbef466c1ce9ae5da2cbe
7
- data.tar.gz: 678eab6e764a9ad9fc85c8e09a5b5918409f88a5fc293022c9fa847e3283bd0bc90a37700dd87c36d474566ce13047e7b57a1d8524e635b3ad54952d659ab7bb
6
+ metadata.gz: aa81fcbfff70cb71a13c599ceaf4be687e97e38d2889424290fd0b6c7487c967575fae28864d47cc46988d164ed18894df257c2a6838a2c55e7d40d7b07c3318
7
+ data.tar.gz: 75e7edea3e478392eccfd6fdde4f25e8b33d711d539746f09dd63543a15faaaae1dd4fbb621348e3cd7ec4ad7d1a5042dd6b7a88cda35e118203e19adeb25460
@@ -1,6 +1,7 @@
1
1
  module OpenGraphFetcher
2
2
  class Error < StandardError; end
3
3
 
4
+ class InvalidURIError < Error; end
4
5
  class InvalidSchemeError < Error; end
5
6
  class InvalidPortError < Error; end
6
7
  class InvalidHostError < Error; end
@@ -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 = URI.parse(@url)
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" if uri.port && uri.port != 443
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.start(uri.hostname, uri.port, ipaddr: ip, use_ssl: true, open_timeout: OPEN_TIMEOUT, read_timeout: READ_TIMEOUT) do |http|
61
- http.request(request)
62
- end
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
@@ -1,3 +1,3 @@
1
1
  module OpenGraphFetcher
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.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-04 00:00:00.000000000 Z
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.5.16
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: []