class PokeAPI

Constants

BASE_URL
RESOURCES

Public Class Methods

new(format = :json) click to toggle source
# File lib/poke_api.rb, line 23
def initialize(format = :json)
  @format = format
end

Public Instance Methods

method_missing(method, *args) click to toggle source
# File lib/poke_api.rb, line 27
def method_missing(method, *args)
  if RESOURCES.include? method.to_s
    method = hyphenate(method.to_s)
    get(method.to_s, args[0], args[1])
  end
end

Private Instance Methods

get(resource, id = '', format = @format) click to toggle source
# File lib/poke_api.rb, line 36
def get(resource, id = '', format = @format)
  url = "#{BASE_URL}#{resource}/#{id}"
  response = RestClient.get(url)
  if format == :hash
    response = JSON.parse(response)
  elsif format == :ostruct
    response = OpenStruct.new(JSON.parse(response))
  end
  response
end
hyphenate(resource) click to toggle source
# File lib/poke_api.rb, line 47
def hyphenate(resource)
  resource.gsub('_', '-')
end