Class: MdnQuery::Search
- Inherits:
-
Object
- Object
- MdnQuery::Search
- Defined in:
- lib/mdn_query/search.rb
Overview
A search request to the Mozilla Developer Network documentation.
Instance Attribute Summary collapse
-
#css_classnames ⇒ String
A search option (see #initialize).
-
#highlight ⇒ String
A search option (see #initialize).
-
#html_attributes ⇒ String
A search option (see #initialize).
-
#locale ⇒ String
A search option (see #initialize).
-
#query ⇒ String
A search option (see #initialize).
-
#result ⇒ String
A search option (see #initialize).
-
#topics ⇒ String
A search option (see #initialize).
Instance Method Summary collapse
-
#execute ⇒ MdnQuery::SearchResult
Executes the search request.
-
#initialize(query, options = {}) ⇒ MdnQuery::Search
constructor
Creates a new search.
-
#next_page ⇒ MdnQuery::SearchResult?
Fetches the next page of the search result.
-
#open ⇒ void
Opens the search in the default web browser.
-
#previous_page ⇒ MdnQuery::SearchResult?
Fetches the previous page of the search result.
-
#url ⇒ String
Creates the URL used for the request.
Constructor Details
#initialize(query, options = {}) ⇒ MdnQuery::Search
Creates a new search.
The search request is not automatically executed (use #execute).
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mdn_query/search.rb', line 23 def initialize(query, = {}) @url = "#{MdnQuery::BASE_URL}.json" @query = query @css_classnames = [:css_classnames] @locale = [:locale] || 'en-US' @highlight = [:highlight] || false @html_attributes = [:html_attributes] @topics = [:topics] || ['js'] @result = nil end |
Instance Attribute Details
#css_classnames ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def css_classnames @css_classnames end |
#highlight ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def highlight @highlight end |
#html_attributes ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def html_attributes @html_attributes end |
#locale ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def locale @locale end |
#query ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def query @query end |
#result ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def result @result end |
#topics ⇒ String
Returns a search option (see #initialize).
5 6 7 |
# File 'lib/mdn_query/search.rb', line 5 def topics @topics end |
Instance Method Details
#execute ⇒ MdnQuery::SearchResult
Executes the search request.
54 55 56 |
# File 'lib/mdn_query/search.rb', line 54 def execute @result = MdnQuery::SearchResult.from_url(url) end |
#next_page ⇒ MdnQuery::SearchResult?
Fetches the next page of the search result.
If there is no search result yet, #execute will be called instead.
64 65 66 67 68 69 70 71 72 |
# File 'lib/mdn_query/search.rb', line 64 def next_page if @result.nil? execute elsif @result.next? query_url = url query_url << "&page=#{@result.current_page + 1}" @result = MdnQuery::SearchResult.from_url(query_url) end end |
#open ⇒ void
This method returns an undefined value.
Opens the search in the default web browser.
93 94 95 96 |
# File 'lib/mdn_query/search.rb', line 93 def open html_url = url.sub('.json?', '?') Launchy.open(html_url) end |
#previous_page ⇒ MdnQuery::SearchResult?
Fetches the previous page of the search result.
If there is no search result yet, #execute will be called instead.
80 81 82 83 84 85 86 87 88 |
# File 'lib/mdn_query/search.rb', line 80 def previous_page if @result.nil? execute elsif @result.previous? query_url = url query_url << "&page=#{@result.current_page - 1}" @result = MdnQuery::SearchResult.from_url(query_url) end end |
#url ⇒ String
Creates the URL used for the request.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mdn_query/search.rb', line 38 def url query_url = "#{@url}?q=#{@query}&locale=#{@locale}" query_url << @topics.map { |t| "&topic=#{t}" }.join unless @css_classnames.nil? query_url << "&css_classnames=#{@css_classnames}" end unless @html_attributes.nil? query_url << "&html_attributes=#{@html_attributes}" end query_url << "&highlight=#{@highlight}" unless @highlight.nil? query_url end |