blizzard_api 0.4.0 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +21 -0
  3. data/.rubocop.yml +2 -1
  4. data/CHANGELOG.md +65 -0
  5. data/Gemfile.lock +19 -14
  6. data/README.md +6 -3
  7. data/blizzard_api.gemspec +6 -4
  8. data/lib/blizzard_api/diablo/game_data/generic_data_endpoint.rb +2 -2
  9. data/lib/blizzard_api/diablo/request.rb +2 -2
  10. data/lib/blizzard_api/hearthstone/game_data/generic_data_endpoint.rb +2 -2
  11. data/lib/blizzard_api/hearthstone/request.rb +2 -2
  12. data/lib/blizzard_api/request.rb +45 -38
  13. data/lib/blizzard_api/starcraft.rb +7 -0
  14. data/lib/blizzard_api/starcraft/community/legacy.rb +88 -0
  15. data/lib/blizzard_api/starcraft/request.rb +2 -2
  16. data/lib/blizzard_api/version.rb +1 -1
  17. data/lib/blizzard_api/wow.rb +33 -0
  18. data/lib/blizzard_api/wow/game_data/azerite_essence.rb +2 -0
  19. data/lib/blizzard_api/wow/game_data/covenant.rb +79 -0
  20. data/lib/blizzard_api/wow/game_data/creature.rb +2 -0
  21. data/lib/blizzard_api/wow/game_data/generic_data_endpoint.rb +2 -2
  22. data/lib/blizzard_api/wow/game_data/item.rb +2 -0
  23. data/lib/blizzard_api/wow/game_data/journal.rb +15 -0
  24. data/lib/blizzard_api/wow/game_data/media.rb +37 -0
  25. data/lib/blizzard_api/wow/game_data/modified_crafting.rb +67 -0
  26. data/lib/blizzard_api/wow/game_data/mount.rb +2 -0
  27. data/lib/blizzard_api/wow/game_data/pet.rb +46 -0
  28. data/lib/blizzard_api/wow/game_data/spell.rb +2 -0
  29. data/lib/blizzard_api/wow/game_data/tech_talent.rb +57 -0
  30. data/lib/blizzard_api/wow/profile/character_profile.rb +19 -1
  31. data/lib/blizzard_api/wow/profile/guild.rb +2 -0
  32. data/lib/blizzard_api/wow/profile/profile.rb +2 -2
  33. data/lib/blizzard_api/wow/request.rb +2 -2
  34. data/lib/blizzard_api/wow/search/search_composer.rb +3 -3
  35. data/lib/blizzard_api/wow/search/search_request.rb +1 -1
  36. data/lib/blizzard_api/wow/slug.rb +12 -0
  37. metadata +16 -8
  38. data/.gitlab-ci.yml +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba9df0ce8e6bc1398057fa55eae73f4081d370f13d204f0d6f2eccb313f3bed2
4
- data.tar.gz: 5f3d190b6d324c45b456ccd856b6271b6e2447816930b7e451aa7fa325c85d98
3
+ metadata.gz: bf8d2354d8c5693db38cc06fb9a531ea3088c2ef03df14de07369e7b3bf30e3b
4
+ data.tar.gz: 05fd0395084f152722138b93bed892ceb5cbb826563aae96830b5f4d85acfd0b
5
5
  SHA512:
