Module: Pokegem
- Defined in:
- lib/pokegem.rb,
lib/pokegem/version.rb
Constant Summary collapse
- BASE_URL =
'http://pokeapi.co/api/v1'
- RESOURCES =
%w(pokedex pokemon move ability type egg description sprite game)
- VERSION =
"0.0.3"
Class Method Summary collapse
Class Method Details
.get(resource, n) ⇒ Object
12 13 14 15 16 |
# File 'lib/pokegem.rb', line 12 def get(resource, n) raise "Invalid resource, select from #{RESOURCES.join(', ')}" unless RESOURCES.include?(resource) (@cache ||= init_hash)[resource][n] ||= Typhoeus.get("#{BASE_URL}/#{resource}/#{n}", followlocation: true).[:response_body] end |
.get_obj(resource, n) ⇒ Object
18 19 20 |
# File 'lib/pokegem.rb', line 18 def get_obj(resource, n) (@obj_cache ||= init_hash)[resource][n] ||= OpenStruct.new(JSON.parse(get(resource, n))) end |
.init_hash ⇒ Object
10 |
# File 'lib/pokegem.rb', line 10 def init_hash; RESOURCES.reduce({}) { |h, r| h.merge! r => {} } end |