gwitch 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5d7c8e1ed024ba5333231113ae82e55743789dbf45eef7c5d7fd3fdcd50caeb
4
- data.tar.gz: dc6eba3677e8fcf0ee4038dfc275398b2ddc31e1fd1a70793c7505fc2eb60298
3
+ metadata.gz: 82b6169c6117ff674bb11ac60e62d9eadaa6445755e6b6ebc0c1ea23d62a0254
4
+ data.tar.gz: 1b83dd4d0243c328917d997f6c9463cc4b464711560172d8cf2afb6d45167a1e
5
5
  SHA512:
6
- metadata.gz: 1e78faf779696cf21b962d04085eb0dea98f14a9c2a9baca056109f12a0c3099c370fb12c43f4e02a74aad7a1b20f8af82a585ce84f7f14ca5be23b587e6bb85
7
- data.tar.gz: 9f5d471a7c462a01744eafa8560dddc82edb7f8fc864ddd36b6ce450d8741c27d70844c912540ca375048ab96055ee4a81aa567dd1816d2b27f49ab9fad5e0ad
6
+ metadata.gz: eff925c25baaba1530f03d0389669e337249a4997420e2ca22760950e2ecaf5055e7c724ccbf407d1f0cd3b1702de0a93eab734cdd680e2adb532f883df9d4a3
7
+ data.tar.gz: b3099b85d58a4c74ac6aa6a6d5bf971b038484d03b501da5c6f6026f37b3758b1f4ddf8ad5565fff49993c3b278dd7cea816730ea6ed5e2e43d7b20e9b347106
@@ -1,3 +1,18 @@
1
+ ## [1.0.0](https://github.com/dounx/gwitch/releases/tag/1.0.0) (2020-05-25)
2
+
3
+ * Fix typo
4
+ * Fix a bug
5
+ * Update gemspec
6
+ * Update readme
7
+ * Refactor code
8
+ * Add some rake tasks
9
+ * Add gitignore
10
+ * Add mutex for multi threads
11
+ * Remove redundant comments
12
+ * Make get all games simultaneously
13
+ * Add cli
14
+ * Add some tests
15
+
1
16
  ## [0.0.1](https://github.com/dounx/gwitch/releases/tag/0.0.1) (2020-05-21)
2
17
 
3
18
  * Initial import
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Gwitch
2
2
 
3
- Gwitch can get switch games info (including price) from nintendo official API.
3
+ Gwitch can get switch games' info (including price) from nintendo official API.
4
4
 
5
5
  ## Prerequisites
6
6
 
@@ -24,21 +24,37 @@ And then execute:
24
24
 
25
25
  ## Basic Usage
26
26
 
27
- ```ruby
28
- require 'gwitch'
27
+ ### Bash
28
+
29
+ ```
30
+ Usage: gwitch [options]
31
+ -g, --games Get all games (without price info)
32
+ -p, --price alpha2,nsuid1,nsuid2 Get games' price (Max 50 nsuids)
33
+ -c, --countries Get avaliable countries' alpha2 code
34
+ -v, --version Print version and exit
35
+ -h, --help Show help
36
+ ```
37
+
38
+ The returned data will always be in json format.
39
+
40
+ Can be used with pipes and redirects.
41
+
42
+ eg.
43
+
44
+ ```bash
45
+ gwitch -g >> games.json
29
46
  ```
30
47
 
31
- Get all games from americas, asia and europe eshops.
48
+ ### Ruby
32
49
 
33
50
  ```ruby
34
- games = Gwitch::Game.all
51
+ require 'gwitch'
35
52
  ```
36
53
 
37
- Get all games from one eshops.
54
+ Get all games.
38
55
 
39
56
  ```ruby
40
- # Americas, Asia and Europe
41
- games = Gwitch::Game.all('Americas')
57
+ games = Gwitch::Game.all
42
58
  ```
43
59
 
44
60
  Get all avaliable countries.
@@ -50,16 +66,21 @@ countries = Gwitch::Country.all
50
66
  Get country's info.
51
67
 
52
68
  ```ruby
