Class: Nytimes::Events::List

Inherits:
Base
  • Object
show all
Defined in:
lib/nytimes-events/list.rb

Constant Summary

Constants inherited from Base

Base::API_NAME, Base::API_OBJECTS, Base::API_SERVER, Base::API_URL, Base::API_VERSION, Base::RESPONSE_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_key, #api_key=, #api_url

Constructor Details

#initialize(api_key, batch_size = 20) ⇒ List

Returns a new instance of List.



6
7
8
9
10
11
12
# File 'lib/nytimes-events/list.rb', line 6

def initialize(api_key, batch_size = 20)
  super(api_key)
  @batch_size = batch_size
  @current_offset = 0
  @results = 0
  @previous_params = Hash.new
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



4
5
6
# File 'lib/nytimes-events/list.rb', line 4

def batch_size
  @batch_size
end

Instance Method Details

#find(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/nytimes-events/list.rb', line 14

def find(params = {})
  to_head if previous_params_differ_from(params)

  @previous_params = params
  response = RestClient.get(api_url_for(params))

  case response_code_for response
  when 403
    raise RestClient::Exception, "Access forbidden by NYTimes API. Perhaps the API key isn't working?"
  when 200
    json = JSON.parse(response)
    set_results_returned(json)
    json
  end
end

#next_pageObject Also known as: next



34
35
36
37
# File 'lib/nytimes-events/list.rb', line 34

def next_page
  @current_offset += @batch_size unless @current_offset + @batch_size > @results
  find(@previous_params)
end

#prev_pageObject Also known as: previous



40
41
42
43
# File 'lib/nytimes-events/list.rb', line 40

def prev_page
  @current_offset -= @batch_size unless @batch_size > @current_offset
  find(@previous_params)
end

#to_headObject



46
47
48
# File 'lib/nytimes-events/list.rb', line 46

def to_head
  @current_offset = 0
end