json_error 1.0.0 → 1.0.1

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: a4322948d35b22110cc1c146b0da8bd3d0f16f94
4
- data.tar.gz: 55b445035a7813c5e4d73a12e1c270696aac45b9
3
+ metadata.gz: d8d02b5a8dc09b18176bde4e04b523d218f255cf
4
+ data.tar.gz: 7bf5c086a9d80d91ceff09d6d430dbafabbe6768
5
5
  SHA512:
6
- metadata.gz: e8dcdc4ca99ac2245247d35cbecfd2dc1e5e2ced09fd8fcfa303a2a699bd33868dc03fff3cd99daaef8bb734eb24fe19e2dc8c78e092046d54a3a3251241b4d7
7
- data.tar.gz: fc4668dd5daca19f4a389d0b455617b3ae7958bf5461f5d7c2a5f01e207df5c04a951495a1e3adaf0117b250bfb5638af8ffd289d1d300c30f1025ecdd7c46c2
6
+ metadata.gz: 462ecb79ea50676654177e3aef1474faf926393696cce610929ffe9ca27c3f202c700a5304733115598da6895249b75d19345640ed72540e28a7051073c6ac6c
7
+ data.tar.gz: 14cabc02842c091ec6cb8af643ec2677293a14cd42dc55d8fa3d067b2d87427b18765f873b60ca747cafd0552c1a4d601fd09b9388f1ea1fdc2ffe68548f6f1a
data/README.md CHANGED
@@ -63,7 +63,15 @@ Error.new(key: :error)
63
63
  ```
64
64
  however, json errors must have a key. Key is not inherited.
65
65
 
66
- *Default properties* are properties included to JSON output by default (:key, status: 500). You can change status in locale.
66
+ *Default properties* are properties included to JSON output by default (:key, status: 500).
67
+ - You can alias key property like in example below:
68
+ ```ruby
69
+ class SomeError < JSONError.base
70
+ # There will be "error_code" within .to_json output instead of "key"
71
+ key :error, as: :error_code
72
+ end
73
+ ```
74
+ - You can change status in locale.
67
75
 
68
76
  *Translated properties* are error attributes which values will be loaded from locale. Translated properties can be inherited.
69
77
 
@@ -23,4 +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
27
  end
@@ -6,9 +6,12 @@ module JSONError
6
6
  attr_reader :context, :key, :options
7
7
 
8
8
  class << self
9
+ attr_reader :key_alias
10
+
9
11
  def inherited(base)
10
12
  base.instance_variable_set(:@translated_properties, translated_properties)
11
13
  base.instance_variable_set(:@custom_properties, custom_properties)
14
+ base.instance_variable_set(:@key_alias, key_alias)
12
15
  end
13
16
 
14
17
  def base?
@@ -19,10 +22,11 @@ module JSONError
19
22
  raise(Errors::DisallowedPropertySetting, property: property, class: self)
20
23
  end
21
24
 
22
- def key(key = nil)
25
+ def key(key = nil, options = {})
23
26
  return @key if key.nil?
24
27
  disallowed_property_setting!(:key) if base?
25
28
 
29
+ @key_alias = options[:as] if options[:as].is_a?(Symbol)
26
30
  @key ||= key
27
31
  end
28
32
 
@@ -102,7 +106,9 @@ module JSONError
102
106
  end
103
107
 
104
108
  def visible_properties_hash
105
- Hash[visible_properties.map { |property| [property, send(property)] }]
109
+ h = Hash[visible_properties.map { |property| [property, send(property)] }]
110
+ h[key_alias] = h.delete(:key) unless key_alias.nil?
111
+ h
106
112
  end
107
113
 
108
114
  def visible_properties
@@ -112,5 +118,9 @@ module JSONError
112
118
  def custom_properties
113
119
  self.class.custom_properties || []
114
120
  end
121
+
122
+ def key_alias
123
+ self.class.key_alias
124
+ end
115
125
  end
116
126
  end
@@ -1,3 +1,3 @@
1
1
  module JSONError
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -16,3 +16,11 @@ class ChildError < MissingTranslations
16
16
  translated_properties :additional_translated
17
17
  custom_properties :additional_custom
18
18
  end
19
+
20
+ class WithKeyAlias < JSONError.base
21
+ key :with_key_alias, as: :error_code
22
+ end
23
+
24
+ class WithKeyAliasChild < WithKeyAlias
25
+ key :with_key_alias_child
26
+ end
@@ -272,3 +272,59 @@ describe ChildError do
272
272
  end
273
273
  end
274
274
  end
275
+
276
+ describe WithKeyAlias do
277
+ let(:error_object) { described_class.new }
278
+ let(:error_key) { :with_key_alias }
279
+
280
+ describe '#key' do
281
+ subject { error_object.key }
282
+ it { is_expected.to eq(error_key) }
283
+ end
284
+
285
+ describe '#to_json' do
286
+ let(:parse) { -> (s) { JSON.parse(s) } }
287
+ subject { error_object.to_json }
288
+
289
+ describe '#keys' do
290
+ let(:keys) { %w(status error_code) }
291
+ it { expect(parse.call(subject).keys).to eq(keys) }
292
+ end
293
+
294
+ describe 'status property' do
295
+ it { expect(parse.call(subject)['status']).to eq(500) }
296
+ end
297
+
298
+ describe 'error_code property' do
299
+ it { expect(parse.call(subject)['error_code']).to eq(error_key.to_s) }
300
+ end
301
+ end
302
+ end
303
+
304
+ describe WithKeyAliasChild do
305
+ let(:error_object) { described_class.new }
306
+ let(:error_key) { :with_key_alias_child }
307
+
308
+ describe '#key' do
309
+ subject { error_object.key }
310
+ it { is_expected.to eq(error_key) }
311
+ end
312
+
313
+ describe '#to_json' do
314
+ let(:parse) { -> (s) { JSON.parse(s) } }
315
+ subject { error_object.to_json }
316
+
317
+ describe '#keys' do
318
+ let(:keys) { %w(status error_code) }
319
+ it { expect(parse.call(subject).keys).to eq(keys) }
320
+ end
321
+
322
+ describe 'status property' do
323
+ it { expect(parse.call(subject)['status']).to eq(500) }
324
+ end
325
+
326
+ describe 'error_code property' do
327
+ it { expect(parse.call(subject)['error_code']).to eq(error_key.to_s) }
328
+ end
329
+ end
330
+ end
@@ -1,3 +1,6 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
 
3
6
  require 'i18n'
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.0
4
+ version: 1.0.1
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-10-25 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - krzysztof.buszewicz@gmail.com
@@ -129,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
143
  version: '0'
130
144
  requirements: []
131
145
  rubyforge_project:
132
- rubygems_version: 2.4.5
146
+ rubygems_version: 2.4.8
133
147
  signing_key:
134
148
  specification_version: 4
135
149
  summary: JSON errors base for APIs.