Module: Crowd::Client

Extended by:
Client
Included in:
Client
Defined in:
lib/crowd-client.rb,
lib/crowd-client/version.rb,
lib/crowd-client/exceptions.rb

Defined Under Namespace

Classes: Exception, Group, User

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#configObject



19
20
21
# File 'lib/crowd-client.rb', line 19

def config
  @config
end

#connectionObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/crowd-client.rb', line 64

def connection
  ::Faraday::Connection.new(
    :url        => config.url,
    :headers    => { :accept => 'application/json' },
    :user_agent => "Crowd Client for Ruby/#{VERSION}"
  ) do |builder|
      builder.request  :json
      builder.use Faraday::Response::Logger, config.logger if config.logger
      builder.use Faraday::Response::ParseJson
      builder.adapter  :patron
  end.tap {|connection| connection.basic_auth config.application, config.password }
end

#in_group?(username, group_name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/crowd-client.rb', line 56

def in_group?(username, group_name)
  response = connection.get("user/group/nested") do |request|
    request.params[:username] = username
    request.params[:groupname] = group_name
  end
  response.status == 200
end

#login(username, password) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/crowd-client.rb', line 23

def (username, password)
  response = connection.post('session', :username => username, :password => password)

  raise Exception::AuthenticationFailed.new if response.status == 400 && response.body['reason'] == 'INVALID_USER_AUTHENTICATION'
  raise Exception::InactiveAccount.new if response.status == 400 && response.body['reason'] == 'INACTIVE_ACCOUNT'
  raise Exception::UnknownError.new(response.body.to_s) if response.status != 201
  return response.body['token']
end

#logout(token) ⇒ Object



37
38
39
40
# File 'lib/crowd-client.rb', line 37

def logout(token)
  response = connection.delete("session/#{token}", {})
  raise Exception::UnknownError.new(response.body.to_s) if response.status != 204
end

#user(token) ⇒ Object



42
43
44
45
46
47
# File 'lib/crowd-client.rb', line 42

def user(token)
  response = connection.get("session/#{token}") do |request|
    request.params[:expand] = 'user'
  end
  response.body['user']
end

#user_groups(username) ⇒ Object



49
50
51
52
53
54
# File 'lib/crowd-client.rb', line 49

def user_groups(username)
  response = connection.get("user/group/nested") do |request|
    request.params[:username] = username
  end
  response.body['groups']
end

#valid_session?(token) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/crowd-client.rb', line 32

def valid_session?(token)
  response = connection.post("session/#{token}", {})
  response.status == 200
end