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, Logger, User

Constant Summary collapse

VERSION =
"0.1.6"

Instance Method Summary collapse

Instance Method Details

#configObject



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

def config
  @config
end

#connectionObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/crowd-client.rb', line 70

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 Crowd::Client::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)


62
63
64
65
66
67
68
# File 'lib/crowd-client.rb', line 62

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



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

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



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

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

#search(email) ⇒ Object



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

def search(email)
  response = connection.get("search?entity-type=user&restriction=" + CGI::escape("email=#{email}"))
  response.body['users']
end

#user(token) ⇒ Object



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

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

#user_groups(username) ⇒ Object



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

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)


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

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