Class: PUBG::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pubg/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, platform_region = "xbox-na") ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/pubg/client.rb', line 6

def initialize(api_key=nil, platform_region="xbox-na")
  # TODO: raise error if api_key nil
  $api_key = api_key || ENV["PUBG_API_KEY"]
  $platform_region = platform_region || ENV["PUBG_PLATFORM_REGION"]
end

Class Method Details

.request(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pubg/client.rb', line 54

def self.request(path)
  request = Typhoeus.get(
  [$base_url, path].join(""),
  headers: { 
       Accept: "application/vnd.api+json",
       Authorization: "Bearer #{$api_key}" 
     }
)

   response = Oj.load(request.body)

   case request.code
   when 404
     raise PUBError.new(response["errors"][0]["title"])
   when 429
     raise PUBError.new("RateLimit-Limit reached")
   end
   
   return response
end

Instance Method Details

#api_keyObject



12
13
14
# File 'lib/pubg/client.rb', line 12

def api_key
  $api_key
end

#match(platform_region = $platform_region, match_id) ⇒ Object



35
36
37
38
# File 'lib/pubg/client.rb', line 35

def match(platform_region=$platform_region, match_id)
  path = "/shards/#{platform_region}/matches/#{match_id}"
  PUBG::Match.new(Client.request(path))
end

#platform_regionObject



16
17
18
# File 'lib/pubg/client.rb', line 16

def platform_region
  $platform_region
end

#player(platform_region = $platform_region, player_id) ⇒ Object



20
21
22
23
# File 'lib/pubg/client.rb', line 20

def player(platform_region=$platform_region, player_id)
  path = "/shards/#{platform_region}/players/#{player_id}"
  PUBG::Player.new(Client.request(path))
end

#players(platform_region = $platform_region, items) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/pubg/client.rb', line 25

def players(platform_region=$platform_region, items)
  if items.include?("account.")
    params = "?filter[playerIds]=#{items}"
  else
    params = "?filter[playerNames]=#{items}"
  end
  path = "/shards/#{platform_region}/players#{params}"
  PUBG::Player.new(Client.request(path), true)
end

#seasons(platform_region = $platform_region) ⇒ Object



44
45
46
47
# File 'lib/pubg/client.rb', line 44

def seasons(platform_region=$platform_region)
  path = "/shards/#{platform_region}/seasons"
  PUBG::Seasons.new(Client.request(path))
end

#statusObject



49
50
51
52
# File 'lib/pubg/client.rb', line 49

def status
  path = "/status"
  PUBG::Status.new(Client.request(path))
end

#telemetry(url) ⇒ Object



40
41
42
# File 'lib/pubg/client.rb', line 40

def telemetry(url)
  PUBG::Telemetry.new(telemetry_request(url))
end

#telemetry_request(url) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pubg/client.rb', line 75

def telemetry_request(url)
  request = Typhoeus.get(
    url,
    headers: { 
      Accept: "application/vnd.api+json"
    }
  )

  response = Oj.load(request.body)

  case request.code
  when 404, 403
    raise PUBError.new("Cant find telemetry file")
  when 500
    raise PUBError.new("Server error")
  end

  return response
end