Class: ClashOfClansApi::TokenClient

Inherits:
Object
  • Object
show all
Defined in:
lib/clash_of_clans_api/token_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ TokenClient



10
11
12
13
# File 'lib/clash_of_clans_api/token_client.rb', line 10

def initialize(email, password)
  @email    = email
  @password = password
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



7
8
9
# File 'lib/clash_of_clans_api/token_client.rb', line 7

def email
  @email
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/clash_of_clans_api/token_client.rb', line 8

def password
  @password
end

Class Method Details

.create!(email, password) ⇒ Object



56
57
58
# File 'lib/clash_of_clans_api/token_client.rb', line 56

def create!(email, password)
  new(email, password).login!
end

Instance Method Details

#create_api_key(name, description, ip_addresses) ⇒ Object



43
44
45
46
47
# File 'lib/clash_of_clans_api/token_client.rb', line 43

def create_api_key(name, description, ip_addresses)
  response = TokenApi.apikey_create(name: name, description: description, ip_addresses: (ip_addresses.is_a?(Array) ? ip_addresses : [ip_addresses]), headers: @session_headers)
  
  Models::Token.new(response['key'], token_client: self)
end

#list_api_keysObject



35
36
37
38
39
40
41
# File 'lib/clash_of_clans_api/token_client.rb', line 35

def list_api_keys
  response = TokenApi.apikey_list(headers: @session_headers)
  
  response['keys'].map do |key|
    Models::Token.new(key, token_client: self)
  end
end

#login!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/clash_of_clans_api/token_client.rb', line 15

def login!
  TokenApi.(email: email, password: password, plain_response: true).tap do |response|
    if response.is_a?(Net::HTTPSuccess)
      cookies = CGI::Cookie.parse(response['set-cookie'])
      
      @session_headers = {
        'Cookie' => "session=#{cookies['session'][0]}",
      }
    else
      raise NoSuccessError, response
    end
  end
end

#logoutObject



29
30
31
32
33
# File 'lib/clash_of_clans_api/token_client.rb', line 29

def logout
  TokenApi.logout(headers: @session_headers)
ensure
  @session_headers = nil
end

#revoke_api_key(id) ⇒ Object



49
50
51
52
53
# File 'lib/clash_of_clans_api/token_client.rb', line 49

def revoke_api_key(id)
  TokenApi.apikey_revoke(id: id, headers: @session_headers)
  
  true
end