class GamerStats::Bf3::Player

Public Class Methods

new(name, platform, player = {}) click to toggle source
# File lib/bf3/bf3.rb, line 9
def initialize(name, platform, player = {})
  @name = name
  @platform = platform
  @player = player.deep_dup
end

Public Instance Methods

current() click to toggle source
# File lib/bf3/bf3.rb, line 15
def current
  get_current
end
kdr() click to toggle source
# File lib/bf3/bf3.rb, line 39
def kdr
  load
  return get_current('stats/global/kills').to_f / get_current('stats/global/deaths')
end
load(opt='clear,global') click to toggle source
# File lib/bf3/bf3.rb, line 34
def load(opt='clear,global')
  raw_load(opt) unless loaded?
  get_current
end
load!(opt='clear,global') click to toggle source
# File lib/bf3/bf3.rb, line 29
def load!(opt='clear,global')
  raw_load(opt)
  get_current
end
stats() click to toggle source
# File lib/bf3/bf3.rb, line 24
def stats
  raw_load unless loaded? 'stats'
  get_current 'stats'
end
stats!() click to toggle source
# File lib/bf3/bf3.rb, line 19
def stats!
  raw_load
  get_current 'stats'
end

Private Instance Methods

get_current(path='') click to toggle source
# File lib/bf3/bf3.rb, line 51
def get_current(path='')
  @player.deep_dup.path(path)
end
loaded?(path='') click to toggle source
# File lib/bf3/bf3.rb, line 46
def loaded?(path='')
  val = @player.path(path)
  false if val.nil? or val.empty?
end
merge(player) click to toggle source
# File lib/bf3/bf3.rb, line 55
def merge(player)
  @player.deep_merge player
end
raw_load(opt='clear,global') click to toggle source
# File lib/bf3/bf3.rb, line 59
def raw_load(opt='clear,global')
  body = {
    player: @name,
    output: 'json',
    opt: opt
  }

  begin
    response = HTTParty.post("http://api.bf3stats.com/#{@platform}/player/", :body => body, timeout: 5)
  rescue => e
    raise GamerStatsError, "Error on loading the player: #{e.message}"
  end
  
  if response.code == 200 && response['status'] == "data"
    @player = merge JSON(response.body)
  else
    raise GamerStatsError, "Bf3: #{response['error']}"
  end
end