53
- country = Gwitch::Country.new('US')
54
- country.alpha2 # => 'US'
55
- country.region # => 'Americas'
56
- country.currency # => 'USD'
69
+ country = countries.first
70
+ country.alpha2 # => 'US'
71
+ country.region # => 'Americas'
72
+ country.currency # => 'USD'
73
+ country.avaliable? # => true
57
74
  ```
58
75
 
59
- Query game's price.
76
+ Query games' price (Max 50 games).
60
77
 
61
78
  ```ruby
62
- price = Gwitch::Game.price('US', 'en', '70010000000141')
79
+ prices = Gwitch::Game.price('US', 'en', '70010000000141,70010000000142')
80
+
81
+ prices = Gwitch::Game.price('US', 'en', ['70010000000141', '70010000000142'])
82
+
83
+ prices = Gwitch::Game.price('US', 'en', [70010000000141, 70010000000142])
63
84
  ```
64
85
 
65
86
  ## Build
@@ -68,7 +89,15 @@ price = Gwitch::Game.price('US', 'en', '70010000000141')
68
89
  git clone https://github.com/Dounx/gwitch
69
90
  cd gwitch
70
91
  gem build gwitch.gemspec
71
- gem install --local gwitch-0.0.1.gem
92
+ gem install --local gwitch-*.gem
93
+ ```
94
+
95
+ ## Rake
96
+
97
+ List of available tasks
98
+
99
+ ```bash
100
+ rake --tasks
72
101
  ```
73
102
 
74
103
  ## License
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require_relative "../lib/gwitch"
6
+ require_relative "../lib/gwitch/cli"
7
+
8
+ cli = Gwitch::CLI.new
9
+ cli.parse
10
+
11
+ options = cli.options
12
+
13
+ if options[:game]
14
+ puts Gwitch::Game.all.to_json
15
+ elsif options[:price]
16
+ opts = options[:price]
17
+ country = Gwitch::Country.new opts[0]
18
+ nsuids = opts.drop(1).map(&:to_i)
19
+ puts Gwitch::Game.price(country.alpha2, nsuids.join(',')).to_json
20
+ elsif options[:country]
21
+ puts Gwitch::Country.all.map(&:alpha2).to_json
22
+ else
23
+ puts cli.parser
24
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'gwitch/game'
4
- require_relative 'gwitch/country'
3
+ require_relative "gwitch/game"
4
+ require_relative "gwitch/country"
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require_relative "version"
5
+
6
+ module Gwitch
7
+ class CLI
8
+ attr_reader :options, :parser
9
+
10
+ def parse(args = ARGV)
11
+ @options = setup_options(args)
12
+ end
13
+
14
+ private
15
+
16
+ def setup_options(args)
17
+ parse_options(args)
18
+ end
19
+
20
+ def parse_options(argv)
21
+ opts = {}
22
+ @parser = option_parser(opts)
23
+ @parser.parse!(argv)
24
+ opts
25
+ end
26
+
27
+ def option_parser(opts)
28
+ parser = OptionParser.new { |o|
29
+ o.on "-g", "--games", "Get all games (without price info)" do |arg|
30
+ opts[:game] = arg
31
+ end
32
+
33
+ o.on "-p", "--price alpha2,nsuid1,nsuid2", Array, "Get games' price (Max 50 nsuids)" do |arg|
34
+ opts[:price] = arg
35
+ end
36
+
37
+ o.on "-c", "--countries", "Get avaliable countries' alpha2 code" do |arg|
38
+ opts[:country] = arg
39
+ end
40
+
41
+ o.on "-v", "--version", "Print version and exit" do |arg|
42
+ puts "Gwitch #{VERSION}"
43
+ exit 0
44
+ end
45
+ }
46
+
47
+ parser.banner = "Usage: gwitch [options]"
48
+ parser.on_tail "-h", "--help", "Show help" do
49
+ puts parser
50
+ exit 1
51
+ end
52
+
53
+ parser
54
+ end
55
+ end
56
+ end
@@ -1,30 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'countries'
4
- require_relative 'game'
3
+ require "countries"
4
+ require_relative "game"
5
5
 
6
6
  module Gwitch
7
7
  class Country
8
+ InvaildAlpha2CodeError = Class.new(StandardError)
9
+
8
10
  class << self
9
11
  # All avaliable countries
10
12
  def all
11
- ISO3166::Country.all.map(&:alpha2).select do |alpha2|
12
- avaliable?(alpha2)
13
- end.map { |alpha2| Country.new(alpha2) }
14
- end
15
-
16
- private
13
+ countries = ISO3166::Country.all.map do |country|
14
+ Country.new(country.alpha2)
15
+ end
17
16
 
18
- def avaliable?(alpha2)
19
- # An americas game
20
- nsuid = '70010000000141'
21
-
22
- !Game.price(alpha2, nsuid).nil?
17
+ countries.select{ |country| country.avaliable? }
23
18
  end
24
19
  end
25
20
 
26
21
  def initialize(alpha2)
27
22
  @country = ISO3166::Country[alpha2]
23
+ raise InvaildAlpha2CodeError unless @country
28
24
  end
29
25
 
30
26
  def alpha2
@@ -38,5 +34,12 @@ module Gwitch
38
34
  def currency
39
35
  @country.currency_code
40
36
  end
37
+
38
+ def avaliable?
39
+ # An americas game
40
+ nsuid = '70010000000141'
41
+
42
+ !Game.price(alpha2, nsuid).nil?
43
+ end
41
44
  end
42
45
  end
@@ -1,34 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'region'
3
+ require_relative "region"
4
4
 
5
5
  module Gwitch
6
6
  class Game
7
7
  IdsExceedMaxError = Class.new(StandardError)
8
8
 
9
+ MAX_IDS_SIZE = 50
10
+
9
11
  class << self
10
- def all(area = nil)
11
- case area
12
- when 'Americas'
13
- Region::Americas.games
14
- when 'Asia'
15
- Region::Asia.games
16
- when 'Europe'
17
- Region::Europe.games
18
- else
19
- Region.games
20
- end
12
+ def all
13
+ Region.games
21
14
  end
22
15
 
23
- # ids can be String or Array
24
16
  def price(alpha2, ids, lang = 'en')
25
- raise IdsExceedMaxError if ids.is_a?(Array) && ids.size > 50
17
+ ids = ids.split(',') if ids.is_a?(String)
18
+ raise IdsExceedMaxError if ids.size > MAX_IDS_SIZE
26
19
 
27
20
  api_url = 'https://api.ec.nintendo.com/v1/price'
28
21
  queries = {
29
22
  country: alpha2,
30
23
  lang: lang,
31
- ids: ids
24
+ ids: ids.join(',')
32
25
  }
33
26
 
34
27
  uri = URI.parse(api_url)
@@ -1,19 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'region/americas'
4
- require_relative 'region/asia'
5
- require_relative 'region/europe'
3
+ require_relative "region/americas"
4
+ require_relative "region/asia"
5
+ require_relative "region/europe"
6
6
 
7
7
  module Gwitch
8
-
9
- # Get all games from americas, asia and europe eshops
10
8
  class Region
11
9
  def self.games
12
- Hash[
13
- [Americas, Asia, Europe].collect do |region|
14
- [region.name.split('::').last, region.games]
10
+ semaphore = Mutex.new
11
+ games = []
12
+
13
+ threads =
14
+ [Americas, Asia, Europe].map do |region|
15
+ Thread.new do
16
+ semaphore.synchronize {
17
+ games += region.games
18
+ }
19
+ end
15
20
  end
16
- ]
21
+ threads.each(&:join)
22
+ games
17
23
  end
18
24
  end
19
25
  end
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'algoliasearch'
3
+ require "algoliasearch"
4
4
 
5
5
  module Gwitch
6
6
  class Region
7
-
8
- # A class which get games from americas eshop.
9
7
  class Americas
