Class: MdnQuery::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/mdn_query/document.rb

Overview

A document of an entry of the Mozilla Developer Network documentation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, url = nil) ⇒ MdnQuery::Document

Creates a new document with an initial top level section.

Parameters:

  • title (String)

    the titel of the top level section

  • url (String) (defaults to: nil)

    the URL to the document on the web



35
36
37
38
39
# File 'lib/mdn_query/document.rb', line 35

def initialize(title, url = nil)
  @title = title
  @url = url
  @section = MdnQuery::Section.new(title)
end

Instance Attribute Details

#sectionMdnQuery::Section (readonly)

Returns the top level section.

Returns:



8
9
10
# File 'lib/mdn_query/document.rb', line 8

def section
  @section
end

#titleString (readonly)

Returns:

  • (String)


5
6
7
# File 'lib/mdn_query/document.rb', line 5

def title
  @title
end

#urlString (readonly)

Returns:

  • (String)


5
6
7
# File 'lib/mdn_query/document.rb', line 5

def url
  @url
end

Class Method Details

.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

Instance Method Details

#openvoid

This method returns an undefined value.

Opens the document in the default web browser if a URL has been specified.



44
45
46
# File 'lib/mdn_query/document.rb', line 44

def open
  Launchy.open(@url) unless @url.nil?
end

#to_sString Also known as: to_md

Returns the string representation of the document.

Returns:

  • (String)


51
52
53
# File 'lib/mdn_query/document.rb', line 51

def to_s
  @section.to_s
end