-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7839f1
commit 8866bb2
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'net/http' | ||
|
||
module Pokeapi | ||
BASE_URI = "https://pokeapi.co/api/v2" | ||
|
||
class Resource | ||
def self.request resource | ||
uri = URI("#{BASE_URI}/#{resource}") | ||
response = Net::HTTP.get_response(uri) | ||
JSON.parse(response.body) | ||
end | ||
|
||
def self.get params = nil | ||
throw "You can't call Resource#get directly" if name.demodulize == 'Resource' | ||
|
||
request "#{name.demodulize.downcase}/#{params}" | ||
end | ||
|
||
def self.get_all | ||
next_url_params = nil | ||
results = [] | ||
|
||
loop do | ||
data = get next_url_params | ||
results += data["results"] | ||
|
||
next_url_params = if data["next"] then | ||
"?#{data["next"].split("?").try(:last)}" | ||
else | ||
nil | ||
end | ||
|
||
break unless next_url_params | ||
end | ||
|
||
results | ||
end | ||
end | ||
|
||
class Pokemon < Resource | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require "test_helper" | ||
require 'pokeapi' | ||
|
||
class PokeapiApiResourceTest < ActiveSupport::TestCase | ||
test "Resource#request" do | ||
VCR.use_cassette("pokeapi_resource_request") do | ||
response = Pokeapi::Resource.request 'pokemon' | ||
assert :success | ||
assert response["count"] > 0 | ||
end | ||
end | ||
|
||
test "Resource#get(params) throws an error" do | ||
VCR.use_cassette("pokeapi_resource_get") do | ||
assert_raises StandardError do | ||
response = Pokeapi::Resource.get 'pokemon' | ||
end | ||
end | ||
end | ||
|
||
test "Resource#get_all throws an error" do | ||
VCR.use_cassette("pokeapi_resource_get_all") do | ||
assert_raises StandardError do | ||
response = Pokeapi::Resource.get_all | ||
end | ||
end | ||
end | ||
end | ||
|
||
class PokeapiApiPokemonTest < ActiveSupport::TestCase | ||
test "Pokemon#get_all retrieves a list of pokemons from API" do | ||
VCR.use_cassette("pokeapi_pokemon_get_all") do | ||
response = Pokeapi::Pokemon.get_all | ||
assert :success | ||
end | ||
end | ||
|
||
test "Resource#get_all throws an error" do | ||
VCR.use_cassette("pokeapi_resource_get_1") do | ||
response = Pokeapi::Pokemon.get '1' | ||
assert response["id"] == 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters