error_responder 0.1.2 → 0.1.3

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: a0f03a0c2ca978d3cf0c46b0279831fe688e3168
4
- data.tar.gz: 93b6c5e5214ada20ef702b958c5c7d098ce83a75
3
+ metadata.gz: fe2e88742a39fd8c0f5e8474bd4779b850ee53ba
4
+ data.tar.gz: 3d6bd8e66527e3e337b5f28945e2b5c2f88d1362
5
5
  SHA512:
6
- metadata.gz: daeacff19b90d5f0fd689dd3b910aceddc7faa38c32db0241fd91308076b403163ebd61c04caea48cdf163184a03e00bc85d84987f5af1b1a2b0d1e9d1fe4858
7
- data.tar.gz: 36bb15c49d6ec472530366565a2b09469e6a3222db12be6e829ba558c47e19d6fbe873d885fc5ad790361b7b374601794676252a89f37df12f365c4aa1f30147
6
+ metadata.gz: bdb111b60d949383bf999df88fad59e02af215b8e68c46b1e619d831b28e024d46e80cdf6594289b26c9aec749cb9a7c133ee00771c414df996f7ce97ee49de2
7
+ data.tar.gz: 3ea3a397c7da9f9671f0d951120021e010411bb1b241c74009736e1fe94788e80c5f66274dd966ab219a6cfec7e4a7cf0a43ba67363094cfeca249bcc5fdede5
data/README.md CHANGED
@@ -1,28 +1,88 @@
1
- # ErrorResponder
2
- Short description and motivation.
1
+ [![Gem Version](https://badge.fury.io/rb/error_responder.svg)](https://badge.fury.io/rb/error_responder)
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ ### Error responder for Rails API
4
+ #### Generator of standard HTTP responses and error serializer for models.
6
5
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
6
+ ### Getting Started
7
+
8
+ Add it to your Gemfile:
9
9
 
10
10
  ```ruby
11
11
  gem 'error_responder'
12
12
  ```
13
13
 
14
- And then execute:
15
- ```bash
16
- $ bundle
14
+ #### 1) Generate standard HTTP responses
15
+
16
+ ```ruby
17
+ err_respond(err_code, key: nil, message: nil)
18
+ ```
19
+
20
+
21
+ #### 2) Generate responce with model validation errors
22
+
23
+ ```ruby
24
+ serialize_errors(errors, options = {})
25
+ ```
26
+
27
+ ### Usage example
28
+
29
+ #### 1) Generate standard HTTP responses
30
+
31
+ ```ruby
32
+ err_respond 404
33
+ ```
34
+
35
+ Will be generated:
36
+
37
+ ```json
38
+ {
39
+ "status": "404",
40
+ "info": "Not Found",
41
+ "errors": {}
42
+ }
17
43
  ```
18
44
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install error_responder
45
+ You can pass a custom error message:
46
+ ```ruby
47
+ err_respond 404, key: 'user', message: 'Not present in database.'
48
+ ```
49
+
50
+ Will be generated:
51
+
52
+ ```json
53
+ {
54
+ "status": "404",
55
+ "info": "Not Found",
56
+ "errors": {
57
+ "user": "Not present in database."
58
+ }
59
+ }
22
60
  ```
23
61
 
24
- ## Contributing
25
- Contribution directions go here.
62
+ #### 2) Generate responce with model validation errors
63
+
64
+ ```ruby
65
+ @user = User.new(user_params)
66
+ if @user.save
67
+ # ...
68
+ else
69
+ serialize_errors(@user.errors)
70
+ end
71
+ ```
72
+
73
+ Will be generated (model errors):
74
+
75
+ ```json
76
+ {
77
+ "status": 409,
78
+ "info": "Conflict",
79
+ "errors": {
80
+ "username": "Username can't be blank.",
81
+ "first_name": "First name can't be blank.",
82
+ "last_name": "Last name can't be blank."
83
+ }
84
+ }
85
+ ```
26
86
 
27
87
  ## License
28
88
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -17,6 +17,8 @@ module ErrorResponder
17
17
  return if errors.nil?
18
18
 
19
19
  json = {}
20
+ json[:status] = 409
21
+ json[:info] = Rack::Utils::HTTP_STATUS_CODES[409]
20
22
  json[:errors] = {}
21
23
 
22
24
  errors.to_hash(true).each do |key, message|
@@ -1,3 +1,3 @@
1
1
  module ErrorResponder
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_responder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Stroganov