wot_api 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
  SHA1:
3
- metadata.gz: e0909caedc0a048860390e94624ac37f4c9af8c2
4
- data.tar.gz: 8a75cb0165ee35d8c9db6de2c8a04cadd5a76ad5
3
+ metadata.gz: 4586e3de28e0bacf291c1cbe01b18ff30fc2ace9
4
+ data.tar.gz: 0cd304b95e69d48dc12509c91fd5b1496787423e
5
5
  SHA512:
6
- metadata.gz: 4fe70ddf3fa23be6410327910a1fabdc23577ed4273d0d961d5ebbf6cedf65b232e3bd6336ec05e1e79c617b084c81083e9acd169726bf307a07c1fc80595257
7
- data.tar.gz: 2c3755fbc5ed330fa206babc9f7a2115878b7b8d75558d99f5e2edecce56935c542af7ae0b2c840a26665cfab41dd3b8a4c30ed72905f9212cffbc89349bcfc5
6
+ metadata.gz: 43d4e887e6524d6671567387d364e317869f2d8fdfb6f4971154a07aced1e0bc9691aa7f00252657243b2737907b32326d1a465a2ab7a4e513e5b1c368fef79f
7
+ data.tar.gz: 513b084b8a91ca13d245da5953f85692145b1de7f3ec9ee90ae66bc27f3abef0e2a8ba0cb80f25d5915f1aff684389ef5833bd79e4a09328038bedce6e79989b
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.1
5
+ - 2.0.0
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - ruby-head
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
+ [![Build Status](https://travis-ci.org/jcantara/wot_api.svg?branch=master)](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
- Initialize the gem with your Application ID:
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
- WotApi::Base.application_id = '123456'
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 nil on a failure.
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/[my-github-username]/wot_api/fork )
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 :application_id
52
+ attr_reader :configuration
53
+ attr_reader :default_region
46
54
 
47
- def application_id=(id)
48
- @application_id = id
49
- #self.default_params application_id: @application_id
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
- params.merge({application_id: @application_id})
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
- response = WotApi::Base.post(endpoint, body: merged_params(params))
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
- nil
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
@@ -0,0 +1,15 @@
1
+ module WotApi
2
+
3
+ class Error < StandardError
4
+ end
5
+
6
+ class ResponseError < Error
7
+ end
8
+
9
+ class InvalidRegionError < Error
10
+ end
11
+
12
+ class InvalidConfigError < Error
13
+ end
14
+
15
+ end
@@ -1,3 +1,3 @@
1
1
  module WotApi
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/wot_api.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "wot_api/version"
2
2
  require "wot_api/base"
3
- #require "wot_api/player"
3
+ require "wot_api/exceptions"
4
4
 
5
5
  module WotApi; end
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 the correct base_uri" do
6
- expect(WotApi::Base.base_uri).to eq 'https://api.worldoftanks.com'
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 ".application_id=" do
25
- it "stores value to application_id class variable" do
26
- WotApi::Base.application_id = '12345'
27
- expect(WotApi::Base.application_id).to eq '12345'
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
- it "updates value in application_id when set multiple times" do
31
- WotApi::Base.application_id = 'aaaaa'
32
- expect(WotApi::Base.application_id).to eq 'aaaaa'
33
- WotApi::Base.application_id = '54321'
34
- expect(WotApi::Base.application_id).to eq '54321'
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
- #it "sets the httparty default_params hash application_id key" do
38
- # WotApi::Base.application_id = '22222'
39
- # expect(WotApi::Base.default_params[:application_id]).to eq '22222'
40
- #end
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
- it "merges the params hash argument with the application_id" do
53
- arguments = {a: 'hi', b: 'test', c: 3}
54
- WotApi::Base.application_id = 'abc123'
55
- expect(WotApi::Base.merged_params(arguments)).to eq arguments.merge(application_id: 'abc123')
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).exactly(1).times
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 = '123456'
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
- it "receives a response from api with data in an Array or Hash" do
80
- FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}#{endpoint}", :response => File.join(File.dirname(__FILE__), 'fixtures', "#{method_name}.json"))
81
- expect(WotApi::Base.send(method_name.to_sym)).to be_a_kind_of(Array).or be_a_kind_of(Hash)
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
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_runtime_dependency "httparty", "~> 0.13"
28
28
 
29
- spec.requirements << "Application ID from Wargaming developers portal."
29
+ spec.requirements << "Application ID(s) from Wargaming developers portal."
30
30
  end
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.1
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-10 00:00:00.000000000 Z
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/player.rb
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
@@ -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