Class: MdnQuery::SearchResult

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

Overview

A result from a search query.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ MdnQuery::SearchResult

Creates a new search result.

Parameters:

  • json (Hash)

    the hash version of the JSON response



36
37
38
39
40
41
42
43
44
# File 'lib/mdn_query/search_result.rb', line 36

def initialize(json)
  @query = json[:query]
  @pages = {
    count: json[:pages] || 0,
    current: json[:page]
  }
  @total = json[:count]
  @items = json[:documents]
end

Instance Attribute Details

#itemsArray<Hash> (readonly)

Returns the raw items of the search result.

Returns:

  • (Array<Hash>)

    the raw items of the search result



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

def items
  @items
end

#pagesHash (readonly)

Returns information about the pages.

Returns:

  • (Hash)

    information about the pages



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

def pages
  @pages
end

#queryString (readonly)

Returns the query that was searched for.

Returns:

  • (String)

    the query that was searched for



11
12
13
# File 'lib/mdn_query/search_result.rb', line 11

def query
  @query
end

#totalFixnum (readonly)

Returns the total number of entries.

Returns:

  • (Fixnum)

    the total number of entries



14
15
16
# File 'lib/mdn_query/search_result.rb', line 14

def total
  @total
end

Class Method Details

.from_url(url) ⇒ MdnQuery::SearchResult

Creates a search result with the results from the URL.

Parameters:

  • url (String)

    the URL to the search result on the web

Returns:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mdn_query/search_result.rb', line 20

def self.from_url(url)
  begin
    response = RestClient::Request.execute(method: :get, url: url,
                                           headers: { accept: 'json' })
  rescue RestClient::Exception, SocketError => e
    raise MdnQuery::HttpRequestFailed.new(url, e),
          'Could not retrieve search result'
  end
  json = JSON.parse(response.body, symbolize_names: true)
  new(json)
end

Instance Method Details

#current_pageFixnum

Returns the number of the current page.

Returns:

  • (Fixnum)


70
71
72
# File 'lib/mdn_query/search_result.rb', line 70

def current_page
  @pages[:current]
end

#empty?Boolean

Returns whether there are any entries.

Returns:

  • (Boolean)


49
50
51
# File 'lib/mdn_query/search_result.rb', line 49

def empty?
  @pages[:count].zero?
end

#next?Boolean

Returns whether there is a next page.

Returns:

  • (Boolean)


56
57
58
# File 'lib/mdn_query/search_result.rb', line 56

def next?
  !empty? && @pages[:current] < @pages[:count]
end

#previous?Boolean

Returns whether there is a previous page.

Returns:

  • (Boolean)


63
64
65
# File 'lib/mdn_query/search_result.rb', line 63

def previous?
  !empty? && @pages[:current] > 1
end

#to_listMdnQuery::List

Creates a list with the items.

Returns:



77
78
79
80
81
82
# File 'lib/mdn_query/search_result.rb', line 77

def to_list
  items = @items.map do |i|
    MdnQuery::Entry.new(i[:title], i[:excerpt], i[:url])
  end
  MdnQuery::List.new(query, *items)
end