Class: JsonApiReader::Fetcher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



3
4
5
6
# File 'lib/json_api_reader/fetcher.rb', line 3

def initialize(url, options={})
  @url     = url
  @options = options
end

Class Method Details

.fetch_all(url, options = {}) ⇒ Object



9
10
11
# File 'lib/json_api_reader/fetcher.rb', line 9

def fetch_all(url, options={})
  new(url, options).fetch_all
end

Instance Method Details

#fetch_allObject



14
15
16
17
18
19
20
21
# File 'lib/json_api_reader/fetcher.rb', line 14

def fetch_all
  response = HTTParty.get(@url, query: query, headers: headers)

  raise JsonApiReader::RecordNotFoundError.new("404 - URL: '#{@url}'") if 404 == response.code
  raise JsonApiReader::NotAuthorizedError.new("401 - URL: '#{@url}'") if 401 == response.code
  raise JsonApiReader::Error.new("#{response.code} returned from URL: '#{@url}'") if response.code >= 400
  JsonApiReader::PageResult.new JSON.parse(response.body)
end

#headersObject



23
24
25
26
27
# File 'lib/json_api_reader/fetcher.rb', line 23

def headers
  {'Content-Type' => 'application/json',
   'Accept'       => 'application/json'}.
      merge(@options[:headers] || {})
end

#queryObject



29
30
31
# File 'lib/json_api_reader/fetcher.rb', line 29

def query
  @options[:query]
end