Class: Embiggen::HtmlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/embiggen/html_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ HtmlClient

Returns a new instance of HtmlClient.



9
10
11
12
13
# File 'lib/embiggen/html_client.rb', line 9

def initialize(uri)
  @uri = uri
  @http = ::Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true if uri.scheme == 'https'
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/embiggen/html_client.rb', line 7

def uri
  @uri
end

Instance Method Details

#follow(timeout, selector) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/embiggen/html_client.rb', line 15

def follow(timeout, selector)
  response = request(timeout)
  return unless response.is_a?(::Net::HTTPOK)

  document = Nokogiri::HTML(response.body)
  element = document.at_css(selector)
  element&.[]('href')
rescue ::Timeout::Error => e
  raise NetworkError.new(
    "Timeout::Error: could not follow #{uri}: #{e.message}", uri
  )
rescue StandardError => e
  raise NetworkError.new(
    "StandardError: could not follow #{uri}: #{e.message}", uri
  )
end