Class: MdnQuery::Search

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

Overview

A search request to the Mozilla Developer Network documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ MdnQuery::Search

Creates a new search.

The search request is not automatically executed (use #execute).

Parameters:

Options Hash (options):

  • :css_classnames (String)

    the CSS classes to match

  • :highlight (Boolean)

    whether the query is highlighted

  • :html_attributes (String)

    the HTML attribute text to match

  • :locale (String)

    the locale to match against

  • :topics (Array<String>)

    the topics to search in



23
24
25
26
27
28
29
30
31
32
# File 'lib/mdn_query/search.rb', line 23

def initialize(query, options = {})
  @url = "#{MdnQuery::BASE_URL}.json"
  @query = query
  @css_classnames = options[:css_classnames]
  @locale = options[:locale] || 'en-US'
  @highlight = options[:highlight] || false
  @html_attributes = options[:html_attributes]
  @topics = options[:topics] || ['js']
  @result = nil
end

Instance Attribute Details

#css_classnamesString

Returns a search option (see #initialize).

Returns:



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

def css_classnames
  @css_classnames
end

#highlightString

Returns a search option (see #initialize).

Returns:



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

def highlight
  @highlight
end

#html_attributesString

Returns a search option (see #initialize).

Returns:



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

def html_attributes
  @html_attributes
end

#localeString

Returns a search option (see #initialize).

Returns:



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

def locale
  @locale
end

#queryString

Returns a search option (see #initialize).

Returns:



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

def query
  @query
end

#resultString

Returns a search option (see #initialize).

Returns:



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

def result
  @result
end

#topicsString

Returns a search option (see #initialize).

Returns:



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

def topics
  @topics
end

Instance Method Details

#executeMdnQuery::SearchResult

Executes the search request.

Returns:



54
55
56
# File 'lib/mdn_query/search.rb', line 54

def execute
  @result = MdnQuery::SearchResult.from_url(url)
end

#next_pageMdnQuery::SearchResult?

Fetches the next page of the search result.

If there is no search result yet, #execute will be called instead.

Returns:



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

#openvoid

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_pageMdnQuery::SearchResult?

Fetches the previous page of the search result.

If there is no search result yet, #execute will be called instead.

Returns:



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

#urlString

Creates the URL used for the request.

Returns:

  • (String)

    the full URL



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