6
- metadata.gz: aa67738363d505b469eeb89c5fcc140fe2bbd8834c3be1afd588ba1a7ae9ac79347409bfdecad97a7a24ea1c427b70a5b4f3812b8d838d11c5b41bb34916bc09
7
- data.tar.gz: 6489a58fc1132f50ccd977f0ad4dff408997c9fff80be0d38b7e5d43602287c99490af2604b6bed7a913baa22ad701189fe9eb366a58b39b1266b57a275df73d
6
+ metadata.gz: fcc914e44f283a236eccd316601f175925d0304b34a6dae3dd5a69c5c441cbd5b8f7ba9e7f4b1080375a7b53f82bb93e5e273deedeba1586e612ea7eb6368066
7
+ data.tar.gz: 5a28789fa6f1839f4a81a8de96756a6d8b7a6ae5ae7c25f9b6f9747f4acd9fd0e7a762ed29f0578465141a278510bf7a211308e4701c1aca1f6d16b8d53bd600
@@ -0,0 +1,21 @@
1
+ name: Ruby
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@21351ecc0a7c196081abca5dc55b08f085efe09a
12
+ with:
13
+ ruby-version: 2.6
14
+ - name: Install dependencies
15
+ run: bundle install
16
+ - name: Run tests
17
+ run: bundle exec rake
18
+ env:
19
+ BNET_APPLICATION_ID: ${{ secrets.BNET_APPLICATION_ID }}
20
+ BNET_APPLICATION_SECRET: ${{ secrets.BNET_APPLICATION_SECRET }}
21
+ USE_CACHE: false
@@ -1,6 +1,6 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
- Metrics/LineLength:
3
+ Layout/LineLength:
4
4
  Max: 140
5
5
 
6
6
  Metrics/ModuleLength:
@@ -12,6 +12,7 @@ Metrics/AbcSize:
12
12
 
13
13
  AllCops:
14
14
  TargetRubyVersion: 2.5
15
+ NewCops: enable
15
16
  Exclude:
16
17
  - 'vendor/**/*'
17
18
  - 'bin/**/*'
@@ -1,5 +1,70 @@
1
1
  Please view this file on the master branch, otherwise it may be outdated
2
2
 
3
+ **Version 0.5.3**
4
+
5
+ Added missing legacy SC2 endpoints
6
+
7
+ See https://github.com/francis-schiavo/blizzard_api/pull/2
8
+
9
+ **Version 0.5.2**
10
+
11
+ Added new endpoints: https://us.forums.blizzard.com/en/blizzard/t/wow-shadowlands-api-update-covenenats-soulbinds-more/13385
12
+
13
+ **Version 0.5.1**
14
+
15
+ Added new endpoints: https://us.forums.blizzard.com/en/blizzard/t/wow-game-data-api-modified-crafting-support/12727
16
+
17
+ **Version 0.5.0**
18
+
19
+ This version brings a lot of internal changes to the way the gem works. While
20
+ there no breaking change is expected use it carefully.
21
+
22
+ ## New features:
23
+
24
+ ### Extended mode
25
+
26
+ When creating a request you can now specify **mode** as the last argument. Available modes:
27
+ * **regular**: No changes, should work as it always did.
28
+ * **extended**: All requests now return an array with two objects, the actual HTTPResponse object and the usual Hash.
29
+
30
+ ```ruby
31
+ api_client = BlizzardApi::Wow::Item.new 'us', :extended
32
+ response, item_data = api_client.get 35_000
33
+
34
+ puts response.code # 200
35
+ puts item_data[:name][:en_US] # Brutal Gladiator's Dragonhide Legguards
36
+ ```
37
+
38
+ This is intended to expose the response code and headers.
39
+
40
+ **Important**: Extended mode completely disables the cache.
41
+
42
+ ### Custom headers
43
+
44
+ You an now pass custom headers in the **options** hash.
45
+
46
+ There is also a new shorthand for the `If-Modified-Since` header.
47
+
48
+ ```ruby
49
+ # If-Modified-Since shorhand
50
+ auction_data = BlizzardApi::Wow.auction.get 1146, since: DateTime.parse('2099-01-01Z')
51
+
52
+ # Using custom headers
53
+ auction_data = BlizzardApi::Wow.auction.get 1146, headers: { 'If-Modified-Since' => 'Sun, 27 Sep 2020 02:17:03 GMT' }
54
+ ```
55
+
56
+ **Important**
57
+ * Headers are not part of the cache key, use the option `ignore_cache: true` when needed.
58
+ * The `since` shorthand will always disable the cache.
59
+
60
+ **Version 0.4.2**
61
+
62
+ Added new retail and classic search endpoints described here: https://us.forums.blizzard.com/en/blizzard/t/world-of-warcraft-api-patch-notes-20200708/10310
63
+
64
+ **Version 0.4.1**
65
+
66
+ Added new retail and classic endpoints described here: https://us.forums.blizzard.com/en/blizzard/t/world-of-warcraft-api-patch-notes-20200609/8902
67
+
3
68
  **Version 0.4.0**
