Class: CampaignMaster::Client

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

Constant Summary collapse

AuthenticationError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, id) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
# File 'lib/campaign_master/client.rb', line 7

def initialize(username, password, id)
  @login_expiration_seconds = -1
  @security_token = false

  self.username  = username
  self.password  = password
  self.id        = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/campaign_master/client.rb', line 5

def id
  @id
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/campaign_master/client.rb', line 5

def password
  @password
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/campaign_master/client.rb', line 5

def username
  @username
end

Instance Method Details

#call(class_name) ⇒ Object



37
38
39
# File 'lib/campaign_master/client.rb', line 37

def call(class_name)
  CampaignMaster.const_get(class_name).new(self)
end

#get_security_tokenObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/campaign_master/client.rb', line 16

def get_security_token
  if self.token_expired
    soap_client = CampaignMaster::SoapClient.get_client()
    response = soap_client.call(:login,
        message: { clientID: self.id, userName: self.username, password: self.password },
        :attributes => CampaignMaster::SoapClient.attributes
    )

    raise AuthenticationError if !self.valid_response?(response)

    @login_expiration_seconds = Time.now.to_i + response.body[:login_response][:login_result][:minutes_till_token_expires].to_i * 60
    @security_token = response.body[:login_response][:login_result][:token_string]
  end

  @security_token
end

#token_expiredObject



33
34
35
# File 'lib/campaign_master/client.rb', line 33

def token_expired
  @login_expiration_seconds < Time.now.to_i
end