Class: Auth::Transis::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/auth-transis-client.rb,
lib/auth-transis-client/version.rb

Constant Summary collapse

VERSION =
"0.0.5"
API_VERSION =
"v1"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/auth-transis-client.rb', line 10

def initialize(opts={})
  fill_in_access_token!(opts)
  if opts[:site] && opts[:token]
    @connection = Faraday.new opts[:site] do |conn|
      conn.request :oauth2, opts[:token]
      conn.request :json

      conn.response :json, :content_type => /\bjson$/

      conn.adapter Faraday.default_adapter
    end
  else
    raise <<-ERROR
      Either provide :token or provide :site, :username, :password, :client_id, and :client_secret
    ERROR
  end
end

Instance Method Details

#create_organization(org_name) ⇒ Object



82
83
84
85
# File 'lib/auth-transis-client.rb', line 82

def create_organization(org_name)
  response = @connection.post("/api/#{API_VERSION}/organizations", {:name => org_name})
  response.success? && response.body
end

#create_user(email) ⇒ Object



77
78
79
80
# File 'lib/auth-transis-client.rb', line 77

def create_user(email)
  response = @connection.post("/api/#{API_VERSION}/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.



67
68
69
70
71
72
73
74
75
# File 'lib/auth-transis-client.rb', line 67

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 = @connection.post("/api/#{API_VERSION}/organizations/#{organization_id}/members.json", {:email_address => email})
    response.success? && successfuls << response.body
  end
  successfuls
end

#fill_in_access_token!(opts) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/auth-transis-client.rb', line 28

def fill_in_access_token!(opts)
  return if opts[:token]
  return unless opts[:site] && opts[:username] && opts[:client_id] && opts[:client_secret]
  connection = Faraday.new opts[:site] do |conn|
    conn.request :json
    conn.response :json, :content_type => /\bjson$/
    conn.use :instrumentation
    conn.adapter Faraday.default_adapter
  end
  response = connection.post('/oauth/token',
           :grant_type    => 'password',
           :client_id     => opts[:client_id],
           :client_secret => opts[:client_secret],
           :username      => opts[:username],
           :password      => opts[:password])
  raise 'Failed to get an access token' unless response.success? && response.body['access_token']
  opts[:token] = response.body['access_token']
end

#get_credentialsObject



47
48
49
# File 'lib/auth-transis-client.rb', line 47

def get_credentials
  @connection.get('/api/v1/me.json').body
end

#get_members_of_organization(organization_id) ⇒ Object



57
58
59
# File 'lib/auth-transis-client.rb', line 57

def get_members_of_organization(organization_id)
  @connection.get("/api/#{API_VERSION}/organizations/#{organization_id}/members.json").body
end

#get_organizations(user_id = nil) ⇒ Object



51
52
53
54
55
# File 'lib/auth-transis-client.rb', line 51

def get_organizations(user_id=nil)
  options = {}
  options[:user_id]=user_id if user_id
  @connection.get("/api/#{API_VERSION}/organizations.json", options).body
end