Class: Poke::API::Loader
- Inherits:
-
Object
- Object
- Poke::API::Loader
- 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
-
#resource_path ⇒ Object
Returns the value of attribute resource_path.
Instance Method Summary collapse
- #all ⇒ Object
- #find(id) ⇒ Object
-
#initialize(resource_path) ⇒ Loader
constructor
A new instance of Loader.
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_path ⇒ Object
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
#all ⇒ Object
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 |