Class: Nytimes::Articles::ResultSet

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nytimes_articles/result_set.rb

Overview

The ResultSet is returned by Article#search and contains an array of up to 10 results out of the total matches. For your convenience, this object provides a selection of array methods on the underlying collection of articles.

Constant Summary collapse

BATCH_SIZE =
10

Constants inherited from Base

Base::API_BASE, Base::API_NAME, Base::API_SERVER, Base::API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key, api_key=, boolean_field, build_request_url, date_field, debug=, decode_html_entities=, integer_field, invoke, text_field

Constructor Details

#initialize(params) ⇒ ResultSet

Returns a new instance of ResultSet.



34
35
36
37
38
39
# File 'lib/nytimes_articles/result_set.rb', line 34

def initialize(params)
	@offset = params[:offset]
	@total_results = params[:total_results]
	@results = params[:results]
	@facets = FacetHash.init_from_api(params[:facets])
end

Instance Attribute Details

#facetsObject (readonly)

If you have specified a list of :facets for Article#search, they will be returned in a hash keyed by the facet name here.



28
29
30
# File 'lib/nytimes_articles/result_set.rb', line 28

def facets
  @facets
end

#offsetObject (readonly)

The offset of the result_set. Note that this is essentially the ordinal position of the batch among all results. First 10 results are offset 0, the next 10 are offset 1, etc.



15
16
17
# File 'lib/nytimes_articles/result_set.rb', line 15

def offset
  @offset
end

#resultsObject (readonly)

The results array of articles returned. Note that if you call Articles#find with :fields => :none, this will return nil even if there are matching results.



24
25
26
# File 'lib/nytimes_articles/result_set.rb', line 24

def results
  @results
end

#total_resultsObject (readonly)

The total results that matched the query.



19
20
21
# File 'lib/nytimes_articles/result_set.rb', line 19

def total_results
  @total_results
end

Class Method Details

.init_from_api(api_hash) ⇒ Object

Used to initialize a new result_set from Article#search.



57
58
59
60
61
62
63
# File 'lib/nytimes_articles/result_set.rb', line 57

def self.init_from_api(api_hash)
	self.new(:offset => integer_field(api_hash['offset']),
					 :total_results => integer_field(api_hash['total']),
					 :results => api_hash['results'].map {|r| Article.init_from_api(r)},
					 :facets => Facet.init_from_api(api_hash['facets'])
					)
end

Instance Method Details

#page_numberObject

For your convenience, the page_number method is an alternate version of #offset that counts up from 1.



43
44
45
46
# File 'lib/nytimes_articles/result_set.rb', line 43

def page_number
	return 0 if @total_results == 0
	@offset + 1
end

#total_pagesObject

Calculates the total number of pages in the results based on the standard batch size and total results.



50
51
52
53
# File 'lib/nytimes_articles/result_set.rb', line 50

def total_pages
	return 0 if @total_results == 0
	(@total_results.to_f / BATCH_SIZE).ceil
end