json_error 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8d02b5a8dc09b18176bde4e04b523d218f255cf
4
- data.tar.gz: 7bf5c086a9d80d91ceff09d6d430dbafabbe6768
3
+ metadata.gz: 28a81b856dd02155dd289ec81a832f45b29b4f97
4
+ data.tar.gz: f06bd7600097f2b491bfc5b54185fe84bc047551
5
5
  SHA512:
6
- metadata.gz: 462ecb79ea50676654177e3aef1474faf926393696cce610929ffe9ca27c3f202c700a5304733115598da6895249b75d19345640ed72540e28a7051073c6ac6c
7
- data.tar.gz: 14cabc02842c091ec6cb8af643ec2677293a14cd42dc55d8fa3d067b2d87427b18765f873b60ca747cafd0552c1a4d601fd09b9388f1ea1fdc2ffe68548f6f1a
6
+ metadata.gz: 994b65539357730f815ec54b1d21eace76d8b2d1dd6e3982cfc869c81b73e757bbe80ea4a1f5f946a0a78facbc12397caf1930baaf7bf23655a8af576cb117c6
7
+ data.tar.gz: ec9faff67503c5ad0883729d53c6b8d61e3f3ed3d5e7dc41a949eaa9041e8069f2f297396fd8e5c84946fdf3f45668f71494a1d85d308bb363689b3f621c56a8
data/README.md CHANGED
@@ -18,7 +18,7 @@ And then execute:
18
18
  ## Introduction
19
19
 
20
20
  ### What is JSONError gem?
21
- It provides parent class with simple DSL to define API errors. It is based on concept, that error is something that went wrong (_what? = key_), in some circumstances (_when? = context_).
21
+ It provides parent class with simple DSL to define API errors. It is based on concept that error is something that went wrong (_what? = key_) under certain circumstances (_when? = context_).
22
22
 
23
23
  ### Why would I use it?
24
24
  To design, manage and maintain API's errors JSON responses with ease.
@@ -31,13 +31,11 @@ Using locale file (e.g. json_errors.en.yml).
31
31
  en:
32
32
  json_errors:
33
33
  resource_not_found:
34
- status: 404
35
34
  message: 'Resource %{name} (%{id}) was not found.'
36
35
  description: 'Blah blah blah.'
37
36
  href: 'https://developers.api.pl/doc/errors/resource_not_found'
38
37
  batmans:
39
38
  resource_not_found:
40
- status: 404
41
39
  message: 'Resource Batman (%{id}) was not found.'
42
40
  description: "Cuz I'm Batman"
43
41
  ```
@@ -51,6 +49,8 @@ Let's take a look at example error class definition.
51
49
  ```ruby
52
50
  class Error < JSONError.base
53
51
  key :error
52
+ status 500
53
+
54
54
  translated_properties :message, :href
55
55
  custom_properties :details, :some_dependency
56
56
  visible_properties :key, :status, :message, :href, :details
@@ -63,6 +63,9 @@ Error.new(key: :error)
63
63
  ```
64
64
  however, json errors must have a key. Key is not inherited.
65
65
 
66
+ *Status* is the error http status. If not specified will default to 500. It may be expressed
67
+ by integer or string.
68
+
66
69
  *Default properties* are properties included to JSON output by default (:key, status: 500).
67
70
  - You can alias key property like in example below:
68
71
  ```ruby
@@ -87,11 +90,14 @@ Let's introduce two example error classes now. They will be used in further exam
87
90
  module Errors
88
91
  class ResourceNotFound < JSONError.base
89
92
  key :resource_not_found
93
+ status 404
90
94
  translated_properties :message, :href, :description
91
95
  end
92
96
 
93
97
  class OtherError < JSONError.base
94
98
  key :other_error
99
+ status 500
100
+
95
101
  translated_properties :message
96
102
  end
97
103
  end
@@ -106,7 +112,6 @@ Just add entries to locale.
106
112
  en:
107
113
  json_errors:
108
114
  resource_not_found:
109
- status: 404
110
115
  message: 'Resource %{name} (%{id}) was not found.'
111
116
  description: 'Blah blah blah.'
112
117
  href: 'https://developers.api.pl/doc/errors/resource_not_found'
@@ -183,13 +188,11 @@ You can specify different contexts for the same error key.
183
188
  en:
184
189
  json_errors:
185
190
  resource_not_found:
186
- status: 404
187
191
  message: 'Resource %{name} (%{id}) was not found.'
188
192
  description: 'Blah blah blah.'
189
193
  href: 'https://developers.api.pl/doc/errors/resource_not_found'
190
194
  batmans:
191
195
  resource_not_found:
192
- status: 404
193
196
  message: 'Resource Batman (%{id}) was not found.'
194
197
  description: "Cuz I'm Batman"
195
198
  ```
data/json_error.gemspec CHANGED
@@ -23,5 +23,5 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency 'bundler', '~> 1.10'
24
24
  s.add_development_dependency 'rake', '~> 10.0'
25
25
  s.add_development_dependency 'rspec'
26
- s.add_development_dependency 'codeclimate-test-reporter'
26
+ s.add_development_dependency 'simplecov'
27
27
  end
@@ -30,6 +30,13 @@ module JSONError
30
30
  @key ||= key
31
31
  end
32
32
 
33
+ def status(status = nil)
34
+ return @status if status.nil?
35
+ disallowed_property_setting!(:status) if base?
36
+
37
+ @status = status
38
+ end
39
+
33
40
  def properties
34
41
  [*default_properties, *translated_properties, *custom_properties]
35
42
  end
@@ -73,7 +80,7 @@ module JSONError
73
80
  end
74
81
 
75
82
  def status
76
- translate(:status, default: DEFAULT_STATUS)
83
+ self.class.status || 500
77
84
  end
78
85
 
79
86
  def to_json(*args)
@@ -1,3 +1,3 @@
1
1
  module JSONError
2
- VERSION = '1.0.1'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -1,11 +1,15 @@
1
1
  class ResourceNotFound < JSONError.base
2
2
  key :resource_not_found
3
+ status 404
4
+
3
5
  translated_properties :message, :description, :href
4
6
  custom_properties :details
5
7
  end
6
8
 
7
9
  class MissingTranslations < JSONError.base
8
10
  key :missing_translations
11
+ status 404
12
+
9
13
  translated_properties :message, :description, :href
10
14
  custom_properties :details
11
15
  visible_properties(*default_properties, :message, :details)
@@ -19,8 +23,10 @@ end
19
23
 
20
24
  class WithKeyAlias < JSONError.base
21
25
  key :with_key_alias, as: :error_code
26
+ status 400
22
27
  end
23
28
 
24
29
  class WithKeyAliasChild < WithKeyAlias
25
30
  key :with_key_alias_child
31
+ status '429'
26
32
  end
@@ -1,13 +1,11 @@
1
1
  en:
2
2
  json_errors:
3
3
  resource_not_found:
4
- status: 404
5
4
  message: 'Resource %{name} (%{id}) was not found.'
6
5
  description: 'Blah blah blah.'
7
6
  href: 'https://developers.api.pl/doc/errors/resource_not_found'
8
7
  other:
9
8
  resource_not_found:
10
- status: 400
11
9
  message: 'Resource not found.'
12
10
  description: 'Bleh bleh bleh.'
13
11
  href: 'https://developers.api.pl/doc/errors/resource_not_found?context=other'
@@ -85,7 +85,7 @@ describe ResourceNotFound do
85
85
 
86
86
  context 'for context "other"' do
87
87
  subject { described_class.new(context: 'other').status }
88
- it { is_expected.to eq(400) }
88
+ it { is_expected.to eq(404) }
89
89
  end
90
90
  end
91
91
 
@@ -177,7 +177,7 @@ describe MissingTranslations do
177
177
 
178
178
  describe '#status' do
179
179
  subject { described_class.new.status }
180
- it { is_expected.to eq(500) }
180
+ it { is_expected.to eq(404) }
181
181
  end
182
182
 
183
183
  describe '#key' do
@@ -292,7 +292,7 @@ describe WithKeyAlias do
292
292
  end
293
293
 
294
294
  describe 'status property' do
295
- it { expect(parse.call(subject)['status']).to eq(500) }
295
+ it { expect(parse.call(subject)['status']).to eq(400) }
296
296
  end
297
297
 
298
298
  describe 'error_code property' do
@@ -320,7 +320,7 @@ describe WithKeyAliasChild do
320
320
  end
321
321
 
322
322
  describe 'status property' do
323
- it { expect(parse.call(subject)['status']).to eq(500) }
323
+ it { expect(parse.call(subject)['status']).to eq('429') }
324
324
  end
325
325
 
326
326
  describe 'error_code property' do
@@ -102,17 +102,10 @@ describe JSONError::Base do
102
102
  end
103
103
 
104
104
  describe '#status' do
105
+ let(:error) { described_class.new(key: :other_error) }
105
106
  subject { error.status }
106
107
 
107
- context 'when not defined in locale' do
108
- let(:error) { described_class.new(key: :other_error) }
109
- it { is_expected.to eq(500) }
110
- end
111
-
112
- context 'when defined in locale' do
113
- let(:error) { described_class.new(key: :resource_not_found) }
114
- it { is_expected.to eq(404) }
115
- end
108
+ it { is_expected.to eq(500) }
116
109
  end
117
110
 
118
111
  describe '#key' do
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
1
+ require 'simplecov'
2
+ SimpleCov.start
3
3
 
4
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Buszewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2018-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: codeclimate-test-reporter
84
+ name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  requirements: []
145
145
  rubyforge_project:
146
- rubygems_version: 2.4.8
146
+ rubygems_version: 2.6.8
147
147
  signing_key:
148
148
  specification_version: 4
149
149
  summary: JSON errors base for APIs.
@@ -155,4 +155,3 @@ test_files:
155
155
  - spec/json_error/errors/disallowed_property_setting.rb
156
156
  - spec/json_error_spec.rb
157
157
  - spec/spec_helper.rb
158
- has_rdoc: