Class: Jerakia::Server::Rest
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Jerakia::Server::Rest
- Defined in:
- lib/jerakia/server/rest.rb
Class Method Summary collapse
Instance Method Summary collapse
- #auth_denied ⇒ Object
- #authenticate! ⇒ Object
- #determine_content_type! ⇒ Object
- #encode_result(data) ⇒ Object
-
#initialize ⇒ Rest
constructor
A new instance of Rest.
- #jerakia ⇒ Object
- #mandatory_params(mandatory, params) ⇒ Object
- #request_failed(message, status_code = 501) ⇒ Object
- #token_ttl ⇒ Object
- #token_valid?(token) ⇒ Boolean
- #wrong_media_type(message, status_code = 415) ⇒ Object
Constructor Details
#initialize ⇒ Rest
Returns a new instance of Rest.
16 17 18 19 |
# File 'lib/jerakia/server/rest.rb', line 16 def initialize @authorized_tokens={} super end |
Class Method Details
Instance Method Details
#auth_denied ⇒ Object
25 26 27 |
# File 'lib/jerakia/server/rest.rb', line 25 def auth_denied request_failed('unauthorized', 401) end |
#authenticate! ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/jerakia/server/rest.rb', line 38 def authenticate! token = env['HTTP_X_AUTHENTICATION'] auth_denied if token.nil? return true if token_valid?(token) unless Jerakia::Server::Auth.authenticate(token) auth_denied end @authorized_tokens[token] = Time.now end |
#determine_content_type! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/jerakia/server/rest.rb', line 57 def determine_content_type! if not env.key?('CONTENT_TYPE') or env['CONTENT_TYPE'] == "application/json" content_type 'application/json' @content_type = :json elsif env['CONTENT_TYPE'] == "application/x-msgpack" content_type 'application/x-msgpack' @content_type = :msgpack else wrong_media_type("Content type #{env['CONTENT_TYPE']} not supported", 415) end end |
#encode_result(data) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/jerakia/server/rest.rb', line 48 def encode_result(data) case @content_type when :json return data.to_json when :msgpack return data.to_msgpack end end |
#jerakia ⇒ Object
21 22 23 |
# File 'lib/jerakia/server/rest.rb', line 21 def jerakia self.class.jerakia end |
#mandatory_params(mandatory, params) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/jerakia/server/rest.rb', line 88 def mandatory_params(mandatory, params) mandatory.each do |m| unless params.include?(m) request_failed("Must include parameter #{m} in request", 400) end end end |
#request_failed(message, status_code = 501) ⇒ Object
82 83 84 85 86 |
# File 'lib/jerakia/server/rest.rb', line 82 def request_failed(, status_code=501) halt(status_code, encode_result({ :status => 'failed', :message => })) end |
#token_ttl ⇒ Object
29 30 31 |
# File 'lib/jerakia/server/rest.rb', line 29 def token_ttl Jerakia::Server.config["token_ttl"] end |
#token_valid?(token) ⇒ Boolean
33 34 35 36 |
# File 'lib/jerakia/server/rest.rb', line 33 def token_valid?(token) return false unless @authorized_tokens[token].is_a?(Time) (Time.now - @authorized_tokens[token]) < token_ttl.to_i end |
#wrong_media_type(message, status_code = 415) ⇒ Object
78 79 80 |
# File 'lib/jerakia/server/rest.rb', line 78 def wrong_media_type(, status_code=415) halt(status_code, {'Content-Type' => 'text/plain'}, ) end |