10
8
  CLIENT = Algolia::Client.new(
11
9
  application_id: 'U3B6GR4UA3',
@@ -18,10 +16,8 @@ module Gwitch
18
16
 
19
17
  # Trick for API limitation.
20
18
  # Nintendo limit each facet can only get 1000 results.
21
- # So we use as much as possible to get results.
19
+ # So use facets as much as possible to get results.
22
20
  # Maybe incompletely got.
23
- # DON'T use this directly.
24
- # Use filters method.
25
21
  FACETS = {
26
22
  generalFilters: ['Deals', 'DLC available', 'Demo available', 'Online Play via Nintendo Switch Online', 'Nintendo Switch Game Voucher'],
27
23
  availability: ['New releases', 'Available now', 'Pre-purchase', 'Coming soon'],
@@ -78,8 +74,6 @@ module Gwitch
78
74
  hits
79
75
  end
80
76
 
81
- # GalleryParsedError = Class.new
82
-
83
77
  def parse(raw)
84
78
  host = 'https://www.nintendo.com'
85
79
  microsite_host = 'https://assets.nintendo.com/image/upload/f_auto,q_auto,w_960,h_540'
@@ -132,6 +126,7 @@ module Gwitch
132
126
  categories: game['categories'],
133
127
  maker: game['publishers']&.join(', '),
134
128
  player: game['players'],
129
+ region: 'Americas',
135
130
  images: image_urls,
136
131
  url: url,
137
132
  release_at: game['releaseDateMask']
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'open-uri'
4
- require 'json'
3
+ require "open-uri"
4
+ require "json"
5
5
 
6
6
  module Gwitch
7
7
  class Region
8
-
9
- # A class which get games from asia eshop.
10
8
  class Asia
11
9
  API_URL = 'https://search.nintendo.jp/nintendo_soft/search.json'
12
10
  QUERIES = {
@@ -17,7 +15,6 @@ module Gwitch
17
15
  }.freeze
18
16
 
19
17
  class << self
20
- # Get all games from asia eshop.
21
18
  def games
22
19
  games = []
23
20
  uri = URI.parse(API_URL)
@@ -66,6 +63,7 @@ module Gwitch
66
63
  languages: game['lang'],
67
64
  modes: modes,
68
65
  dlcs: game['cnsuid'] || [],
66
+ region: 'Asia',
69
67
  images: image_urls,
70
68
  url: url,
71
69
  release_at: game['pdate']
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'open-uri'
4
- require 'json'
3
+ require "open-uri"
4
+ require "json"
5
5
 
6
6
  module Gwitch
7
7
  class Region
8
-
9
- # A class which get games from europe eshop.
10
8
  class Europe
11
9
  RowsTooSmallError = Class.new(StandardError)
12
10
 
@@ -23,7 +21,6 @@ module Gwitch
23
21
  }.freeze
24
22
 
25
23
  class << self
26
- # Get all games from asia eshop.
27
24
  def games
28
25
  uri = URI.parse(API_URL)
29
26
  uri.query = URI.encode_www_form(QUERIES)
@@ -62,6 +59,7 @@ module Gwitch
62
59
  languages: game['language_availability']&.first&.split(',') || [],
63
60
  modes: modes,
64
61
  cloud_save: game['cloud_saves_b'],
62
+ region: 'Europe',
65
63
  images: [
66
64
  schema + game['image_url'],
67
65
  schema + game['image_url_sq_s'],
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Gwitch
4
- VERSION = '0.0.1'
5
- end
2
+ VERSION = "1.0.0"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gwitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dounx
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-21 00:00:00.000000000 Z
11
+ date: 2020-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: algoliasearch
@@ -38,16 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
- description: This gem can get switch games info from nintendo official API.
41
+ description: Gwitch can get switch games' info (including price) from nintendo official
42
+ API.
42
43
  email: imdounx@gmail.com
43
- executables: []
44
+ executables:
45
+ - gwitch
44
46
  extensions: []
45
47
  extra_rdoc_files: []
46
48
  files:
47
49
  - CHANGELOG.md
48
50
  - LICENSE
49
51
  - README.md
52
+ - bin/gwitch
50
53
  - lib/gwitch.rb
54
+ - lib/gwitch/cli.rb
51
55
  - lib/gwitch/country.rb
52
56
  - lib/gwitch/game.rb
53
57
  - lib/gwitch/region.rb
@@ -81,5 +85,5 @@ requirements: []
81
85
  rubygems_version: 3.1.2
82
86
  signing_key:
83
87
  specification_version: 4
84
- summary: Nintendo switch game API.
88
+ summary: Nintendo switch games' info API.
85
89
  test_files: []