wot_api 0.0.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/README.md +23 -8
- data/lib/wot_api/base.rb +43 -9
- data/lib/wot_api/exceptions.rb +15 -0
- data/lib/wot_api/version.rb +1 -1
- data/lib/wot_api.rb +1 -1
- data/spec/base_spec.rb +91 -25
- data/spec/fixtures/error.json +10 -0
- data/wot_api.gemspec +1 -1
- metadata +7 -6
- data/lib/wot_api/player.rb +0 -28
- data/spec/player_spec.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4586e3de28e0bacf291c1cbe01b18ff30fc2ace9
|
4
|
+
data.tar.gz: 0cd304b95e69d48dc12509c91fd5b1496787423e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43d4e887e6524d6671567387d364e317869f2d8fdfb6f4971154a07aced1e0bc9691aa7f00252657243b2737907b32326d1a465a2ab7a4e513e5b1c368fef79f
|
7
|
+
data.tar.gz: 513b084b8a91ca13d245da5953f85692145b1de7f3ec9ee90ae66bc27f3abef0e2a8ba0cb80f25d5915f1aff684389ef5833bd79e4a09328038bedce6e79989b
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# WotApi
|
2
2
|
|
3
|
-
API wrapper for World of Tanks
|
3
|
+
API wrapper for World of Tanks in Ruby
|
4
|
+
|
5
|
+
[](https://travis-ci.org/jcantara/wot_api)
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -20,25 +22,38 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
Get an Application ID from Wargaming and read the available endpoints here: https://na.wargaming.net/developers/
|
22
24
|
|
23
|
-
|
25
|
+
NOTE: Other WoT geographical regions require different Application IDs
|
26
|
+
|
27
|
+
Initialize the gem with your Application ID(s):
|
28
|
+
|
29
|
+
WotApi::Base.config({na: '123456'})
|
30
|
+
|
31
|
+
The available regions are: na, ru, eu, asia, kr
|
32
|
+
|
33
|
+
The first region specified becomes the default if no region is specified in endpoint method arguments.
|
24
34
|
|
25
|
-
|
35
|
+
If using Rails, it is recommended to create an initializer that looks something like:
|
36
|
+
|
37
|
+
WotApi::Base.config(YAML.load_file("#{::Rails.root}/config/wot_api.yml"))
|
38
|
+
|
39
|
+
Along with a yaml file, config/wot_api.yml:
|
40
|
+
|
41
|
+
na: 123456
|
42
|
+
ru: 6asdf6
|
26
43
|
|
27
44
|
Call endpoints like such:
|
28
45
|
|
29
|
-
WotApi::Base.account_list(search: 'tank')
|
46
|
+
WotApi::Base.account_list(search: 'tank', region: 'ru')
|
30
47
|
|
31
|
-
Will return an array or hash with the results, or
|
48
|
+
Will return an array or hash with the results, or throw an error with a message on a failure.
|
32
49
|
|
33
50
|
## Future plans
|
34
51
|
|
35
|
-
Throw exceptions on failures
|
36
|
-
|
37
52
|
Add class wrappers for endpoints with convenience methods for class relationships and such.
|
38
53
|
|
39
54
|
## Contributing
|
40
55
|
|
41
|
-
1. Fork it ( https://github.com/
|
56
|
+
1. Fork it ( https://github.com/jcantara/wot_api/fork )
|
42
57
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
58
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
59
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/wot_api/base.rb
CHANGED
@@ -4,7 +4,6 @@ module WotApi
|
|
4
4
|
|
5
5
|
class Base
|
6
6
|
include HTTParty
|
7
|
-
base_uri 'https://api.worldoftanks.com'
|
8
7
|
|
9
8
|
ENDPOINTS = [
|
10
9
|
'/wot/account/list/',
|
@@ -41,12 +40,31 @@ module WotApi
|
|
41
40
|
'/wot/tanks/achievements/'
|
42
41
|
]
|
43
42
|
|
43
|
+
REGIONS = {
|
44
|
+
na: 'https://api.worldoftanks.com',
|
45
|
+
ru: 'https://api.worldoftanks.ru',
|
46
|
+
eu: 'https://api.worldoftanks.eu',
|
47
|
+
asia: 'https://api.worldoftanks.asia',
|
48
|
+
kr: 'https://api.worldoftanks.kr'
|
49
|
+
}
|
50
|
+
|
44
51
|
class << self
|
45
|
-
attr_reader :
|
52
|
+
attr_reader :configuration
|
53
|
+
attr_reader :default_region
|
46
54
|
|
47
|
-
def
|
48
|
-
@
|
49
|
-
|
55
|
+
def config(params={})
|
56
|
+
@configuration = {}
|
57
|
+
@default_region = nil
|
58
|
+
params.each do |conf|
|
59
|
+
region = conf[0].to_sym
|
60
|
+
application_id = conf[1]
|
61
|
+
if REGIONS[region] && application_id
|
62
|
+
@default_region ||= region
|
63
|
+
@configuration[region] = {base_uri: REGIONS[region], application_id: application_id.to_s}
|
64
|
+
else
|
65
|
+
raise WotApi::InvalidConfigError
|
66
|
+
end
|
67
|
+
end
|
50
68
|
end
|
51
69
|
|
52
70
|
def pathname(path)
|
@@ -54,16 +72,32 @@ module WotApi
|
|
54
72
|
end
|
55
73
|
|
56
74
|
def merged_params(params)
|
57
|
-
|
75
|
+
raise WotApi::InvalidConfigError unless @configuration.class == Hash
|
76
|
+
if region = params.delete(:region).to_sym rescue nil
|
77
|
+
config = @configuration[region]
|
78
|
+
else
|
79
|
+
config = @configuration[@default_region]
|
80
|
+
end
|
81
|
+
base_uri = config[:base_uri]
|
82
|
+
application_id = config[:application_id]
|
83
|
+
raise WotApi::InvalidRegionError unless base_uri && application_id
|
84
|
+
self.base_uri base_uri
|
85
|
+
params.merge({application_id: application_id})
|
58
86
|
end
|
59
87
|
|
60
88
|
ENDPOINTS.each do |endpoint|
|
61
89
|
define_method WotApi::Base.pathname(endpoint) do |params = {}|
|
62
|
-
|
90
|
+
begin
|
91
|
+
response = WotApi::Base.post(endpoint, body: merged_params(params))
|
92
|
+
rescue
|
93
|
+
raise
|
94
|
+
end
|
63
95
|
if response && response['data']
|
64
|
-
response['data']
|
96
|
+
return response['data']
|
65
97
|
else
|
66
|
-
|
98
|
+
message = 'Unknown Error'
|
99
|
+
message = response['error']['message'] if response && response['error'] && response['error']['message']
|
100
|
+
raise WotApi::ResponseError, message
|
67
101
|
end
|
68
102
|
end
|
69
103
|
end
|
data/lib/wot_api/version.rb
CHANGED
data/lib/wot_api.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -2,8 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe WotApi::Base do
|
4
4
|
|
5
|
-
it "has
|
6
|
-
|
5
|
+
it "has an empty base_uri" do
|
6
|
+
# NOTE: may remove this entirely if base_uri is just going to be set dynamically
|
7
|
+
expect(WotApi::Base.base_uri).to eq nil
|
7
8
|
end
|
8
9
|
|
9
10
|
it "includes HTTParty" do
|
@@ -21,23 +22,44 @@ describe WotApi::Base do
|
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
|
-
describe "
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
describe "REGIONS" do
|
26
|
+
WotApi::Base::REGIONS.each do |region|
|
27
|
+
describe "#{region}" do
|
28
|
+
it "has a sym region key" do
|
29
|
+
expect(region[0]).to be_a Symbol
|
30
|
+
end
|
31
|
+
|
32
|
+
it "has a string value" do
|
33
|
+
expect(region[1]).to be_a String
|
34
|
+
end
|
35
|
+
|
36
|
+
it "has a https URL value" do
|
37
|
+
expect(URI.parse(region[1]).scheme).to eq 'https'
|
38
|
+
end
|
39
|
+
end
|
28
40
|
end
|
41
|
+
end
|
29
42
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
43
|
+
describe ".config" do
|
44
|
+
context "with a valid config" do
|
45
|
+
it "creates hash of regions with base_uri and application_id" do
|
46
|
+
WotApi::Base.config({'na' => '123456'})
|
47
|
+
expect(WotApi::Base.configuration).to eq({na: {base_uri: 'https://api.worldoftanks.com', application_id: '123456'}})
|
48
|
+
end
|
49
|
+
|
50
|
+
it "sets first item as default region" do
|
51
|
+
WotApi::Base.config({'na' => '123456'})
|
52
|
+
expect(WotApi::Base.default_region).to eq :na
|
53
|
+
WotApi::Base.config({'ru' => '444444','na' => '123456'})
|
54
|
+
expect(WotApi::Base.default_region).to eq :ru
|
55
|
+
end
|
35
56
|
end
|
36
57
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
58
|
+
context "with an invalid config" do
|
59
|
+
it "raises an error" do
|
60
|
+
expect{WotApi::Base.config({lalala: 'fake'})}.to raise_error
|
61
|
+
end
|
62
|
+
end
|
41
63
|
end
|
42
64
|
|
43
65
|
describe ".pathname" do
|
@@ -49,10 +71,30 @@ describe WotApi::Base do
|
|
49
71
|
end
|
50
72
|
|
51
73
|
describe ".merged_params" do
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
74
|
+
context "with a region parameter" do
|
75
|
+
it "merges the params hash argument with the application_id from the specified region" do
|
76
|
+
arguments = {a: 'hi', b: 'test', c: 3, region: 'na'}
|
77
|
+
WotApi::Base.config(na: 'abc123')
|
78
|
+
expect(WotApi::Base.merged_params(arguments)).to eq({a: 'hi', b: 'test', c: 3}.merge(application_id: 'abc123'))
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "with no region parameter" do
|
83
|
+
it "merges the params hash argument with the application_id from the default first region" do
|
84
|
+
arguments = {a: 'hi', b: 'test', c: 3}
|
85
|
+
WotApi::Base.config(na: 'abc123')
|
86
|
+
expect(WotApi::Base.merged_params(arguments)).to eq arguments.merge(application_id: 'abc123')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "sets base_uri" do
|
91
|
+
WotApi::Base.config(na: 'abc123')
|
92
|
+
WotApi::Base.merged_params({})
|
93
|
+
expect(WotApi::Base.base_uri).to eq WotApi::Base::REGIONS[:na]
|
94
|
+
|
95
|
+
WotApi::Base.config(ru: 'abc123')
|
96
|
+
WotApi::Base.merged_params({})
|
97
|
+
expect(WotApi::Base.base_uri).to eq WotApi::Base::REGIONS[:ru]
|
56
98
|
end
|
57
99
|
end
|
58
100
|
|
@@ -60,25 +102,49 @@ describe WotApi::Base do
|
|
60
102
|
WotApi::Base::ENDPOINTS.each do |endpoint|
|
61
103
|
method_name = WotApi::Base.pathname(endpoint)
|
62
104
|
describe ".#{method_name}" do
|
105
|
+
before(:example) do
|
106
|
+
WotApi::Base.config({na: '123456'})
|
107
|
+
end
|
108
|
+
|
63
109
|
it "creates a method named #{method_name}" do
|
64
110
|
expect(WotApi::Base.respond_to?(method_name.to_sym)).to eq true
|
65
111
|
end
|
66
112
|
|
67
113
|
it "posts to the endpoint #{endpoint}" do
|
68
|
-
expect(WotApi::Base).to receive(:post).
|
114
|
+
expect(WotApi::Base).to receive(:post).and_return({'data' => true})
|
69
115
|
WotApi::Base.send(method_name.to_sym)
|
70
116
|
end
|
71
117
|
|
72
118
|
it "accepts a hash of arguments to post to the endpoint and merges them with the application_id" do
|
73
119
|
arguments = {a: 'hi', b: 'test', c: 3}
|
74
|
-
WotApi::Base.application_id
|
75
|
-
expect(WotApi::Base).to receive(:post).with(endpoint, body: arguments.merge(application_id: WotApi::Base.application_id)).exactly(1).times
|
120
|
+
expect(WotApi::Base).to receive(:post).with(endpoint, body: arguments.merge(application_id: '123456')).and_return({'data' => true})
|
76
121
|
WotApi::Base.send(method_name.to_sym, arguments)
|
77
122
|
end
|
78
123
|
|
79
|
-
|
80
|
-
|
81
|
-
|
124
|
+
context "with a valid response" do
|
125
|
+
it "receives a response from api with data in an Array or Hash" do
|
126
|
+
FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}#{endpoint}", :response => File.join(File.dirname(__FILE__), 'fixtures', "#{method_name}.json"))
|
127
|
+
expect(WotApi::Base.send(method_name.to_sym)).to be_a_kind_of(Array).or be_a_kind_of(Hash)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "with an invalid response" do
|
132
|
+
it "raises an exception" do
|
133
|
+
FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}#{endpoint}", :response => File.join(File.dirname(__FILE__), 'fixtures', "error.json"))
|
134
|
+
expect{WotApi::Base.send(method_name.to_sym)}.to raise_error
|
135
|
+
end
|
136
|
+
|
137
|
+
it "includes the error message from WoT in the exception" do
|
138
|
+
FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}#{endpoint}", :response => File.join(File.dirname(__FILE__), 'fixtures', "error.json"))
|
139
|
+
expect{WotApi::Base.send(method_name.to_sym)}.to raise_error('APPLICATION_ID_NOT_SPECIFIED')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context "with httparty exception" do
|
144
|
+
it "reraises the httparty exception" do
|
145
|
+
allow(WotApi::Base).to receive(:post).and_raise(HTTParty::Error)
|
146
|
+
expect{WotApi::Base.send(method_name.to_sym)}.to raise_error(HTTParty::Error)
|
147
|
+
end
|
82
148
|
end
|
83
149
|
end
|
84
150
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Server: nginx
|
3
|
+
Date: Tue, 05 Aug 2014 21:48:28 GMT
|
4
|
+
Content-Type: application/json
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
Connection: keep-alive
|
7
|
+
Vary: Accept-Encoding
|
8
|
+
Access-Control-Allow-Origin: *
|
9
|
+
|
10
|
+
{"status":"error","error":{"field":"application_id","message":"APPLICATION_ID_NOT_SPECIFIED","code":402,"value":null}}
|
data/wot_api.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wot_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse Cantara
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -103,13 +103,14 @@ extensions: []
|
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
105
|
- ".gitignore"
|
106
|
+
- ".travis.yml"
|
106
107
|
- Gemfile
|
107
108
|
- LICENSE.txt
|
108
109
|
- README.md
|
109
110
|
- Rakefile
|
110
111
|
- lib/wot_api.rb
|
111
112
|
- lib/wot_api/base.rb
|
112
|
-
- lib/wot_api/
|
113
|
+
- lib/wot_api/exceptions.rb
|
113
114
|
- lib/wot_api/version.rb
|
114
115
|
- spec/base_spec.rb
|
115
116
|
- spec/fixtures/account_achievements.json
|
@@ -132,6 +133,7 @@ files:
|
|
132
133
|
- spec/fixtures/encyclopedia_tanks.json
|
133
134
|
- spec/fixtures/encyclopedia_tankturrets.json
|
134
135
|
- spec/fixtures/endpoints.yml
|
136
|
+
- spec/fixtures/error.json
|
135
137
|
- spec/fixtures/globalwar_clans.json
|
136
138
|
- spec/fixtures/globalwar_famepoints.json
|
137
139
|
- spec/fixtures/globalwar_maps.json
|
@@ -145,7 +147,6 @@ files:
|
|
145
147
|
- spec/fixtures/ratings_types.json
|
146
148
|
- spec/fixtures/tanks_achievements.json
|
147
149
|
- spec/fixtures/tanks_stats.json
|
148
|
-
- spec/player_spec.rb
|
149
150
|
- spec/spec_helper.rb
|
150
151
|
- wot_api.gemspec
|
151
152
|
homepage: https://github.com/jcantara/wot_api
|
@@ -167,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
168
|
- !ruby/object:Gem::Version
|
168
169
|
version: '0'
|
169
170
|
requirements:
|
170
|
-
- Application ID from Wargaming developers portal.
|
171
|
+
- Application ID(s) from Wargaming developers portal.
|
171
172
|
rubyforge_project:
|
172
173
|
rubygems_version: 2.2.2
|
173
174
|
signing_key:
|
@@ -195,6 +196,7 @@ test_files:
|
|
195
196
|
- spec/fixtures/encyclopedia_tanks.json
|
196
197
|
- spec/fixtures/encyclopedia_tankturrets.json
|
197
198
|
- spec/fixtures/endpoints.yml
|
199
|
+
- spec/fixtures/error.json
|
198
200
|
- spec/fixtures/globalwar_clans.json
|
199
201
|
- spec/fixtures/globalwar_famepoints.json
|
200
202
|
- spec/fixtures/globalwar_maps.json
|
@@ -208,5 +210,4 @@ test_files:
|
|
208
210
|
- spec/fixtures/ratings_types.json
|
209
211
|
- spec/fixtures/tanks_achievements.json
|
210
212
|
- spec/fixtures/tanks_stats.json
|
211
|
-
- spec/player_spec.rb
|
212
213
|
- spec/spec_helper.rb
|
data/lib/wot_api/player.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module WotApi
|
2
|
-
|
3
|
-
class Player < Base
|
4
|
-
attr_reader :nickname, :account_id
|
5
|
-
|
6
|
-
def initialize(params={})
|
7
|
-
ArgumentError.new("application_id is required") unless self.class.default_params[:application_id]
|
8
|
-
@nickname = params['nickname']
|
9
|
-
@account_id = params['account_id']
|
10
|
-
end
|
11
|
-
|
12
|
-
class << self
|
13
|
-
|
14
|
-
def find_all_by_name_like(query)
|
15
|
-
result = self.post('/wot/account/list/', body: {search: query})
|
16
|
-
data = result['data']
|
17
|
-
if data
|
18
|
-
data.map{|d| self.new(d)}
|
19
|
-
else
|
20
|
-
[]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
data/spec/player_spec.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe WotApi::Player do
|
4
|
-
context "with a presumably valid application_id" do
|
5
|
-
before(:all) { WotApi::Base.application_id = '12345'; puts WotApi::Base.application_id }
|
6
|
-
|
7
|
-
it "inherits application_id from base" do
|
8
|
-
puts WotApi::Base.application_id
|
9
|
-
expect(WotApi::Player.default_params[:application_id]).to eq '12345'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context "with no application_id" do
|
14
|
-
before(:all) { WotApi::Base.application_id = nil; puts WotApi::Base.application_id }
|
15
|
-
|
16
|
-
it "throws an exception when attempting to initialize an instance" do
|
17
|
-
puts WotApi::Base.application_id
|
18
|
-
expect{ WotApi::Player.new }.to raise_error
|
19
|
-
end
|
20
|
-
|
21
|
-
it "has a nil application_id" do
|
22
|
-
expect(WotApi::Player.default_params[:application_id]).to be_nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
it "gets latest application_id from Base when it becomes updated" do
|
27
|
-
WotApi::Base.application_id = 'aaaaa'
|
28
|
-
expect(WotApi::Player.default_params[:application_id]).to eq 'aaaaa'
|
29
|
-
WotApi::Base.application_id = '54321'
|
30
|
-
expect(WotApi::Player.default_params[:application_id]).to eq '54321'
|
31
|
-
end
|
32
|
-
end
|