arkaan 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/arkaan/utils/controller.rb +22 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eebe941e4b49edb670452878122f9fdaab8f5d8a
4
- data.tar.gz: 2499a92b741d4a6caf46a72b8cd01ea6a2cd561b
3
+ metadata.gz: 3a31199bbce66658b871208d0b4225f242842912
4
+ data.tar.gz: b1c6d940b08a683cfeffd51858bf9177ae9cd3a5
5
5
  SHA512:
6
- metadata.gz: c707fd2f08439d13c3b309aabb506977fdfb6dfdf458ae19d850e256c577c38f92acc9857488cec7b7360990bff4ea528abb924116edc9ca76dd21ad710d3f4c
7
- data.tar.gz: bd34382eeaa742be1e29ed5d6b839ddb954d6984aa78ddfd1778a721555138d6eeb13726b50425e2c70d179b778a3fe7a69280df10d3ba66d17dce3bb99d1920
6
+ metadata.gz: a2769ae73df7c8280fd6f32e0f23f4b9b5d7a365dbad5c23f9e5df4cb751726145ab065e44074c4e7b498cd9833ef8388143a14ea11ed1838084bb6b5fd75df9
7
+ data.tar.gz: 008b88cd408d6cb199cf8dc005f8d7113bc9d5ee6de9e294b79896f8636562641df8cb27d8460ea69de507a0788dc8e52435ef23b551278bed0287bcea7393b1
@@ -3,13 +3,14 @@ module Arkaan
3
3
  # Base controller to handle the standard error when accessing the API.
4
4
  # @author Vincent Courtois <courtois.vincenet@outlook.com>
5
5
  class Controller < Sinatra::Base
6
+
6
7
  before do
7
- @parameters = JSON.parse(request.body.read.to_s) rescue {}
8
- if get_param('app_key').nil? || get_param('token').nil?
9
- halt 400, {message: 'bad_request'}.to_json
10
- end
11
- gateway = Arkaan::Monitoring::Gateway.where(token: get_param('token')).first
12
- application = Arkaan::OAuth::Application.where(key: get_param('app_key')).first
8
+ add_body_to_params
9
+ check_presence('token', 'app_key')
10
+
11
+ gateway = Arkaan::Monitoring::Gateway.where(token: params['token']).first
12
+ application = Arkaan::OAuth::Application.where(key: params['app_key']).first
13
+
13
14
  if gateway.nil?
14
15
  halt 404, {message: 'gateway_not_found'}.to_json
15
16
  elsif application.nil?
@@ -19,11 +20,21 @@ module Arkaan
19
20
  end
20
21
  end
21
22
 
22
- # Gets a parameters from the parsed body hash, or the parsed querystring hash.
23
- # @param [String] key - the key of the parameter to get.
24
- # @return [any] the value associated with the key.
25
- def get_param(key)
26
- return (params.nil? || params[key].nil? ? @parameters[key] : params[key])
23
+ # Checks the presence of several fields given as parameters and halts the execution if it's not present.
24
+ # @param fields [Array<String>] an array of fields names to search in the parameters
25
+ def check_presence(*fields)
26
+ fields.each do |field|
27
+ halt 400, {message: 'bad_request'}.to_json if params[field].nil?
28
+ end
29
+ end
30
+
31
+ # Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
32
+ # of the SON body if they have similar keys.
33
+ def add_body_to_params
34
+ parsed_body = JSON.parse(request.body.read.to_s) rescue {}
35
+ parsed_body.keys.each do |key|
36
+ params[key] = parsed_body[key]
37
+ end
27
38
  end
28
39
  end
29
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arkaan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Courtois