Class: Visor::Auth::Server

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Common::Config, Common::Exception
Defined in:
lib/auth/server.rb

Overview

The VISoR Auth System (VAS) server class. This class supports all users management operations through the REST API implemented along the following routes.

Instance Method Summary collapse

Instance Method Details

#delete('/users/: access_key') ⇒ JSON

Delete an user and returns its information.

Parameters:

  • access_key (String)

    The wanted user access_key.

Returns:

  • (JSON)

    The already deleted user detailed information.

Raises:

  • (HTTP Error 404)

    User not found.



189
190
191
192
193
194
195
196
# File 'lib/auth/server.rb', line 189

delete '/users/:access_key' do |access_key|
  begin
    user = DB.delete_user(access_key)
    {user: user}.to_json
  rescue NotFound => e
    json_error 404, e.message
  end
end

#get('/users/: access_key') ⇒ JSON

Get information about a specific user.

{"image": {
    "_id":<_id>,
    "access_key":<access_key>,
    "secret_key":<secret_key>,
    "email":<email>,
    "created_at":<creation timestamp>,
    "updated_at":<update timestamp>
}}

Parameters:

  • access_key (String)

    The wanted user access_key.

Returns:

  • (JSON)

    The user detailed information.

Raises:

  • (HTTP Error 404)

    If user not found.



117
118
119
120
121
122
123
124
# File 'lib/auth/server.rb', line 117

get '/users/:access_key' do |access_key|
  begin
    user = DB.get_user(access_key)
    {user: user}.to_json
  rescue NotFound => e
    json_error 404, e.message
  end
end

#get('/users') ⇒ JSON

Get information about all registered users.

{ "users": [{
    "_id":<_id>,
    "access_key":<access_key>,
    "secret_key":<secret_key>,
    "email":<email>,
    "created_at":<creation timestamp>,
    "updated_at":<update timestamp>,
    }, ...]}

The following options can be passed as query parameters.

Parameters:

  • access_key (String)

    The user access_key.

  • email (String)

    The user email address.

  • created_at (Date)

    The image creation timestamp.

  • updated_at (Date)

    The image update timestamp.

Returns:

  • (JSON)

    The registered users information.

Raises:

  • (HTTP Error 404)

    If there is no registered users.



88
89
90
91
92
93
94
95
# File 'lib/auth/server.rb', line 88

get '/users' do
  begin
    users = DB.get_users(params)
    {users: users}.to_json
  rescue NotFound => e
    json_error 404, e.message
  end
end

#post('/users') ⇒ JSON

Registers a new user and returns its data.

Parameters:

  • http-body (JSON)

    The user information.

Returns:

  • (JSON)

    The already created user detailed information.

Raises:

  • (HTTP Error 400)

    User information validation errors.

  • (HTTP Error 404)

    User not found after registered.

  • (HTTP Error 409)

    access_key was already taken.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/auth/server.rb', line 139

post '/users' do
  begin
    info = JSON.parse(request.body.read, @parse_opts)
    user = DB.post_user(info[:user])
    {user: user}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue ArgumentError => e
    json_error 400, e.message
  rescue ConflictError => e
    json_error 409, e.message
  end
end

#put('/users/: access_key') ⇒ JSON

Update an existing user information and return it.

Parameters:

  • access_key (String)

    The wanted user access_key.

  • http-body (JSON)

    The user information.

Returns:

  • (JSON)

    The already updated user detailed information.

Raises:

  • (HTTP Error 400)

    User information validation errors.

  • (HTTP Error 404)

    User not found.



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/auth/server.rb', line 166

put '/users/:access_key' do |access_key|
  begin
    info = JSON.parse(request.body.read, @parse_opts)
    user = DB.put_user(access_key, info[:user])
    {user: user}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue ArgumentError => e
    json_error 400, e.message
  end
end