Skip to content

Commit

Permalink
Merge pull request #6 from rickpeyton/fake-api-client
Browse files Browse the repository at this point in the history
Fake api client and invalid request exception
  • Loading branch information
rickpeyton authored Jun 22, 2019
2 parents 0577177 + 3d31102 commit 4720d11
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 69 deletions.
12 changes: 7 additions & 5 deletions .rspec_status
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
example_id | status | run_time |
----------------------------------------- | ------ | --------------- |
./spec/nintendo_eshop/game_spec.rb[1:1:1] | passed | 0.00568 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:2] | passed | 0.0112 seconds |
./spec/nintendo_eshop_spec.rb[1:1] | passed | 0.00086 seconds |
example_id | status | run_time |
------------------------------------------------------ | ------ | --------------- |
./spec/acceptance/retrieve_a_valid_record_spec.rb[1:1] | passed | 0.11786 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:1] | passed | 0.00419 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:2] | passed | 0.00606 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:3] | passed | 0.00398 seconds |
./spec/nintendo_eshop_spec.rb[1:1] | passed | 0.00083 seconds |
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
nintendo_eshop (0.1.0.alpha2)
nintendo_eshop (0.1.0.alpha3)

GEM
remote: https://rubygems.org/
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ game.title # "Sonic Forces"
game.url # "/games/detail/sonic-forces-switch"
```

## Errors

Error handling is still a work in progress

```ruby
game = NintendoEshop::Game.retrieve("invalid id")