4
69
 
5
70
  Added support to searchable endpoints
@@ -1,31 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blizzard_api (0.4.0)
4
+ blizzard_api (0.5.2)
5
5
  redis (~> 4.1, >= 4.1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- ast (2.4.0)
11
- dotenv (2.7.5)
12
- jaro_winkler (1.5.3)
13
- minitest (5.11.3)
14
- parallel (1.17.0)
15
- parser (2.6.3.0)
16
- ast (~> 2.4.0)
10
+ ast (2.4.1)
11
+ dotenv (2.7.6)
12
+ minitest (5.14.2)
13
+ parallel (1.20.1)
14
+ parser (2.7.2.0)
15
+ ast (~> 2.4.1)
17
16
  rainbow (3.0.0)
18
17
  rake (13.0.1)
19
- redis (4.1.4)
20
- rubocop (0.74.0)
21
- jaro_winkler (~> 1.5.1)
18
+ redis (4.2.5)
19
+ regexp_parser (2.0.0)
20
+ rexml (3.2.4)
21
+ rubocop (0.93.1)
22
22
  parallel (~> 1.10)
23
- parser (>= 2.6)
23
+ parser (>= 2.7.1.5)
24
24
  rainbow (>= 2.2.2, < 4.0)
25
+ regexp_parser (>= 1.8)
26
+ rexml
27
+ rubocop-ast (>= 0.6.0)
25
28
  ruby-progressbar (~> 1.7)
26
- unicode-display_width (>= 1.4.0, < 1.7)
29
+ unicode-display_width (>= 1.4.0, < 2.0)
30
+ rubocop-ast (1.3.0)
31
+ parser (>= 2.7.1.5)
27
32
  ruby-progressbar (1.10.1)
28
- unicode-display_width (1.6.0)
33
+ unicode-display_width (1.7.0)
29
34
  yard (0.9.25)
30
35
 
31
36
  PLATFORMS
data/README.md CHANGED
@@ -1,9 +1,7 @@
1
1
  # Blizzard Api
2
2
 
3
- [![pipeline status](https://gitlab.com/francisschiavo/blizzard_api/badges/master/pipeline.svg)](https://gitlab.com/francisschiavo/blizzard_api/commits/master)
4
3
  [![Gem Version](https://badge.fury.io/rb/blizzard_api.svg)](https://badge.fury.io/rb/blizzard_api)
5
4
 
6
-
7
5
  This gem allow you to interface with the new blizzard api (2018) using the OAuth2 protocol for every request.
8
6
 
9
7
  **Important**: This gem does not support, nor will support China endpoints.
@@ -145,6 +143,7 @@ end
145
143
  - types
146
144
  - type :id
147
145
  - display_media :id
146
+ - search
148
147
  * Blizzard::Wow::Guild
149
148
  - rewards
150
149
  - perks
@@ -211,6 +210,7 @@ end
211
210
  - get :id
212
211
  - complete
213
212
  - status :realms
213
+ - search
214
214
  * Blizzard::Wow::Region
215
215
  - index
216
216
  - get :id
@@ -231,10 +231,12 @@ end
231
231
  - class :id
232
232
  - subclass :class_id, :subclass_id
233
233
  - media :id
234
+ - search
234
235
  * Blizzard::Wow::AzeriteEssence
235
236
  - index
236
237
  - get :id
237
238
  - media :id
239
+ - search
238
240
  * Blizzard::Wow::ReputationTier
239
241
  - index
240
242
  - get :id
@@ -262,6 +264,7 @@ end
262
264
  - get :id
263
265
  * Blizzard::Wow::Spell
264
266
  - get :id
267
+ - search
265
268
  * Blizzard::Wow::Zone
266
269
  - index
267
270
  - get :id
@@ -346,7 +349,7 @@ Every endpoint requiring a *region_id* parameter will accepts either the integer
346
349
 
347
350
  ## Contributing
348
351
 
349
- Bug reports and pull requests are welcome on Gitlab at https://gitlab.com/francisschiavo/blizzard_api/issues
352
+ Bug reports and pull requests are welcome on Github at https://github.com/francis-schiavo/blizzard_api/issues
350
353
 
351
354
  ## License
352
355
 
@@ -9,10 +9,10 @@ Gem::Specification.new do |spec|
9
9
  spec.name = 'blizzard_api'
10
10
  spec.version = BlizzardApi::VERSION
11
11
  spec.authors = ['Francis Schiavo']
12
- spec.email = ['francis.schiavo@francisschiavo.com']
12
+ spec.email = ['francis@schiavo.dev']
13
13
  spec.summary = 'Unofficial Ruby client for Blizzard Entertainment API'
14
14
  spec.description = 'This is a simple interface to obtain data from Blizzard API'
15
- spec.homepage = 'https://gitlab.com/francisschiavo/blizzard_api'
15
+ spec.homepage = 'https://github.com/francis-schiavo/blizzard_api'
16
16
  spec.license = 'MIT'
17
17
 
18
18
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -20,8 +20,10 @@ Gem::Specification.new do |spec|
20
20
  raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
21
21
 
22
22
  spec.metadata['homepage_uri'] = spec.homepage
23
- spec.metadata['source_code_uri'] = 'https://gitlab.com/francisschiavo/blizzard_api'
24
- spec.metadata['changelog_uri'] = 'https://gitlab.com/francisschiavo/blizzard_api/blob/master/CHANGELOG.md'
23
+ spec.metadata['source_code_uri'] = 'https://github.com/francis-schiavo/blizzard_api'
24
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/francis-schiavo/blizzard_api/issues'
25
+ spec.metadata['changelog_uri'] = 'https://github.com/francis-schiavo/blizzard_api/blob/master/CHANGELOG.md'
26
+ spec.metadata['documentation_uri'] = 'https://rubydoc.info/gems/blizzard_api'
25
27
 
26
28
  # Specify which files should be added to the gem when it is released.
27
29
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -4,8 +4,8 @@ module BlizzardApi
4
4
  module Diablo
5
5
  # Generic endpoint to support most data requests with minor configurations
6
6
  class GenericDataEndpoint < Diablo::Request
7
- def initialize(region = nil)
8
- super region
7
+ def initialize(region = nil, mode = :regular)
8
+ super region, mode
9
9
  endpoint_setup
10
10
  @ttl ||= CACHE_DAY
11
11
  end
@@ -6,8 +6,8 @@ module BlizzardApi
6
6
  class Request < BlizzardApi::Request
7
7
  ##
8
8
  # @!macro regions
9
- def initialize(region = nil)
10
- super region
9
+ def initialize(region = nil, mode = :regular)
10
+ super region, mode
11
11
  @game = 'd3'
12
12
  end
13
13
  end
@@ -4,8 +4,8 @@ module BlizzardApi
4
4
  module Hearthstone
5
5
  # Generic endpoint to support most data requests with minor configurations
6
6
  class GenericDataEndpoint < Hearthstone::Request
7
- def initialize(region = nil)
8
- super region
7
+ def initialize(region = nil, mode = :regular)
8
+ super region, mode
9
9
  endpoint_setup
10
10
  @ttl ||= CACHE_DAY
11
11
  end
@@ -6,8 +6,8 @@ module BlizzardApi
6
6
  class Request < BlizzardApi::Request
7
7
  ##
8
8
  # @!macro regions
9
- def initialize(region = nil)
10
- super region
9
+ def initialize(region = nil, mode = :regular)
10
+ super region, mode
11
11
  @game = 'hearthstone'
12
12
  end
13
13
  end
@@ -8,6 +8,7 @@
8
8
  # @option options [String] :access_token Overrides the access_token for a single call
9
9
  # @option options [Boolean] :ignore_cache If set to true the request will not use the cache
10
10
  # @option options [Integer] :ttl Override the default time (in seconds) a request should be cached
11
+ # @option options [DateTime] :since Adds the If-modified-since headers. Will always ignore cache when set.
11
12
 
12
13
  ##
13
14
  # @!macro [new] regions
@@ -16,7 +17,7 @@
16
17
 
17
18
  ##
18
19
  # @!macro [new] response
19
- # @return [OpenStruct,Array] API Response. The actual type of the returned object depends on the *format* option
20
+ # @return [Hash] API Response. The actual type of the returned object depends on the *format* option
20
21
  # in the configuration module
21
22
 
22
23
  ##
@@ -31,18 +32,12 @@ module BlizzardApi
31
32
  ##
32
33
  # Simplifies the requests to Blizzard APIS
33
34
  class Request
34
- # One minute cache
35
- CACHE_MINUTE = 60
36
35
  # One hour cache
37
- CACHE_HOUR = 60 * CACHE_MINUTE
36
+ CACHE_HOUR = 3600
38
37
  # One day cache
39
38
  CACHE_DAY = 24 * CACHE_HOUR
40
- # One week cache
41
- CACHE_WEEK = CACHE_DAY * 7
42
- # One (commercial) month cache
43
- CACHE_MONTH = CACHE_DAY * 30
44
39
  # Three (commercial) months cache
45
- CACHE_TRIMESTER = CACHE_MONTH * 3
40
+ CACHE_TRIMESTER = CACHE_DAY * 90
46
41
 
47
42
  # Common endpoints
48
43
  BASE_URLS = {
@@ -59,13 +54,20 @@ module BlizzardApi
59
54
  # @return [String] Api region
60
55
  attr_accessor :region
61
56
 
57
+ ##
58
+ # @!attribute mode
59
+ # @return [:regular, :extended]
60
+ attr_accessor :mode
61
+
62
62
  ##
63
63
  # @!macro regions
64
- def initialize(region = nil)
64
+ def initialize(region = nil, mode = :regular)
65
65
  self.region = region || BlizzardApi.region
66
66
  @redis = Redis.new(host: BlizzardApi.redis_host, port: BlizzardApi.redis_port) if BlizzardApi.use_cache
67
67
  # Use the shared access_token, or create one if it doesn't exists. This avoids unnecessary calls to create tokens.
68
68
  @access_token = BlizzardApi.access_token || create_access_token
69
+ # Mode
70
+ @mode = mode
69
71
  end
70
72
 
71
73
  require 'net/http'
@@ -88,7 +90,7 @@ module BlizzardApi
88
90
  def endpoint_namespace(options)
89
91
  case options[:namespace]
90
92
  when :dynamic
91
- "dynamic-#{region}"
93
+ options.include?(:classic) ? "dynamic-classic-#{region}" : "dynamic-#{region}"
92
94
  when :static
93
95
  options.include?(:classic) ? "static-classic-#{region}" : "static-#{region}"
94
96
  when :profile
@@ -98,10 +100,6 @@ module BlizzardApi
98
100
  end
99
101
  end
100
102
 
101
- def string_to_slug(string)
102
- CGI.escape(string.downcase.tr(' ', '-'))
103
- end
104
-
105
103
  def create_access_token
106
104
  uri = URI.parse("https://#{BlizzardApi.region}.battle.net/oauth/token")
107
105
 
@@ -121,26 +119,24 @@ module BlizzardApi
121
119
  # Creates the whole url for request
122
120
  parsed_url = URI.parse(url)
123
121
 
124
- data = options[:ignore_cache] ? nil : find_in_cache(parsed_url.to_s)
122
+ data = using_cache?(options) ? find_in_cache(parsed_url.to_s) : nil
123
+
125
124
  # If data was found that means cache is enabled and valid
126
- return format_response data if data
125
+ return JSON.parse(data, symbolize_names: true) if data
127
126
 
128
- # Override access_token
129
- @access_token = options[:access_token] if options.include? :access_token
127
+ response = consume_api parsed_url, options
130
128
 
131
- response = consume_api parsed_url
129
+ save_in_cache parsed_url.to_s, response.body, options[:ttl] || CACHE_DAY if using_cache? options
132
130
 
133
- unless options[:ignore_cache]
134
- ttl = options[:ttl] || CACHE_DAY
135
- save_in_cache parsed_url.to_s, response.body, ttl
136
- end
131
+ response_data = response.code.to_i.eql?(304) ? nil : JSON.parse(response.body, symbolize_names: true)
132
+ return [response, response_data] if mode.eql? :extended
137
133
 
138
- format_response response.body
134
+ response_data
139
135
  end
140
136
 
141
137
  def api_request(uri, query_string = {})
142
138
  # List of request options
143
- options_key = %i[ignore_cache ttl format access_token namespace classic]
139
+ options_key = %i[ignore_cache ttl format access_token namespace classic headers since]
144
140
 
145
141
  # Separates request options from api fields and options. Any user-defined option will be treated as api field.
146
142
  options = query_string.select { |k, _v| query_string.delete(k) || true if options_key.include? k }
@@ -159,35 +155,46 @@ module BlizzardApi
159
155
 
160
156
  private
161
157
 
162
- def consume_api(url)
158
+ ##
159
+ # @param options [Hash] Request options
160
+ def using_cache?(options)
161
+ return false if mode.eql?(:extended) || options.key?(:since)
162
+
163
+ !options.fetch(:ignore_cache, false)
164
+ end
165
+
166
+ def consume_api(url, options = {})
163
167
  # Creates a HTTP connection and request to ensure thread safety
164
168
  http = Net::HTTP.new(url.host, url.port)
165
169
  http.use_ssl = true
166
170
  request = Net::HTTP::Get.new(url)
167
171
 
168
- # Blizzard API documentation states the preferred way to send the access_token is using Bearer token on header
169
- request['Authorization'] = "Bearer #{@access_token}"
172
+ add_headers request, options
170
173
 
171
174
  # Executes the request
172
175
  http.request(request).tap do |response|
173
- raise BlizzardApi::ApiException.new 'Request failed', response.code.to_i unless response.code.to_i == 200
176
+ if mode.eql?(:regular) && ![200, 304].include?(response.code.to_i)
177
+ raise BlizzardApi::ApiException.new 'Request failed', response.code.to_i
178
+ end
174
179
  end
175
180
  end
176
181
 
177
- def save_in_cache(resource_url, data, ttl)
178
- return nil unless BlizzardApi.use_cache
182
+ def add_headers(request, options)
183
+ # Blizzard API documentation states the preferred way to send the access_token is using Bearer token on header
184
+ request['Authorization'] = "Bearer #{options.fetch(:access_token, @access_token)}"
185
+ # Format If-modified-since option
186
+ request['If-Modified-Since'] = options[:since].httpdate if options.key? :since
187
+ options[:headers]&.each { |header, content| request[header] = content }
188
+ end
179
189
 
180
- @redis.setex resource_url, ttl, data
190
+ def save_in_cache(resource_url, data, ttl)
191
+ @redis.setex resource_url, ttl, data if BlizzardApi.use_cache
181
192
  end
182
193
 
183
194
  def find_in_cache(resource_url)
184
195
  return false unless BlizzardApi.use_cache
185
196
 
186
- @redis.get resource_url if @redis.exists resource_url
187
- end
188
-
189
- def format_response(data)
190
- JSON.parse(data, symbolize_names: true)
197
+ @redis.get resource_url if @redis.exists? resource_url
191
198
  end
192
199
  end
193
200
  end