Class: WotApi::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/wot_api/base.rb

Constant Summary collapse

REGIONS =
{
  na: 'https://api.worldoftanks.com',
  ru: 'https://api.worldoftanks.ru',
  eu: 'https://api.worldoftanks.eu',
  asia: 'https://api.worldoftanks.asia',
  kr: 'https://api.worldoftanks.kr'
}
REGIONS2 =
{
  na: 'http://na.wargaming.net',
  ru: 'http://ru.wargaming.net',
  eu: 'http://eu.wargaming.net',
  asia: 'http://asia.wargaming.net',
  kr: 'http://kr.wargaming.net',
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



25
26
27
# File 'lib/wot_api/base.rb', line 25

def configuration
  @configuration
end

.default_regionObject (readonly)

Returns the value of attribute default_region.



26
27
28
# File 'lib/wot_api/base.rb', line 26

def default_region
  @default_region
end

Class Method Details

.clans_accounts(params) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wot_api/base.rb', line 43

def clans_accounts(params)
  params ||= {}
  raise WotApi::InvalidArguments unless params[:clan_id]
  if region = params.delete(:region).to_sym rescue nil
    base_uri = REGIONS2[region]
  else
    base_uri = REGIONS2[@default_region]
  end
  raise WotApi::InvalidRegionError unless base_uri
  self.base_uri base_uri
  response = WotApi::Base.get("/clans/#{params[:clan_id]}/accounts", headers: {"X-Requested-With"=> "XMLHttpRequest"})
  JSON.parse(response||"{}")['items']
end

.config(params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wot_api/base.rb', line 28

def config(params={})
  @configuration = {}
  @default_region = nil
  params.each do |conf|
    region = conf[0].to_sym
    application_id = conf[1]
    if REGIONS[region] && application_id
      @default_region ||= region
      @configuration[region] = {base_uri: REGIONS[region], application_id: application_id.to_s}
    else
      raise WotApi::InvalidConfigError
    end
  end
end

.merged_params(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wot_api/base.rb', line 61

def merged_params(params)
  params ||= {}
  raise WotApi::InvalidConfigError unless @configuration.class == Hash
  if region = params.delete(:region).to_sym rescue nil
    config = @configuration[region]
  else
    config = @configuration[@default_region]
  end
  base_uri = config[:base_uri] rescue nil
  application_id = config[:application_id] rescue nil
  raise WotApi::InvalidRegionError unless base_uri && application_id
  self.base_uri base_uri
  params.merge({application_id: application_id})
end

.merged_post(endpoint, params = {}) ⇒ Object



76
77
78
# File 'lib/wot_api/base.rb', line 76

def merged_post(endpoint, params={})
  WotApi::Base.post(endpoint, body: merged_params(params))
end

.method_missing(method_sym, *arguments, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wot_api/base.rb', line 80

def method_missing(method_sym, *arguments, &block)
  if !self.methods.include?(method_sym) && method_sym.to_s =~ /^([^_]*)_([^_]*)$/
    endpoint = "/wot/" + method_sym.to_s.gsub('_','/') + "/"
    begin
      response = merged_post(endpoint, arguments.first)
    rescue
      raise WotApi::ConnectionError
    end
    if response && response['data']
      return response['data']
    else
      message = 'Unknown Error'
      message = response['error']['message'] if response && response['error'] && response['error']['message']
      raise WotApi::ResponseError, message
    end
  else
    super
  end
end

.pathname(path) ⇒ Object



57
58
59
# File 'lib/wot_api/base.rb', line 57

def pathname(path)
  path.gsub(/(\/)+$/,'').gsub('/wot/','').gsub('/', '_')
end