wot_api 1.1.0 → 1.2.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: f1fde6a4c40567dfc6ea8e99dc1ee523a6679c4c
4
- data.tar.gz: 2bf93cb39e4d3cf24136ef4803464da523e9b05c
3
+ metadata.gz: 3e826f62809406bc30070f6f9419724caf6e4741
4
+ data.tar.gz: 40f59f007e5e725b862214079cd892a520438ddd
5
5
  SHA512:
6
- metadata.gz: d42151882278a2dcad9ba63d810f7d34d3901eaf8d2668179b57bdcc8fb4a380d09701d301f32f5996e772293ff8dddde002ecd5e36a3854a9de2c785be50e6b
7
- data.tar.gz: aa61846e095267d40cd6ec8d79a464fa52cc064dc277af7f4656677cf652a66adc1263bffab0c8dba3add74bca04ec52fb702552f5dc3145b277d152ab827908
6
+ metadata.gz: 95dc8341de126a4553908b08f2a99f20be6be49980f278053f646e1e18548d89968b50751625066ea30c787ba0a4b9b0df481d76fe9e392f649a3238aed80554
7
+ data.tar.gz: 5381e7853d8fd3fdb6b425c6af946ee1272572c357bfeffe69dff2acabfc7064b4c71db0bce942296bf1fd3b3dc2044d2ea0ea46021dfcb628b77a800590d248
data/README.md CHANGED
@@ -28,7 +28,7 @@ NOTE: Other WoT geographical regions require different Application IDs
28
28
 
29
29
  Initialize the gem with your Application ID(s):
30
30
 
31
- WotApi::Base.config({na: '123456'})
31
+ WotApi.config({na: '123456'})
32
32
 
33
33
  The available regions are: :na, :ru, :eu, :asia, :kr
34
34
 
@@ -36,7 +36,7 @@ The first region specified becomes the default if no region is specified in endp
36
36
 
37
37
  If using Rails, it is recommended to create an initializer that looks something like:
38
38
 
39
- WotApi::Base.config(YAML.load_file("#{::Rails.root}/config/wot_api.yml"))
39
+ WotApi.config(YAML.load_file("#{::Rails.root}/config/wot_api.yml"))
40
40
 
41
41
  Along with a yaml file, config/wot_api.yml:
42
42
 
@@ -45,13 +45,21 @@ Along with a yaml file, config/wot_api.yml:
45
45
 
46
46
  Call endpoints like such:
47
47
 
48
- WotApi::Base.account_list(search: 'tank', region: :ru)
48
+ WotApi.account_list(search: 'tank', region: :ru)
49
+
50
+ Which wraps the '/wot/account/list' endpoint, with 'search' params and in the :ru region
49
51
 
50
52
  Will return an array or hash with the results, or throw an error with a message on a failure.
51
53
 
52
- ## Future plans
54
+ NOTE: Version 1.2.0 removes the need for all api methods to be called with "WotApi::Base.method", instead can just use "WotApi.method" for less keystrokes. However, the existing methods are still available at WotApi::Base for compatibility.
55
+
56
+ ## Clan member resources
57
+
58
+ There is an additional endpoint used to list clan member resource counts:
59
+
60
+ WotApi.clans_accounts(clan_id: "12345")
53
61
 
54
- Add class wrappers for endpoints with convenience methods for class relationships and such.
62
+ This will return an array of the clan members with their recent and total resource counts
55
63
 
56
64
  ## Contributing
57
65
 
data/lib/wot_api/base.rb CHANGED
@@ -1,102 +1,25 @@
1
- require "httparty"
2
-
1
+ # WotApi::Base wrapper for WotApi for compatibility, moved all real functionality directly to WotApi module
2
+ # This need never change - any future methods added do not need to be added here
3
3
  module WotApi
4
4
 
5
5
  class Base