# => NintendoEshop::InvalidRequestError
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand All @@ -80,7 +90,14 @@ The gem is available as open source under the terms of the [MIT License](https:/

CircleCi is used to push releases to rubygems.org

To release, edit the version.rb file to the version you want to publish, commit that to your master branch, then create and push a git tag with the same name as your version:
To release

* Edit the version.rb file
* `bundle`
* Commit that to your master branch
* Create and push a git tag with the same name as your version

Example

```
git tag -a 0.1.0
Expand Down
8 changes: 8 additions & 0 deletions lib/nintendo_eshop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "json"
require "net/http"

require_relative "nintendo_eshop/api_client"
require_relative "nintendo_eshop/api_request"
require_relative "nintendo_eshop/game"
require_relative "nintendo_eshop/version"
Expand All @@ -13,7 +14,14 @@ class << self
attr_accessor :api_key
attr_accessor :app_id
attr_accessor :base_url

attr_writer :client

def client
@client ||= NintendoEshop::APIClient
end
end

class Error < StandardError; end
class InvalidRequestError < Error; end
end
21 changes: 21 additions & 0 deletions lib/nintendo_eshop/api_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module NintendoEshop
class APIClient
class << self
def post(uri, json: {})
http = setup_http(uri)
req = Net::HTTP::Post.new(uri)
req.add_field "Accept", "application/json"
req.add_field "Content-Type", "application/json"
req.body = JSON.dump(json)
http.request(req)
end

def setup_http(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http
end
end
end
end
21 changes: 6 additions & 15 deletions lib/nintendo_eshop/api_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,17 @@ class << self; end
def request(method, to_json: {})
case method
when :post
post(to_json: to_json)
post(json: to_json)
end
end

def post(to_json: {})
def post(json: {})
uri = URI("#{NintendoEshop.base_url}#{self.class::RESOURCE_PATH}?#{url_params}")
http = setup_http(uri)
req = Net::HTTP::Post.new(uri)
req.add_field "Accept", "application/json"
req.add_field "Content-Type", "application/json"
req.body = JSON.dump(to_json)
res = http.request(req)
JSON.parse(res.body, symbolize_names: true)
end
response = NintendoEshop.client.post(uri, json: json)
parsed_response = JSON.parse(response.body, symbolize_names: true)
raise InvalidRequestError, "ID not found" if parsed_response.dig(:nbHits).zero?

def setup_http(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http
parsed_response
end

def url_params
Expand Down
2 changes: 1 addition & 1 deletion lib/nintendo_eshop/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module NintendoEshop
VERSION = "0.1.0.alpha2".freeze
VERSION = "0.1.0.alpha3".freeze
end
15 changes: 15 additions & 0 deletions spec/acceptance/retrieve_a_valid_record_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RSpec.describe "Retrieve a valid game" do
it "can retrieve a game from the Nintendo API" do
WebMock.allow_net_connect!
NintendoEshop.client = NintendoEshop::APIClient

game = NintendoEshop::Game.retrieve("70010000001130")

expect(game).to be_a NintendoEshop::Game
expect(game.current_price).to eq(game.msrp).or eq(game.sale_price)
expect(game.sale_price).to be_a(Integer).or be_nil

NintendoEshop.client = NintendoEshop::FakeClient
WebMock.disable_net_connect!
end
end
19 changes: 19 additions & 0 deletions spec/fake_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module NintendoEshop
class FakeClient
class << self
def post(_uri, json: {})
response = case json.dig(:query)
when "70010000001539"
File.read("spec/http_responses/sonic_response.txt")
when "70010000001130"
File.read("spec/http_responses/mario_response.txt")
when "invalid"
File.read("spec/http_responses/invalid_response.txt")
end
OpenStruct.new(
body: response
)
end
end
end
end
1 change: 1 addition & 0 deletions spec/http_responses/invalid_response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hits":[],"nbHits":0,"page":0,"nbPages":0,"hitsPerPage":20,"processingTimeMS":1,"exhaustiveNbHits":true,"query":"invalid","params":"query=invalid&restrictSearchableAttributes=%5B%22nsuid%22%5D"}
File renamed without changes.
File renamed without changes.
28 changes: 0 additions & 28 deletions spec/http_stubs/http_stubs.rb

This file was deleted.

18 changes: 7 additions & 11 deletions spec/nintendo_eshop/game_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
RSpec.describe NintendoEshop::Game do
describe ".retrieve" do
it "retrieves a sale game from the Nintendo eShop API" do
extend HTTPStubs
stub_game_retrieve("sonic")
game = NintendoEshop::Game.retrieve("70010000001539")

expect(game).to be_a NintendoEshop::Game
Expand All @@ -17,19 +15,17 @@
end

it "retrieves a non-sale game from the Nintendo eShop API" do
extend HTTPStubs
stub_game_retrieve("mario")
game = NintendoEshop::Game.retrieve("70010000001130")

expect(game).to be_a NintendoEshop::Game
expect(game.art).to match(%r{/Switch_SuperMarioOdyssey_box.png})
expect(game.current_price).to eq 59.99
expect(game.id).to eq "70010000001130"
expect(game.msrp).to eq 59.99
expect(game.platform).to eq "Nintendo Switch"
expect(game.current_price).to eq game.msrp
expect(game.sale_price).to eq nil
expect(game.title).to eq "Super Mario Odyssey"
expect(game.url).to eq "/games/detail/super-mario-odyssey-switch"
end

it "raises an invalid request if a game is not found" do
game = -> { NintendoEshop::Game.retrieve("invalid") }

expect { game.call }.to raise_error(NintendoEshop::InvalidRequestError, "ID not found")
end
end
end
11 changes: 4 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
require "bundler/setup"
require "nintendo_eshop"

require_relative "http_stubs/http_stubs"
require_relative "./fake_client"

require "webmock/rspec"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.expect_with :rspec do |c|
c.syntax = :expect
end
end

def setup
NintendoEshop.base_url = "https://u3b6gr4ua3-dsn.algolia.net"
NintendoEshop.api_key = "9a20c93440cf63cf1a7008d75f7438bf"
NintendoEshop.app_id = "U3B6GR4UA3"
NintendoEshop.api_key = "9a20c93440cf63cf1a7008d75f7438bf"
NintendoEshop.base_url = "https://u3b6gr4ua3-dsn.algolia.net"
NintendoEshop.client = NintendoEshop::FakeClient
end

setup

0 comments on commit 4720d11

Please sign in to comment.