Skip to content

Commit

Permalink
Merge pull request #7 from rickpeyton/list_games_by_interface
Browse files Browse the repository at this point in the history
Add a GamesList.by... interface
  • Loading branch information
rickpeyton authored Jun 25, 2019
2 parents 4720d11 + 0e8231d commit e7eb8bb
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: "2"
prepare:
fetch:
- url: "https://raw.githubusercontent.com/RamseyInHouse/ramsey-cop/master/default.yml"
path: "alternate-rubocop-path.yml"
# prepare:
# fetch:
# - url: "https://raw.githubusercontent.com/RamseyInHouse/ramsey-cop/master/default.yml"
# path: "alternate-rubocop-path.yml"
plugins:
rubocop:
enabled: true
Expand Down
12 changes: 7 additions & 5 deletions .rspec_status
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 |
./spec/acceptance/retrieve_a_valid_record_spec.rb[1:1] | passed | 0.14289 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:1] | passed | 0.00375 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:2] | passed | 0.00616 seconds |
./spec/nintendo_eshop/game_spec.rb[1:1:3] | passed | 0.00564 seconds |
./spec/nintendo_eshop/games_list_spec.rb[1:1] | passed | 0.00254 seconds |
./spec/nintendo_eshop/games_list_spec.rb[1:2:1] | passed | 0.00393 seconds |
./spec/nintendo_eshop_spec.rb[1:1] | passed | 0.00331 seconds |
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
inherit_gem:
ramsey_cop:
- default.yml

AllCops:
TargetRubyVersion: 2.4
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.alpha3)
nintendo_eshop (0.1.0.alpha4)

GEM
remote: https://rubygems.org/
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ NintendoEshop.base_url = "https://u3b6gr4ua3-dsn.algolia.net"
NintendoEshop.api_key = "9a20c93440cf63cf1a7008d75f7438bf"
NintendoEshop.app_id = "U3B6GR4UA3"

### Retrieve a game by ID

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

game.art # "/content/dam/noa/en_US/games/switch/s/super-mario-odyssey-switch/Switch_SuperMarioOdyssey_box.png"
Expand Down Expand Up @@ -62,6 +64,14 @@ game.title # "Sonic Forces"
game.url # "/games/detail/sonic-forces-switch"
```

### Search by Title

```ruby
games = NintendoEshop::GamesList.by_title("Super Mario Odyssey")

games.first.title # "Super Mario Odyssey: Starter"
```

## Errors

Error handling is still a work in progress
Expand Down
90 changes: 90 additions & 0 deletions alternate-rubocop-path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require: rubocop-performance

AllCops:
Exclude:
- db/schema.rb
- vendor/**/*
TargetRubyVersion: 2.4.6

Documentation:
Enabled: false

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Metrics/AbcSize:
Exclude:
- spec/**/*
- test/**/*

Metrics/BlockLength:
Exclude:
- config/initializers/*
- config/routes.rb
- Gemfile
- spec/**/*
- test/**/*
- "*.gemspec"
- "**/*.rake"

Metrics/BlockNesting:
Exclude:
- spec/**/*
- test/**/*

Metrics/CyclomaticComplexity:
Exclude:
- spec/**/*
- test/**/*

Metrics/LineLength:
Max: 120
Exclude:
- config/routes.rb
- config/routes/*
- db/migrate/*

Metrics/MethodLength:
Exclude:
- db/migrate/*
- spec/**/*
- test/**/*

Metrics/ModuleLength:
Exclude:
- spec/**/*
- test/**/*

Style/Alias:
EnforcedStyle: prefer_alias_method

Style/AndOr:
EnforcedStyle: conditionals

Style/DateTime:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/Lambda:
Enabled: false

Style/NumericLiterals:
Enabled: false

Style/PercentLiteralDelimiters:
PreferredDelimiters:
'%i': ()
'%I': ()
'%q': ()
'%Q': ()
'%r': '{}'
'%s': ()
'%w': ()
'%W': ()
'%x': ()

Style/StringLiterals:
EnforcedStyle: double_quotes
Enabled: true
1 change: 1 addition & 0 deletions lib/nintendo_eshop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "nintendo_eshop/api_client"
require_relative "nintendo_eshop/api_request"
require_relative "nintendo_eshop/game"
require_relative "nintendo_eshop/games_list"
require_relative "nintendo_eshop/version"

module NintendoEshop
Expand Down
56 changes: 56 additions & 0 deletions lib/nintendo_eshop/games_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module NintendoEshop
class GamesList < APIRequest
include Enumerable

attr_accessor :title
attr_accessor :games

RESOURCE_PATH = "/1/indexes/noa_aem_game_en_us/query".freeze

def initialize(title)
self.title = title
end

def refresh
response = request(:post, to_json: body)
result = response.dig(:hits)
self.games = refresh_list_objects(result)
self
end

def each
games.each do |game|
yield(game)
end
end

def self.by_title(title)
instance = new(title)
instance.refresh
end

private

def body
{
"query": title.to_s,
"restrictSearchableAttributes": [
"title"
]
}
end

def refresh_list_objects(objects)
objects.map do |object|
game = Game.new(object.dig(:nsuid))
game.art = object.dig(:boxArt)
game.msrp = object.dig(:msrp)
game.platform = object.dig(:platform)
game.sale_price = object.dig(:salePrice)
game.title = object.dig(:title)
game.url = object.dig(:url)
game
end
end
end
end
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.alpha3".freeze
VERSION = "0.1.0.alpha4".freeze
end
16 changes: 13 additions & 3 deletions nintendo_eshop.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]

spec.summary = "Retrieve game prices from the Nintendo eShop"
spec.homepage = "https://github.com/rickpeyton/nintendo_eshop"
spec.license = "MIT"
spec.description = <<~DESCRIPTION
When I want to check the price of a game on the Nintendo eShop I want to search Nintendo's
API by external key so that I am confident I am getting the correct price.
DESCRIPTION
spec.homepage = "https://github.com/rickpeyton/nintendo_eshop"
spec.metadata = {
"changelog_uri" => "https://github.com/rickpeyton/nintendo_eshop/releases",
"homepage_uri" => "https://github.com/rickpeyton/nintendo_eshop",
"source_code_uri" => "https://github.com/rickpeyton/nintendo_eshop"
}
spec.license = "MIT"
spec.required_ruby_version = ">= 2.4.6"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = "exe"
Expand Down
2 changes: 2 additions & 0 deletions spec/fake_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def post(_uri, json: {})
File.read("spec/http_responses/mario_response.txt")
when "invalid"
File.read("spec/http_responses/invalid_response.txt")
when "Mario"
File.read("spec/http_responses/mario_games_list_response.txt")
end
OpenStruct.new(
body: response
Expand Down
1 change: 1 addition & 0 deletions spec/http_responses/mario_games_list_response.txt

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions spec/nintendo_eshop/games_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RSpec.describe NintendoEshop::GamesList do
it "includes enumerable" do
expect(NintendoEshop::GamesList).to include(Enumerable)
end

describe ".by_title" do
it "returns a list of games" do
actual = NintendoEshop::GamesList.by_title("Mario")
first_game = actual.first

expect(actual).to be_a(NintendoEshop::GamesList)
expect(first_game.title).to eq("Dr. Mario World")
end
end
end

0 comments on commit e7eb8bb

Please sign in to comment.