Class: MdnQuery::Document
- Inherits:
-
Object
- Object
- MdnQuery::Document
- Defined in:
- lib/mdn_query/document.rb
Overview
A document of an entry of the Mozilla Developer Network documentation.
Instance Attribute Summary collapse
-
#section ⇒ MdnQuery::Section
readonly
The top level section.
- #title ⇒ String readonly
- #url ⇒ String readonly
Class Method Summary collapse
-
.from_url(url) ⇒ MdnQuery::Document
Creates a document filled with the content of the URL.
Instance Method Summary collapse
-
#initialize(title, url = nil) ⇒ MdnQuery::Document
constructor
Creates a new document with an initial top level section.
-
#open ⇒ void
Opens the document in the default web browser if a URL has been specified.
-
#to_s ⇒ String
(also: #to_md)
Returns the string representation of the document.
Constructor Details
#initialize(title, url = nil) ⇒ MdnQuery::Document
Creates a new document with an initial top level section.
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
#section ⇒ MdnQuery::Section (readonly)
Returns the top level section.
8 9 10 |
# File 'lib/mdn_query/document.rb', line 8 def section @section end |
#title ⇒ String (readonly)
5 6 7 |
# File 'lib/mdn_query/document.rb', line 5 def title @title end |
#url ⇒ String (readonly)
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.
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
#open ⇒ void
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_s ⇒ String Also known as: to_md
Returns the string representation of the document.
51 52 53 |
# File 'lib/mdn_query/document.rb', line 51 def to_s @section.to_s end |