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

Returns a new instance of 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



70
71
72
# File 'lib/clash_of_clans_api/token_client.rb', line 70

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'], self)
end

#create_or_get_api_key_for_current_ipv4_address(name, description = name, overwrite: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/clash_of_clans_api/token_client.rb', line 55

def create_or_get_api_key_for_current_ipv4_address(name, description=name, overwrite: false)
	current_ipv4 = ClashOfClansApi::Utils.current_ipv4_address
	token        = list_api_keys.select{ |i| i.name == name }.first
	
	if token && token.cidr_ranges.include?(current_ipv4)
		token
	elsif (token && overwrite) || !token
		token&.revoke
		create_api_key(name, description, current_ipv4)
	else
		false
	end
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, 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 ApiFrame::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