class Poke::API::Loader
Constants
- API_URL
- BATCH_SIZE
- CLIENT
Attributes
resource_path[RW]
Public Class Methods
new(resource_path)
click to toggle source
# File lib/poke/api/loader.rb, line 14 def initialize(resource_path) @resource_path = resource_path @cache = {} end
Public Instance Methods
all()
click to toggle source
# 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)
click to toggle source
# File lib/poke/api/loader.rb, line 19 def find(id) request "#{API_URL}/#{resource_path}/#{id}/" end
Private Instance Methods
request(url)
click to toggle source
# File lib/poke/api/loader.rb, line 41 def request(url) @cache[url] ||= begin response = CLIENT.get_content(url) JSON.parse(response) if response end end