Class: Paladins::Client

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

Constant Summary collapse

@@session =

include MonitorMixin

{ id: nil, updated_at: nil }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/paladins/client.rb', line 14

def initialize(configuration)
  @api_url     = configuration.api_url
  @dev_id      = configuration.dev_id
  @auth_key    = configuration.auth_key
  @response_format = configuration.response_format
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

#auth_keyObject

Returns the value of attribute auth_key.



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

def auth_key
  @auth_key
end

#dev_idObject

Returns the value of attribute dev_id.



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

def dev_id
  @dev_id
end

#response_formatObject

Returns the value of attribute response_format.



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

def response_format
  @response_format
end

Instance Method Details

#create_sessionObject



26
27
28
29
30
31
# File 'lib/paladins/client.rb', line 26

def create_session
  # /createsession[ResponseFormat]/{developerId}/{signature}/{timestamp}
  response = Faraday.get(url(method: 'createsession', session: false))
  @@session[:updated_at] = Time.now
  @@session[:id] = JSON.parse(response.body).dig('session_id')
end

#get_champion_ranks(player_name) ⇒ Object



68
69
70
71
72
# File 'lib/paladins/client.rb', line 68

def get_champion_ranks(player_name)
  # /getchampionranks[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}/{player}
  response = Faraday.get(url(method: 'getchampionranks', session: true) + "/#{player_name}")
  attributes = JSON.parse(response.body)
end

#get_hirez_server_statusObject



62
63
64
65
66
# File 'lib/paladins/client.rb', line 62

def get_hirez_server_status
  # /gethirezserverstatus[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}
  response = Faraday.get(url(method: 'gethirezserverstatus', session: true))
  attributes = JSON.parse(response.body)
end

#get_match_player_details(match_id) ⇒ Object



86
87
88
89
90
# File 'lib/paladins/client.rb', line 86

def get_match_player_details(match_id)
  # /getmatchplayerdetails[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}/{match_id}
  response = Faraday.get(url(method: 'getmatchplayerdetails', session: true) + "/#{match_id}")
  attributes = JSON.parse(response.body)
end

#get_or_create_sessionObject



52
53
54
# File 'lib/paladins/client.rb', line 52

def get_or_create_session
  ( @@session[:id].nil? || session_expired? ) ? create_session : @@session[:id]
end

#get_player(player_name) ⇒ Object



74
75
76
77
78
# File 'lib/paladins/client.rb', line 74

def get_player(player_name)
  # /getplayer[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}/{player}
  response = Faraday.get(url(method: 'getplayer', session: true) + "/#{player_name}")
  attributes = JSON.parse(response.body)
end

#get_player_status(player_name) ⇒ Object



80
81
82
83
84
# File 'lib/paladins/client.rb', line 80

def get_player_status(player_name)
  # /getplayerstatus[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}/{player}
  response = Faraday.get(url(method: 'getplayerstatus', session: true) + "/#{player_name}")
  attributes = JSON.parse(response.body)
end

#get_signature(method_name, time_now) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
# File 'lib/paladins/client.rb', line 21

def get_signature(method_name, time_now)
  raise ArgumentError.new('Missing dev_id or auth_key') if ( @dev_id.to_s.empty? || @auth_key.to_s.empty? )
  Digest::MD5.hexdigest(@dev_id + method_name + @auth_key + time_now);
end

#pingObject



56
57
58
59
60
# File 'lib/paladins/client.rb', line 56

def ping
  # /ping[ResponseFormat]
  response = Faraday.get("#{@api_url}/pingJson")
  attributes = JSON.parse(response.body)
end

#session_expired?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paladins/client.rb', line 40

def session_expired?
  # synchronize secure taht only one thread at a time can access @@session
  self.synchronize do
    if Time.now - @@session[:updated_at] > 60*12
      @@session[:updated_at] = Time.now
      true
    else
      false
    end
  end
end

#test_sessionObject



33
34
35
36
37
38
# File 'lib/paladins/client.rb', line 33

def test_session
  # /testsession[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}
  time_now = Paladins::Utils.time_now
  response = Faraday.get("#{@api_url}/testsessionJson/#{@dev_id}/#{get_signature('testsession', time_now)}/#{@@session[:id]}/#{time_now}")
  attributes = JSON.parse(response.body)
end

#url(method:, session: false) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/paladins/client.rb', line 92

def url(method:, session: false)
  time_now = Paladins::Utils.time_now
  url = "#{@api_url}/#{method}#{@response_format}/#{@dev_id}/#{get_signature(method, time_now)}"
  url += "/#{get_or_create_session}" if session
  url += "/#{time_now}"

  url
end