class Sensi::Account

Attributes

auth_response[RW]
headers[RW]
token[RW]
token_response[RW]

Public Class Methods

new(username, password) click to toggle source
# File lib/sensi/account.rb, line 22
def initialize(username, password)
        @username = username
        @password = password
        self
end

Public Instance Methods

authorize(username, password) click to toggle source
# 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_token() click to toggle source
# 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
login() click to toggle source
# File lib/sensi/account.rb, line 28
def login
        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
logout() click to toggle source
# 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) click to toggle source
# 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