Class: Poke::API::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/poke/api/loader.rb

Constant Summary collapse

API_URL =
"http://pokeapi.co/api/v1".freeze
CLIENT =
HTTPClient.new
BATCH_SIZE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_path) ⇒ Loader

Returns a new instance of Loader.



14
15
16
17
# File 'lib/poke/api/loader.rb', line 14

def initialize(resource_path)
  @resource_path = resource_path
  @cache = {}
end

Instance Attribute Details

#resource_pathObject

Returns the value of attribute resource_path.



12
13
14
# File 'lib/poke/api/loader.rb', line 12

def resource_path
  @resource_path
end

Instance Method Details

#allObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/poke/api/loader.rb', line 23

def all
  base_url = "#{API_URL}/#{resource_path}/"
  limit = BATCH_SIZE
  offset = 0
  objects = []

  begin
    url = "#{base_url}?limit=#{limit}&offset=#{offset}"
    response = request(url)
    objects += response["objects"]
    offset += limit
  end while response["meta"]["next"]

  objects
end

#find(id) ⇒ Object



19
20
21
# File 'lib/poke/api/loader.rb', line 19

def find(id)
  request "#{API_URL}/#{resource_path}/#{id}/"
end