Class: OpenGraphFetcher::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/open_graph_fetcher/fetcher.rb

Constant Summary collapse

OG_PROPERTIES =
%w[title type image url description site_name].freeze
DNS_TIMEOUT =
3
OPEN_TIMEOUT =
3
READ_TIMEOUT =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Fetcher

Returns a new instance of Fetcher.



18
19
20
# File 'lib/open_graph_fetcher/fetcher.rb', line 18

def initialize(url)
  @url = url
end

Class Method Details

.fetch(url) ⇒ Object



14
15
16
# File 'lib/open_graph_fetcher/fetcher.rb', line 14

def self.fetch(url)
  new(url).fetch
end

Instance Method Details

#fetchObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/open_graph_fetcher/fetcher.rb', line 22

def fetch
  uri = parse_uri(@url)
  raise InvalidSchemeError, "Only HTTPS URLs are allowed" unless uri.scheme == "https"
  raise InvalidPortError, "Only the default HTTPS port (443) is allowed" unless uri.port == 443
  raise InvalidHostError, "Using an IP as host is not allowed" if ip_address?(uri.hostname)

  ip_address = resolve_ip(uri)
  raise PrivateIPError, "Resolved IP address is in a private or reserved range" if private_ip?(ip_address)

  response = fetch_data(uri, ip_address)
  raise ResponseError, "HTTP response is not ok: HTTP #{response.code} #{response.message}" unless response.code == "200"
  raise InvalidContentTypeError, "Only HTML content is allowed" unless html_content?(response)
  
  parse_open_graph_data(response.body)
end