Class: JsonApiReader::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_reader/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, block) ⇒ Reader

Returns a new instance of Reader.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/json_api_reader/reader.rb', line 3

def initialize(options={}, block)
  raise ArgumentError.new("'endpoint' option is required") if options[:endpoint].nil?
  @url     = options[:endpoint]
  @options = options.clone
  @block   = block
end

Class Method Details

.all_pages(options = {}, &block) ⇒ Object



23
24
25
# File 'lib/json_api_reader/reader.rb', line 23

def all_pages(options={}, &block)
  new(options, block).all_pages
end

.first_page(options = {}, &block) ⇒ Object



27
28
29
# File 'lib/json_api_reader/reader.rb', line 27

def first_page(options={}, &block)
  new(options, block).fetch
end

Instance Method Details

#all_pagesObject



10
11
12
13
14
# File 'lib/json_api_reader/reader.rb', line 10

def all_pages
  while !@url.nil?
    @url = fetch.next_url
  end
end

#fetchObject



16
17
18
19
20
# File 'lib/json_api_reader/reader.rb', line 16

def fetch
  result = JsonApiReader::Fetcher.fetch_all(@url, @options)
  @block.call(result)
  result
end