arkaan 0.8.12 → 0.8.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 274d18d1f23d156f2665a5bd1359fe482a0736cd
4
- data.tar.gz: 0db5765f3a5aec00ea6cbcd9e3f23828ee3a8eb9
3
+ metadata.gz: 322dc038e57c83ed444731f3ce872764dcf2d2b6
4
+ data.tar.gz: 81359044b199b30db67afbfcd4182a9352daf126
5
5
  SHA512:
6
- metadata.gz: 2d8915708a7e1a471ae84c6e8d7201ae742aaa1b524b59d67a7beb9f9b0baa4d4510fc8fbb2d8f4291d9d685c12976138c55c6e1f7be68f97444fd58637018ec
7
- data.tar.gz: a435fab17ee5dca22cf9a3de9c80b5c825f9aeda6e989cb060183e31c9c5c884d37844e445fb5726ab102c8dd88f14e2ed5ca72eb7b51cd4aeab2ad3a286ac6d
6
+ metadata.gz: 318e55d0c138f9af923633d0487365b0e362df2df0fc37f1dfb727f432ac0a30cc32e06967c5d7b767f9eef5007d7a573ea4c9612c5f9df8e55f0e46e63179cc
7
+ data.tar.gz: 90af48c1eba840737fed44e9885b2a3fd003e3b955cfd2b15665c38971129b8255676049d0718d5fc4459db1025fcf82f00a7eade04f438a86b67df263f49cde
@@ -2,6 +2,7 @@ require 'mongoid'
2
2
  require 'active_model'
3
3
  require 'active_support'
4
4
  require 'sinatra/base'
5
+ require 'sinatra/config_file'
5
6
 
6
7
  # Main module of the application, holding all the subsequent classes.
7
8
  # @author Vincent Courtois <courtois.vincent@outlook.com>
@@ -3,6 +3,7 @@ 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
+ register Sinatra::ConfigFile
6
7
 
7
8
  before do
8
9
  add_body_to_params
@@ -83,6 +84,31 @@ module Arkaan
83
84
  splitted = request.env['sinatra.route'].split(' ')
84
85
  return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
85
86
  end
87
+
88
+ # Loads the errors configuration file from the config folder.
89
+ # @param file [String] send __FILE__
90
+ def load_errors_from(file)
91
+ config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
92
+ end
93
+
94
+ # Halts the application and creates the returned body from the parameters and the errors config file.
95
+ # @param status [Integer] the HTTP status to halt the application with.
96
+ # @param field [String] the value of the :value key in the returned JSON body, corresponding to the field in error.
97
+ # @param error [String] the name of the error affecing the field.
98
+ # @param suffix [String] the eventual suffix of the URL in the doc.
99
+ def custom_error(status, field, error, suffix: '')
100
+ url = settings.errors[field][error]
101
+ halt status, {status: status, field: field, error: error, docs: "#{url}#{suffix}"}.to_json
102
+ end
103
+
104
+ # Halts the application with a Bad Request error affecting a field of a model.
105
+ # @param instance [Mongoid::Document] the document having a field in error.
106
+ # @param suffix [String] the eventual suffix of the URL in the doc.
107
+ def model_error(instance, suffix: '')
108
+ field = instance.errors.messages.keys.first
109
+ error = instance.errors.messages[field].first
110
+ custom_error(400, field, error, suffix: suffix)
111
+ end
86
112
  end
87
113
  end
88
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arkaan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.12
4
+ version: 0.8.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Courtois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-07 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - '='
179
179
  - !ruby/object:Gem::Version
180
180
  version: 2.0.1
181
+ - !ruby/object:Gem::Dependency
182
+ name: sinatra-contrib
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - '='
186
+ - !ruby/object:Gem::Version
187
+ version: 2.0.1
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - '='
193
+ - !ruby/object:Gem::Version
194
+ version: 2.0.1
181
195
  description: This gem holds the model layer for my table-top RPG games application.
182
196
  email: courtois.vincent@outlook.com
183
197
  executables: []