Class: Sensi::Account

Inherits:
Object
  • Object
show all
Includes:
Sensi
Defined in:
lib/sensi/account.rb

Constant Summary

Constants included from Sensi

API_URI, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Account

Returns a new instance of Account.



22
23
24
25
26
# File 'lib/sensi/account.rb', line 22

def initialize(username, password)
	@username = username
	@password = password
	self
end

Instance Attribute Details

Returns the value of attribute auth_cookie.



11
12
13
# File 'lib/sensi/account.rb', line 11

def auth_cookie
  @auth_cookie
end

#auth_responseObject

Returns the value of attribute auth_response.



11
12
13
# File 'lib/sensi/account.rb', line 11

def auth_response
  @auth_response
end

Returns the value of attribute cookie_hash.



11
12
13
# File 'lib/sensi/account.rb', line 11

def cookie_hash
  @cookie_hash
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/sensi/account.rb', line 11

def headers
  @headers
end

#tokenObject

Returns the value of attribute token.



11
12
13
# File 'lib/sensi/account.rb', line 11

def token
  @token
end

#token_responseObject

Returns the value of attribute token_response.



11
12
13
# File 'lib/sensi/account.rb', line 11

def token_response
  @token_response
end

Instance Method Details



58
59
60
# File 'lib/sensi/account.rb', line 58

def add_auth_cookie_to_headers(auth_cookie)
		@headers = {'Accept' => 'application/json; version=1, */*; q=0.01','Cookie' => auth_cookie}
end

#authorize(username, password) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/sensi/account.rb', line 36

def authorize(username, password)
	@auth_response = HTTParty.post(
		Sensi::API_URI + '/api/authorize', 
		body: {UserName: username, Password: password}.to_json,
		headers: {'Accept' => 'application/json; version=1, */*; q=0.01', 'X-Requested-With' => 'XMLHttpRequest', 'Content-Type' => 'application/json'}
		)
end

#get_connection_tokenObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sensi/account.rb', line 44

def get_connection_token
	@token_response = Curl.get(Sensi::API_URI + '/realtime/negotiate') do |req|
		req.headers['Cookie'] = @auth_cookie
		req.headers['Accept'] = 'application/json; version=1, */*; q=0.01'
	end

	@token = JSON.parse(@token_response.body)['ConnectionToken']
	# @token_response = HTTParty.get(
	# 	Sensi::API_URI + '/realtime/negotiate',
	# 	headers: Sensi::DEFAULT_HEADERS
	# 	)
	# @token = @token_response['ConnectionToken']
end

#loginObject



28
29
30
31
32
33
34
# File 'lib/sensi/account.rb', line 28

def 
	authorize(@username, @password)
	parse_cookies(@auth_response.get_fields('Set-Cookie'))
	save_auth_cookie
	add_auth_cookie_to_headers(@auth_cookie)
	get_connection_token
end

#logoutObject



73
74
75
76
77
78
# File 'lib/sensi/account.rb', line 73

def logout
	HTTParty.delete(
		Sensi::API_URI + '/api/authorize',
		headers: {'Coookie' => @auth_cookie, 'Accept' => 'application/json; version=1, */*; q=0.01'}
		)
end

#parse_cookies(response_cookies) ⇒ Object



62
63
64
65
66
67
# File 'lib/sensi/account.rb', line 62

def parse_cookies(response_cookies)
	@cookie_hash = HTTParty::CookieHash.new
	response_cookies.each do |cookie|
		@cookie_hash.add_cookies(cookie)
	end
end


69
70
71
# File 'lib/sensi/account.rb', line 69

def save_auth_cookie
	@auth_cookie = @cookie_hash.to_cookie_string
end