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.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



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

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]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



36
37
38
# File 'lib/centro/client.rb', line 36

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



36
37
38
# File 'lib/centro/client.rb', line 36

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



36
37
38
# File 'lib/centro/client.rb', line 36

def client_secret
  @client_secret
end

Class Method Details

.api_versionObject



8
9
10
11
# File 'lib/centro/client.rb', line 8

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

.auth_hostObject



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

def auth_host
  return @auth_host if defined?(@auth_host)
  @auth_host = 'localhost:3001'
end

.auth_urlObject



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

def auth_url
  url_from_host(auth_host)
end

.mms_hostObject



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

def mms_host
  return @mms_host if defined?(@mms_host)
  @mms_host = 'localhost:3000'
end

.mms_urlObject



27
28
29
# File 'lib/centro/client.rb', line 27

def mms_url
  url_from_host(mms_host)
end

.ssl_enabled?Boolean

Returns:

  • (Boolean)


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

def ssl_enabled?
  return @ssl_enabled if defined?(@ssl_enabled)
  @ssl_enaabled = false
end

.url_from_host(host) ⇒ Object



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

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

Instance Method Details

#create_organization(org_name) ⇒ Object



94
95
96
97
# File 'lib/centro/client.rb', line 94

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

#create_user(email) ⇒ Object



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

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.



79
80
81
82
83
84
85
86
87
# File 'lib/centro/client.rb', line 79

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

#get_credentialsObject



59
60
61
# File 'lib/centro/client.rb', line 59

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

#get_members_of_organization(organization_id) ⇒ Object



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

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

#get_organizations(user_id = nil) ⇒ Object



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

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

#retrieve_access_token(username, password) ⇒ Object



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

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 = response.body.access_token
end