Class: MdnQuery::SearchResult
- Inherits:
-
Object
- Object
- MdnQuery::SearchResult
- Defined in:
- lib/mdn_query/search_result.rb
Overview
A result from a search query.
Instance Attribute Summary collapse
-
#items ⇒ Array<Hash>
readonly
The raw items of the search result.
-
#pages ⇒ Hash
readonly
Information about the pages.
-
#query ⇒ String
readonly
The query that was searched for.
-
#total ⇒ Fixnum
readonly
The total number of entries.
Class Method Summary collapse
-
.from_url(url) ⇒ MdnQuery::SearchResult
Creates a search result with the results from the URL.
Instance Method Summary collapse
-
#current_page ⇒ Fixnum
Returns the number of the current page.
-
#empty? ⇒ Boolean
Returns whether there are any entries.
-
#initialize(json) ⇒ MdnQuery::SearchResult
constructor
Creates a new search result.
-
#next? ⇒ Boolean
Returns whether there is a next page.
-
#previous? ⇒ Boolean
Returns whether there is a previous page.
-
#to_list ⇒ MdnQuery::List
Creates a list with the items.
Constructor Details
#initialize(json) ⇒ MdnQuery::SearchResult
Creates a new search result.
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
#items ⇒ Array<Hash> (readonly)
Returns the raw items of the search result.
5 6 7 |
# File 'lib/mdn_query/search_result.rb', line 5 def items @items end |
#pages ⇒ Hash (readonly)
Returns information about the pages.
8 9 10 |
# File 'lib/mdn_query/search_result.rb', line 8 def pages @pages end |
#query ⇒ String (readonly)
Returns the query that was searched for.
11 12 13 |
# File 'lib/mdn_query/search_result.rb', line 11 def query @query end |
#total ⇒ Fixnum (readonly)
Returns 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.
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_page ⇒ Fixnum
Returns the number of the current page.
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.
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.
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.
63 64 65 |
# File 'lib/mdn_query/search_result.rb', line 63 def previous? !empty? && @pages[:current] > 1 end |