Class: Client

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

Constant Summary collapse

@@bearer_token_retrieve_time =
3300

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bearer_tokenObject

Returns the value of attribute bearer_token.



3
4
5
# File 'lib/client/client.rb', line 3

def bearer_token
  @bearer_token
end

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/client/client.rb', line 3

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



3
4
5
# File 'lib/client/client.rb', line 3

def client_secret
  @client_secret
end

#instanceObject

Returns the value of attribute instance.



3
4
5
# File 'lib/client/client.rb', line 3

def instance
  @instance
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/client/client.rb', line 3

def password
  @password
end

#security_tokenObject

Returns the value of attribute security_token.



3
4
5
# File 'lib/client/client.rb', line 3

def security_token
  @security_token
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/client/client.rb', line 3

def username
  @username
end

Instance Method Details

#retrieve_and_refresh_bearer_tokenObject

def init(instance, client_id, client_secret, username, password, security_token)

unless instance.nil? or client_id.nil? or client_secret.nil? or usenrame.nil? or password.nil? or security_token.nil?
  @instance = instance
  @client_id = client_id
  @client_secret = client_secret
  @username = username
  @password = password
  @security_token = security_token
else
  raise "Unable to instantiate a client - instance, client_id, client_secret, username, password, or security_token are nil."
end

retrieve_and_refresh_bearer_token

end



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/client/client.rb', line 26

def retrieve_and_refresh_bearer_token
  oauth_url = "https://%s.salesforce.com/services/oauth2/token" % [@instance]
  data = {
    "grant_type": "password",
    "client_id": @client_id,
    "client_secret": @client_secret,
    "username": @username,
    "password": "%s%s" % [@password, @security_token]
  }

  response = RestClient.post oauth_url, data
  response_json = JSON.parse(response.body)

  @bearer_token = response_json["access_token"]
end