Class: Centro::Client

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

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



89
90
91
92
93
94
95
# File 'lib/centro/client.rb', line 89

def initialize(opts={})
  @client_id = opts.delete(:client_id) if opts[:client_id]
  @client_secret = opts.delete(:client_secret) if opts[:client_secret]
  @access_token = opts.delete(:access_token) if opts[:access_token]
  @basic_auth_username = opts.delete(:basic_auth_username) if opts[:basic_auth_username]
  @basic_auth_password = opts.delete(:basic_auth_password) if opts[:basic_auth_password]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



87
88
89
# File 'lib/centro/client.rb', line 87

def access_token
  @access_token
end

#access_token_expires_atObject

Returns the value of attribute access_token_expires_at.



87
88
89
# File 'lib/centro/client.rb', line 87

def access_token_expires_at
  @access_token_expires_at
end

#client_idObject

Returns the value of attribute client_id.



87
88
89
# File 'lib/centro/client.rb', line 87

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



87
88
89
# File 'lib/centro/client.rb', line 87

def client_secret
  @client_secret
end

Class Method Details

.api_versionObject



11
12
13
14
# File 'lib/centro/client.rb', line 11

def api_version
  return @api_version if defined?(@api_version)
  @api_version = 'v1'
end

.api_version=(api_version) ⇒ Object



16
17
18
# File 'lib/centro/client.rb', line 16

def api_version=(api_version)
  @api_version = api_version
end

.auth_hostObject



20
21
22
23
# File 'lib/centro/client.rb', line 20

def auth_host
  return @auth_host if defined?(@auth_host)
  @auth_host = 'accounts.centro.net'
end

.auth_host=(auth_host) ⇒ Object



25
26
27
# File 'lib/centro/client.rb', line 25

def auth_host=(auth_host)
  @auth_host = auth_host
end

.auth_urlObject



65
66
67
# File 'lib/centro/client.rb', line 65

def auth_url
  url_from_host(auth_host)
end

.finance_hostObject



29
30
31
32
# File 'lib/centro/client.rb', line 29

def finance_host
  return @finance_host if defined?(@finance_host)
  @finance_host = 'finance.transis.com'
end

.finance_host=(finance_host) ⇒ Object



34
35
36
# File 'lib/centro/client.rb', line 34

def finance_host=(finance_host)
  @finance_host = finance_host
end

.finance_urlObject



69
70
71
# File 'lib/centro/client.rb', line 69

def finance_url
  url_from_host(finance_host)
end

.mms_hostObject



38
39
40
41
# File 'lib/centro/client.rb', line 38

def mms_host
  return @mms_host if defined?(@mms_host)
  @mms_host = 'the.centro.net'
end

.mms_host=(mms_host) ⇒ Object



43
44
45
# File 'lib/centro/client.rb', line 43

def mms_host=(mms_host)
  @mms_host = mms_host
end

.mms_urlObject



73
74
75
# File 'lib/centro/client.rb', line 73

def mms_url
  url_from_host(mms_host)
end

.planner_hostObject



47
48
49
50
# File 'lib/centro/client.rb', line 47

def planner_host
  return @planner_host if defined?(@planner_host)
  @planner_host = 'planner.centro.net'
end

.planner_host=(planner_host) ⇒ Object



52
53
54
# File 'lib/centro/client.rb', line 52

def planner_host=(planner_host)
  @planner_host = planner_host
end

.planner_urlObject



77
78
79
# File 'lib/centro/client.rb', line 77

def planner_url
  url_from_host(planner_host)
end

.ssl_enabled=(ssl_enabled) ⇒ Object



61
62
63
# File 'lib/centro/client.rb', line 61

def ssl_enabled=(ssl_enabled)
  @ssl_enabled = ssl_enabled
end

.ssl_enabled?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/centro/client.rb', line 56

def ssl_enabled?
  return @ssl_enabled if defined?(@ssl_enabled)
  @ssl_enabled = true
end

.url_from_host(host) ⇒ Object



81
82
83
84
# File 'lib/centro/client.rb', line 81

def url_from_host(host)
  (ssl_enabled? ? 'https://' : 'http://') +
    host + "/api/#{self.api_version}"
end

Instance Method Details

#access_token_expired?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/centro/client.rb', line 106

def access_token_expired?
  access_token_expires_at.nil? || access_token_expires_at < Time.now
end

#create_organization(org_name) ⇒ Object



163
164
165
166
# File 'lib/centro/client.rb', line 163

def create_organization(org_name)
  response = auth_connection.post("organizations", {:name => org_name})
  response.success? && response.body
end

#create_user(email) ⇒ Object



158
159
160
161
# File 'lib/centro/client.rb', line 158

def create_user(email)
  response = auth_connection.post("members.json", {:email_address => email})
  response.success? && response.body
end

#create_user_in_organization(email, organization_id) ⇒ Object

Creates a user in an organization. If the user already exists in AuthTransis they will simply be added to the org (assuming the resource owner has rights), if they don’t exist they will be created and added to the organizations.

Returns a list of the organizations that they were sucessfully added to.



148
149
150
151
152
153
154
155
156
# File 'lib/centro/client.rb', line 148

def create_user_in_organization(email, organization_id)
  org_ids = organization_id.respond_to?(:each) ? organization_id : [organization_id]
  successfuls = []
  org_ids.each do |org_id|
    response = auth_connection.post("organizations/#{organization_id}/members.json", {:email_address => email})
    response.success? && successfuls << response.body
  end
  successfuls
end

#fetch(url) ⇒ Object



97
98
99
# File 'lib/centro/client.rb', line 97

def fetch(url)
  connection(url).get.body
end

#get_credentialsObject



128
129
130
# File 'lib/centro/client.rb', line 128

def get_credentials
  auth_connection.get("me.json").body
end

#get_members_of_organization(organization_id) ⇒ Object



138
139
140
# File 'lib/centro/client.rb', line 138

def get_members_of_organization(organization_id)
  auth_connection.get("organizations/#{organization_id}/members.json").body
end

#get_organizations(user_id = nil) ⇒ Object



132
133
134
135
136
# File 'lib/centro/client.rb', line 132

def get_organizations(user_id=nil)
  options = {}
  options[:user_id] = user_id if user_id
  auth_connection.get("organizations.json", options).body
end

#put(url, data) ⇒ Object

Data will be JSON-ified if it’s not already in string-form



102
103
104
# File 'lib/centro/client.rb', line 102

def put(url, data)
  connection(url).put("", data).body
end

#retrieve_access_token(username, password) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/centro/client.rb', line 110

def retrieve_access_token(username, password)
  raise 'client_id and client_secret required' unless @client_id && @client_secret

  connection = Faraday.new(self.class.auth_url) do |conn|
    set_default_connection_options(conn)
  end

  response = connection.post('/oauth/token',
             :grant_type    => 'password',
             :client_id     => @client_id,
             :client_secret => @client_secret,
             :username      => username,
             :password      => password)
  self.access_token_expires_at = Time.now + response.body.expires_in
  self.access_token = response.body.access_token
end