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

Instance Method Summary collapse

Constructor Details

#initialize(query, json) ⇒ MdnQuery::SearchResult

Creates a new search result.

Parameters:

  • query (String)

    the query that was searched for

  • json (Hash)

    the hash version of the JSON response



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

def initialize(query, json)
  @query = 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

Instance Method Details

#current_pageFixnum

Returns the number of the current page.

Returns:

  • (Fixnum)


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

def current_page
  @pages[:current]
end

#empty?Boolean

Returns whether there are any entries.

Returns:

  • (Boolean)


34
35
36
# File 'lib/mdn_query/search_result.rb', line 34

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

#next?Boolean

Returns whether there is a next page.

Returns:

  • (Boolean)


41
42
43
# File 'lib/mdn_query/search_result.rb', line 41

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

#previous?Boolean

Returns whether there is a previous page.

Returns:

  • (Boolean)


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

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

#to_listMdnQuery::List

Creates a list with the items.

Returns:



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

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