6
- include HTTParty
7
-
8
- REGIONS = {
9
- na: 'https://api.worldoftanks.com',
10
- ru: 'https://api.worldoftanks.ru',
11
- eu: 'https://api.worldoftanks.eu',
12
- asia: 'https://api.worldoftanks.asia',
13
- kr: 'https://api.worldoftanks.kr'
14
- }
15
-
16
- REGIONS2 = {
17
- na: 'http://na.wargaming.net',
18
- ru: 'http://ru.wargaming.net',
19
- eu: 'http://eu.wargaming.net',
20
- asia: 'http://asia.wargaming.net',
21
- kr: 'http://kr.wargaming.net',
22
- }
23
-
24
- class << self
25
- attr_reader :configuration
26
- attr_reader :default_region
27
-
28
- def config(params={})
29
- @configuration = {}
30
- @default_region = nil
31
- params.each do |conf|
32
- region = conf[0].to_sym
33
- application_id = conf[1]
34
- if REGIONS[region] && application_id
35
- @default_region ||= region
36
- @configuration[region] = {base_uri: REGIONS[region], application_id: application_id.to_s}
37
- else
38
- raise WotApi::InvalidConfigError
39
- end
40
- end
41
- end
42
6
 
43
- def clans_accounts(params)
44
- params ||= {}
45
- raise WotApi::InvalidArguments unless params[:clan_id]
46
- if region = params.delete(:region).to_sym rescue nil
47
- base_uri = REGIONS2[region]
48
- else
49
- base_uri = REGIONS2[@default_region]
50
- end
51
- raise WotApi::InvalidRegionError unless base_uri
52
- self.base_uri base_uri
53
- response = WotApi::Base.get("/clans/#{params[:clan_id]}/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
54
- JSON.parse(response||"{}")['items']
55
- end
56
-
57
- def pathname(path)
58
- path.gsub(/(\/)+$/,'').gsub('/wot/','').gsub('/', '_')
59
- end
7
+ def self.config(params={})
8
+ WotApi.config(params)
9
+ end
60
10
 
61
- def merged_params(params)
62
- params ||= {}
63
- raise WotApi::InvalidConfigError unless @configuration.class == Hash
64
- if region = params.delete(:region).to_sym rescue nil
65
- config = @configuration[region]
66
- else
67
- config = @configuration[@default_region]
68
- end
69
- base_uri = config[:base_uri] rescue nil
70
- application_id = config[:application_id] rescue nil
71
- raise WotApi::InvalidRegionError unless base_uri && application_id
72
- self.base_uri base_uri
73
- params.merge({application_id: application_id})
74
- end
11
+ def self.method_missing(method_sym, *params, &block)
12
+ WotApi.method_missing(method_sym, *params, &block)
13
+ end
75
14
 
76
- def merged_post(endpoint, params={})
77
- WotApi::Base.post(endpoint, body: merged_params(params))
78
- end
15
+ def self.respond_to?(method_sym, include_private = false)
16
+ WotApi.respond_to?(method_sym, include_private)
17
+ end
79
18
 
80
- def method_missing(method_sym, *arguments, &block)
81
- if !self.methods.include?(method_sym) && method_sym.to_s =~ /^([^_]*)_([^_]*)$/
82
- endpoint = "/wot/" + method_sym.to_s.gsub('_','/') + "/"
83
- begin
84
- response = merged_post(endpoint, arguments.first)
85
- rescue
86
- raise WotApi::ConnectionError
87
- end
88
- if response && response['data']
89
- return response['data']
90
- else
91
- message = 'Unknown Error'
92
- message = response['error']['message'] if response && response['error'] && response['error']['message']
93
- raise WotApi::ResponseError, message
94
- end
95
- else
96
- super
97
- end
98
- end
19
+ def self.clans_accounts(params={})
20
+ WotApi.clans_accounts(params)
99
21
  end
22
+
100
23
  end
101
24
 
102
25
  end
@@ -1,3 +1,3 @@
1
1
  module WotApi
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,43 @@
1
+ require "httparty"
2
+
3
+ module WotApi
4
+
5
+ def self.config(params={})
6
+ WotApi::Wrapper.configuration = {}
7
+ WotApi::Wrapper.default_region = nil
8
+ params.each do |region, id|
9
+ region = region.to_sym
10
+ if WotApi::Wrapper::REGIONS[region] && id
11
+ WotApi::Wrapper.default_region ||= region
12
+ WotApi::Wrapper.configuration[region] = id.to_s
13
+ else
14
+ raise WotApi::InvalidConfigError, "Region: #{region} or ID: #{id} is invalid"
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.method_missing(method_sym, *params, &block)
20
+ if WotApi::Wrapper.valid_endpoint?(method_sym)
21
+ return WotApi::Wrapper.wot_api_post(method_sym, params.first)
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def self.respond_to?(method_sym, include_private = false)
28
+ if WotApi::Wrapper.valid_endpoint?(method_sym)
29
+ return true
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ # methods outside of 'typical' WoT API calls:
36
+
37
+ def self.clans_accounts(params={})
38
+ raise WotApi::InvalidArguments, ":clan_id required" unless params[:clan_id]
39
+ response = WotApi::Wrapper.wot_web_get("/clans/#{params[:clan_id]}/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
40
+ JSON.parse(response||"{}")['items']
41
+ end
42
+
43
+ end
@@ -0,0 +1,106 @@
1
+ require "httparty"
2
+
3
+ module WotApi
4
+
5
+ class Wrapper
6
+ include HTTParty
7
+
8
+ REGIONS = {
9
+ na: 'https://api.worldoftanks.com',
10
+ ru: 'https://api.worldoftanks.ru',
11
+ eu: 'https://api.worldoftanks.eu',
12
+ asia: 'https://api.worldoftanks.asia',
13
+ kr: 'https://api.worldoftanks.kr'
14
+ }
15
+
16
+ REGIONS_WEB = {
17
+ na: 'http://na.wargaming.net',
18
+ ru: 'http://ru.wargaming.net',
19
+ eu: 'http://eu.wargaming.net',
20
+ asia: 'http://asia.wargaming.net',
21
+ kr: 'http://kr.wargaming.net',
22
+ }
23
+
24
+ class << self
25
+
26
+ attr_accessor :configuration, :default_region
27
+
28
+ def wot_api_post(method_sym, params)
29
+ endpoint = method_to_endpoint(method_sym)
30
+ region = params.delete(:region).to_sym rescue get_default_region
31
+ set_base_uri(region)
32
+ begin
33
+ response = merged_post(endpoint, region, params)
34
+ rescue
35
+ raise WotApi::ConnectionError, "Could not connect to WoT endpoint #{endpoint.to_s}"
36
+ end
37
+ return format_response(response)
38
+ end
39
+
40
+ def wot_web_get(endpoint, params={})
41
+ region = params.delete(:region).to_sym rescue get_default_region
42
+ set_base_uri(region, true)
43
+ begin
44
+ return self.get(endpoint, params).parsed_response
45
+ rescue Exception => e
46
+ raise WotApi::ConnectionError, "ERROR with WoT web endpoint #{endpoint.to_s}"
47
+ end
48
+ end
49
+
50
+ def valid_endpoint?(method_sym)
51
+ !(method_sym.to_s =~ /^([^_]*)_([^_]*)$/).nil?
52
+ end
53
+
54
+ private
55
+
56
+ def method_to_endpoint(method_sym)
57
+ "/wot/" + method_sym.to_s.gsub('_','/') + "/"
58
+ end
59
+
60
+ def set_base_uri(region, web=false)
61
+ base_uri = web ? get_web_base_uri_for_region(region) : get_base_uri_for_region(region)
62
+ raise WotApi::InvalidRegionError unless base_uri
63
+ self.base_uri base_uri
64
+ end
65
+
66
+ def merged_params(params, region)
67
+ application_id = get_application_id_for_region(region)
68
+ raise WotApi::InvalidRegionError, "Invalid region specified: #{region}" unless application_id
69
+ params.merge({application_id: application_id})
70
+ end
71
+
72
+ def get_application_id_for_region(region)
73
+ raise WotApi::InvalidConfigError unless @configuration.class == Hash
74
+ return @configuration[region]
75
+ end
76
+
77
+ def merged_post(endpoint, region, params={})
78
+ self.post(endpoint, body: merged_params(params, region))
79
+ end
80
+
81
+ def get_web_base_uri_for_region(region)
82
+ return REGIONS_WEB[region]
83
+ end
84
+
85
+ def get_base_uri_for_region(region)
86
+ return REGIONS[region]
87
+ end
88
+
89
+ def get_default_region
90
+ raise WotApi::InvalidConfigError unless @default_region
91
+ return @default_region
92
+ end
93
+
94
+ def format_response(response)
95
+ if response && response['data']
96
+ return response['data']
97
+ end
98
+ message = 'Unknown Error'
99
+ message = response['error']['message'] if response && response['error'] && response['error']['message']
100
+ raise WotApi::ResponseError, "WoT API response error: #{message}"
101
+ end
102
+
103
+ end
104
+ end
105
+
106
+ end
data/lib/wot_api.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "wot_api/version"
2
+ require "wot_api/wot_api"
2
3
  require "wot_api/base"
4
+ require "wot_api/wrapper"
3
5
  require "wot_api/exceptions"
4
6
 
5
7
  module WotApi; end
data/spec/base_spec.rb CHANGED
@@ -2,173 +2,103 @@ require 'spec_helper'
2
2
 
3
3
  describe WotApi::Base do
4
4
 
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
8
- end
9
-
10
- it "includes HTTParty" do
11
- expect(WotApi::Base.new).to be_a_kind_of(HTTParty)
12
- end
13
-
14
- describe "REGIONS" do
15
- WotApi::Base::REGIONS.each do |region, url|
16
- describe "#{region}" do
17
- it "has a sym region key" do
18
- expect(region).to be_a Symbol
19
- end
20
-
21
- it "has a string value" do
22
- expect(url).to be_a String
23
- end
24
-
25
- it "uses https" do
26
- expect(URI.parse(url).scheme).to eq 'https'
27
- end
28
- end
29
- end
30
- end
31
-
32
- describe "REGIONS2" do
33
- WotApi::Base::REGIONS2.each do |region, url|
34
- describe "#{region}" do
35
- it "has a sym region key" do
36
- expect(region).to be_a Symbol
37
- end
38
-
39
- it "has a string value" do
40
- expect(url).to be_a String
41
- end
42
-
43
- it "uses http" do
44
- expect(URI.parse(url).scheme).to eq 'http'
45
- end
46
- end
47
- end
48
- end
49
-
50
- describe ".clans_accounts" do
51
- it "calls WotApi::Base.get with endpoint and clan_id" do
52
- WotApi::Base.config({'na' => '123456'})
53
- expect(WotApi::Base).to receive(:get).with("/clans/12345/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
54
- WotApi::Base.clans_accounts(clan_id: "12345")
55
- end
56
- end
57
-
58
- describe ".merged_post" do
59
- it "calls WotApi::Base.post with endpont and merged_params output" do
60
- params1 = {random: 'hash'}
61
- params2 = {misc: 'data'}
62
- endpoint = '/test/endpoint/'
63
- expect(WotApi::Base).to receive(:merged_params).with(params1).and_return(params2)
64
- expect(WotApi::Base).to receive(:post).with(endpoint, {body: params2})
65
- WotApi::Base.merged_post(endpoint, params1)
66
- end
67
- it "has default empty hash for params" do
68
- expect(WotApi::Base).to receive(:merged_params).with({}).and_return({})
69
- allow(WotApi::Base).to receive(:post)
70
- WotApi::Base.merged_post('/test/nothing')
71
- end
72
- end
73
-
74
5
  describe ".config" do
75
6
  context "with a valid config" do
76
- it "creates hash of regions with base_uri and application_id" do
7
+ it "writes valid configuration to WotApi::Wrapper.configuration" do
77
8
  WotApi::Base.config({'na' => '123456'})
78
- expect(WotApi::Base.configuration).to eq({na: {base_uri: 'https://api.worldoftanks.com', application_id: '123456'}})
9
+ expect(WotApi::Wrapper.configuration).to eq({na: '123456'})
79
10
  end
80
11
 
81
12
  it "sets first item as default region" do
82
13
  WotApi::Base.config({'na' => '123456'})
83
- expect(WotApi::Base.default_region).to eq :na
14
+ expect(WotApi::Wrapper.default_region).to eq :na
84
15
  WotApi::Base.config({'ru' => '444444','na' => '123456'})
85
- expect(WotApi::Base.default_region).to eq :ru
16
+ expect(WotApi::Wrapper.default_region).to eq :ru
86
17
  end
87
18
  end
88
19
 
89
20
  context "with an invalid config" do
90
21
  it "raises an error" do
91
- expect{WotApi::Base.config({lalala: 'fake'})}.to raise_error
22
+ expect{WotApi::Base.config({lalala: 'fake'})}.to raise_error WotApi::InvalidConfigError
92
23
  end
93
24
  end
94
25
  end
95
26
 
96
- describe ".merged_params" do
97
- context "with a valid region parameter" do
98
- it "merges the params hash argument with the application_id from the specified region" do
99
- arguments = {a: 'hi', b: 'test', c: 3, region: 'na'}
100
- WotApi::Base.config(na: 'abc123')
101
- expect(WotApi::Base.merged_params(arguments)).to eq({a: 'hi', b: 'test', c: 3}.merge(application_id: 'abc123'))
102
- end
27
+ describe ".method_missing" do
28
+ it "checks WotApi::Wrapper for valid_endpoint" do
29
+ expect(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
30
+ allow(WotApi::Wrapper).to receive(:wot_api_post)
31
+ WotApi::Base.fake_method
103
32
  end
104
33
 
105
- context "with an invalid region parameter" do
106
- it "raises an exception" do
107
- arguments = {a: 'hi', b: 'test', c: 3, region: 'banana'}
108
- WotApi::Base.config(na: 'abc123')
109
- expect{WotApi::Base.merged_params(arguments)}.to raise_error
34
+ context "valid_endpoint true" do
35
+ before(:example) do
36
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
110
37
  end
111
- end
112
38
 
113
- context "with no region parameter" do
114
- it "merges the params hash argument with the application_id from the default first region" do
115
- arguments = {a: 'hi', b: 'test', c: 3}
116
- WotApi::Base.config(na: 'abc123')
117
- expect(WotApi::Base.merged_params(arguments)).to eq arguments.merge(application_id: 'abc123')
39
+ it "calls WotApi::Wrapper.wot_api_post" do
40
+ expect(WotApi::Wrapper).to receive(:wot_api_post)
41
+ WotApi::Base.fake_method
42
+ end
43
+
44
+ it "calls WotApi::Wrapper.wot_api_post with method symbol and parameters" do
45
+ expect(WotApi::Wrapper).to receive(:wot_api_post).with(:fake_method, {hi: true})
46
+ WotApi::Base.fake_method(hi: true)
118
47
  end
119
48
  end
120
49
 
121
- it "sets base_uri" do
122
- WotApi::Base.config(na: 'abc123')
123
- WotApi::Base.merged_params({})
124
- expect(WotApi::Base.base_uri).to eq WotApi::Base::REGIONS[:na]
50
+ context "valid_endpoint false" do
51
+ before(:example) do
52
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(false)
53
+ end
125
54
 
126
- WotApi::Base.config(ru: 'abc123')
127
- WotApi::Base.merged_params({})
128
- expect(WotApi::Base.base_uri).to eq WotApi::Base::REGIONS[:ru]
55
+ it "raises NoMethodError" do
56
+ expect{ WotApi::Base.fake_method(stuff: true) }.to raise_error NoMethodError
57
+ end
129
58
  end
130
59
  end
131
60
 
132
- describe "endpoint methods via method_missing" do
133
- before(:example) do
134
- WotApi::Base.config({na: '123456'})
135
- end
136
-
137
- it "posts to the specified endpoint" do
138
- expect(WotApi::Base).to receive(:post).with("/wot/thing/stuff/", kind_of(Hash)).and_return({'data' => true})
139
- WotApi::Base.thing_stuff(a: 'test')
61
+ describe ".respond_to?" do
62
+ it "checks WotApi::Wrapper for valid_endpoint" do
63
+ expect(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
64
+ WotApi::Base.respond_to?(:fake_method)
140
65
  end
141
66
 
142
- it "accepts a hash of arguments to post to the endpoint and merges them with the application_id" do
143
- arguments = {a: 'hi', b: 'test', c: 3}
144
- expect(WotApi::Base).to receive(:post).with("/wot/thing/stuff/", body: arguments.merge(application_id: '123456')).and_return({'data' => true})
145
- WotApi::Base.thing_stuff(arguments)
146
- end
67
+ context "valid_endpoint true" do
68
+ before(:example) do
69
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
70
+ end
147
71
 
148
- context "with a valid response" do
149
- it "receives a response from api with data in an Array or Hash" do
150
- FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}/wot/thing/stuff/", :response => File.join(File.dirname(__FILE__), 'fixtures', "success.json"))
151
- expect(WotApi::Base.thing_stuff).to be_a_kind_of(Array)
72
+ it "returns true" do
73
+ expect(WotApi::Base.respond_to?(:fake_method)).to eq true
152
74
  end
153
75
  end
154
76
 
155
- context "with an invalid response" do
156
- it "raises an exception" do
157
- FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}/wot/thing/stuff/", :response => File.join(File.dirname(__FILE__), 'fixtures', "error.json"))
158
- expect{WotApi::Base.thing_stuff}.to raise_error(WotApi::ResponseError)
77
+ context "valid_endpoint false" do
78
+ before(:example) do
79
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(false)
159
80
  end
160
81
 
161
- it "includes the error message from WoT in the exception" do
162
- FakeWeb.register_uri(:post, "#{WotApi::Base.base_uri}/wot/thing/stuff/", :response => File.join(File.dirname(__FILE__), 'fixtures', "error.json"))
163
- expect{WotApi::Base.thing_stuff}.to raise_error(WotApi::ResponseError, 'APPLICATION_ID_NOT_SPECIFIED')
82
+ it "returns false" do
83
+ expect(WotApi::Base.respond_to?(:fake_method)).to eq false
164
84
  end
165
85
  end
86
+ end
166
87
 
167
- context "with an invalid endpoint" do
168
- it "raises connection exception" do
169
- allow(WotApi::Base).to receive(:post).and_raise(HTTParty::Error)
170
- expect{WotApi::Base.thing_fake}.to raise_error(WotApi::ConnectionError)
171
- end
88
+ describe ".clans_accounts" do
89
+ it "raises WotApi::InvalidArguments if clan_id not in arguments" do
90
+ expect{ WotApi::Base.clans_accounts }.to raise_error WotApi::InvalidArguments
91
+ end
92
+
93
+ it "calls WotApi::Wrapper.wot_web_get with endpoint and clan_id" do
94
+ expect(WotApi::Wrapper).to receive(:wot_web_get).with("/clans/12345/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
95
+ WotApi::Base.clans_accounts(clan_id: "12345")
96
+ end
97
+
98
+ it "returns parsed JSON" do
99
+ allow(WotApi::Wrapper).to receive(:wot_web_get).and_return({items: [1,2,3]}.to_json)
100
+ expect(WotApi::Base.clans_accounts(clan_id: "12345")).to eq [1,2,3]
172
101
  end
173
102
  end
103
+
174
104
  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/spec/spec_helper.rb CHANGED
@@ -1,5 +1,11 @@
1
+ require 'simplecov'
1
2
  require 'coveralls'
2
- Coveralls.wear!
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
3
9
 
4
10
  require 'bundler/setup'
5
11
  Bundler.setup
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe WotApi do
4
+
5
+ describe ".config" do
6
+ context "with a valid config" do
7
+ it "writes valid configuration to WotApi::Wrapper.configuration" do
8
+ WotApi.config({'na' => '123456'})
9
+ expect(WotApi::Wrapper.configuration).to eq({na: '123456'})
10
+ end
11
+
12
+ it "sets first item as default region" do
13
+ WotApi.config({'na' => '123456'})
14
+ expect(WotApi::Wrapper.default_region).to eq :na
15
+ WotApi.config({'ru' => '444444','na' => '123456'})
16
+ expect(WotApi::Wrapper.default_region).to eq :ru
17
+ end
18
+ end
19
+
20
+ context "with an invalid config" do
21
+ it "raises an error" do
22
+ expect{WotApi.config({lalala: 'fake'})}.to raise_error WotApi::InvalidConfigError
23
+ end
24
+ end
25
+ end
26
+
27
+ describe ".method_missing" do
28
+ it "checks WotApi::Wrapper for valid_endpoint" do
29
+ expect(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
30
+ allow(WotApi::Wrapper).to receive(:wot_api_post)
31
+ WotApi.fake_method
32
+ end
33
+
34
+ context "valid_endpoint true" do
35
+ before(:example) do
36
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
37
+ end
38
+
39
+ it "calls WotApi::Wrapper.wot_api_post" do
40
+ expect(WotApi::Wrapper).to receive(:wot_api_post)
41
+ WotApi.fake_method
42
+ end
43
+
44
+ it "calls WotApi::Wrapper.wot_api_post with method symbol and parameters" do
45
+ expect(WotApi::Wrapper).to receive(:wot_api_post).with(:fake_method, {hi: true})
46
+ WotApi.fake_method(hi: true)
47
+ end
48
+ end
49
+
50
+ context "valid_endpoint false" do
51
+ before(:example) do
52
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(false)
53
+ end
54
+
55
+ it "raises NoMethodError" do
56
+ expect{ WotApi.fake_method(stuff: true) }.to raise_error NoMethodError
57
+ end
58
+ end
59
+ end
60
+
61
+ describe ".respond_to?" do
62
+ it "checks WotApi::Wrapper for valid_endpoint" do
63
+ expect(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
64
+ WotApi.respond_to?(:fake_method)
65
+ end
66
+
67
+ context "valid_endpoint true" do
68
+ before(:example) do
69
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(true)
70
+ end
71
+
72
+ it "returns true" do
73
+ expect(WotApi.respond_to?(:fake_method)).to eq true
74
+ end
75
+ end
76
+
77
+ context "valid_endpoint false" do
78
+ before(:example) do
79
+ allow(WotApi::Wrapper).to receive(:valid_endpoint?).with(:fake_method).and_return(false)
80
+ end
81
+
82
+ it "returns false" do
83
+ expect(WotApi.respond_to?(:fake_method)).to eq false
84
+ end
85
+ end
86
+ end
87
+
88
+ describe ".clans_accounts" do
89
+ it "raises WotApi::InvalidArguments if clan_id not in arguments" do
90
+ expect{ WotApi.clans_accounts }.to raise_error WotApi::InvalidArguments
91
+ end
92
+
93
+ it "calls WotApi::Wrapper.wot_web_get with endpoint and clan_id" do
94
+ expect(WotApi::Wrapper).to receive(:wot_web_get).with("/clans/12345/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
95
+ WotApi.clans_accounts(clan_id: "12345")
96
+ end
97
+
98
+ it "returns parsed JSON" do
99
+ allow(WotApi::Wrapper).to receive(:wot_web_get).and_return({items: [1,2,3]}.to_json)
100
+ expect(WotApi.clans_accounts(clan_id: "12345")).to eq [1,2,3]
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe WotApi::Wrapper do
4
+
5
+ it { is_expected.to be_a_kind_of(HTTParty) }
6
+
7
+ describe ".wot_api_post" do
8
+ context "with a connection error" do
9
+ before(:example) do
10
+ FakeWeb.clean_registry
11
+ WotApi.config({'na' => '123456'})
12
+ end
13
+ it "raises WotApi::ConnectionError" do
14
+ expect{ WotApi::Wrapper.wot_api_post(:thing_stuff, {}) }.to raise_error WotApi::ConnectionError
15
+ end
16
+ end
17
+
18
+ context "with invalid endpoint" do
19
+ before(:example) do
20
+ FakeWeb.register_uri(:post, "https://api.worldoftanks.com/wot/thing/stuff/", :response => File.join(File.dirname(__FILE__), 'fixtures', "error.json"))
21
+ WotApi.config({'na' => '123456'})
22
+ end
23
+ it "raises WotApi::ResponseError" do
24
+ expect{ WotApi::Wrapper.wot_api_post(:thing_stuff, {}) }.to raise_error WotApi::ResponseError
25
+ end
26
+ end
27
+
28
+ context "with valid endpoint" do
29
+ before(:example) do
30
+ FakeWeb.register_uri(:post, "https://api.worldoftanks.com/wot/thing/stuff/", :response => File.join(File.dirname(__FILE__), 'fixtures', "success.json"))
31
+ WotApi.config({'na' => '123456'})
32
+ end
33
+ it "raises WotApi::ResponseError" do
34
+ expect(WotApi::Wrapper.wot_api_post(:thing_stuff, {})).to be_an Array
35
+ end
36
+ end
37
+ end
38
+
39
+ describe ".wot_web_get" do
40
+ context "with a connection error" do
41
+ before(:example) do
42
+ FakeWeb.clean_registry
43
+ WotApi.config({'na' => '123456'})
44
+ end
45
+ it "raises WotApi::ConnectionError" do
46
+ expect{ WotApi::Wrapper.wot_web_get('/endpoint', {}) }.to raise_error WotApi::ConnectionError
47
+ end
48
+ end
49
+
50
+ context "with a valid endpoint" do
51
+ before(:example) do
52
+ FakeWeb.register_uri(:get, "http://na.wargaming.net/endpoint", :response => File.join(File.dirname(__FILE__), 'fixtures', "success.json"))
53
+ WotApi.config({'na' => '123456'})
54
+ end
55
+ it "returns result of get" do
56
+ expect(WotApi::Wrapper.wot_web_get('/endpoint', {})).to eq({"status"=>"ok","count"=>0,"data"=>[]})
57
+ end
58
+ end
59
+ end
60
+
61
+ describe ".valid_endpoint?" do
62
+ it "returns true for endponts like prefix _underscore_ suffix" do
63
+ expect(WotApi::Wrapper.valid_endpoint?(:test_things)).to eq true
64
+ end
65
+ end
66
+
67
+ end
data/wot_api.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec", "~> 3.3.0"
24
24
  spec.add_development_dependency "fakeweb", "~> 1.3"
25
25
  spec.add_development_dependency "coveralls"
26
+ spec.add_development_dependency "simplecov"
26
27
 
27
28
  spec.add_runtime_dependency "httparty", "~> 0.13"
28
29
 
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: 1.1.0
4
+ version: 1.2.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: 2015-08-02 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: httparty
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -112,10 +126,15 @@ files:
112
126
  - lib/wot_api/base.rb
113
127
  - lib/wot_api/exceptions.rb
114
128
  - lib/wot_api/version.rb
129
+ - lib/wot_api/wot_api.rb
130
+ - lib/wot_api/wrapper.rb
115
131
  - spec/base_spec.rb
132
+ - spec/fixtures/connection_error.json
116
133
  - spec/fixtures/error.json
117
134
  - spec/fixtures/success.json
118
135
  - spec/spec_helper.rb
136
+ - spec/wot_api_spec.rb
137
+ - spec/wrapper_spec.rb
119
138
  - wot_api.gemspec
120
139
  homepage: https://github.com/jcantara/wot_api
121
140
  licenses:
@@ -144,6 +163,9 @@ specification_version: 4
144
163
  summary: API wrapper for 'World of Tanks'.
145
164
  test_files:
146
165
  - spec/base_spec.rb
166
+ - spec/fixtures/connection_error.json
147
167
  - spec/fixtures/error.json
148
168
  - spec/fixtures/success.json
149
169
  - spec/spec_helper.rb
170
+ - spec/wot_api_spec.rb
171
+ - spec/wrapper_spec.rb