Method: MdnQuery::Document.from_url

Defined in:
lib/mdn_query/document.rb

.from_url(url) ⇒ MdnQuery::Document

Creates a document filled with the content of the URL.

Parameters:

  • url (String)

    the URL to the document on the web

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mdn_query/document.rb', line 14

def self.from_url(url)
  begin
    response = RestClient::Request.execute(method: :get, url: url,
                                           headers: { accept: 'text/html' })
  rescue RestClient::Exception, SocketError => e
    raise MdnQuery::HttpRequestFailed.new(url, e),
          'Could not retrieve entry'
  end
  dom = Nokogiri::HTML(response.body)
  title = dom.css('h1').text
  article = dom.css('article')
  document = new(title, url)
  MdnQuery::TraverseDom.fill_document(article, document)
  document
end