lhs 14.3.4 → 14.4.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: 23340184de8a2e5596232bb3aa09208c86a11687
4
- data.tar.gz: dbbc8b0fb443b46b077f0743698252c5b9cbcd6f
3
+ metadata.gz: 915b54df82b092a12fef1395a21e854e4f95738d
4
+ data.tar.gz: ff5e0fbca423676b763606ebcdf2d2b2a4562084
5
5
  SHA512:
6
- metadata.gz: dd22d59947be1c7d9187ddca6a0b46f9ca3d7942418b4aadbf3c1d7ec376a1d6fd7d192ba825d48896ed7715b909ff965a7346b0cad4812f015b3ad3d834e44d
7
- data.tar.gz: 04113fb6808937fe56f9859291cb3ba0c308542a408090da03035098b09c59d1f6cff5ad9a3ab95401b922c65f6bb41e9b04ad2bbe0ef5e1313b7a9a8a738eec
6
+ metadata.gz: 5fbbe1749a1266a6b4fc2df4d4aaf9b55b4d24a69d3af3af000c6c9d4b159eea16d7435e93799fcbfc28f0db46f4e86a84283938578edb62b05b4bdf47f1ca2f
7
+ data.tar.gz: 4f2652ef35ec40aa670901203fcbbf02de58fffe900090f90af7acb9fcdc032b40dbe86a350a2cc8c83efb7e25cb7bbb21aa34e74a7586956c6d313c88e2df80
@@ -32,3 +32,6 @@ Metrics/BlockLength:
32
32
 
33
33
  Style/EmptyLinesAroundBlockBody:
34
34
  Enabled: false
35
+
36
+ RSpec/MultipleExpectations:
37
+ Enabled: false
data/README.md CHANGED
@@ -775,7 +775,8 @@ end
775
775
  user.errors #<LHS::Problems::Errors>
776
776
  user.errors.include?(:email) # true
777
777
  user.errors[:email] # ['REQUIRED_PROPERTY_VALUE']
778
- user.errors.messages # {:email=>["REQUIRED_PROPERTY_VALUE"]}
778
+ user.errors.messages # {:email=>["Translated error message that this value is required"]}
779
+ user.errors.codes # {:email=>["REQUIRED_PROPERTY_VALUE"]}
779
780
  user.errors.message # email must be set when user is created."
780
781
  ```
781
782
 
@@ -885,7 +886,8 @@ LHS makes those errors available when accessing `.errors`:
885
886
  )
886
887
 
887
888
  presence.errors.any? # true
888
- presence.place.errors.messages[:opening_hours] # ['REQUIRED_PROPERTY_VALUE']
889
+ presence.place.errors.messages[:opening_hours] # ['This field needs to be present']
890
+ presence.place.errors.codes[:opening_hours] # ['REQUIRED_PROPERTY_VALUE']
889
891
  ```
890
892
 
891
893
  ### Non blocking validation errors, so called warnings
@@ -6,7 +6,7 @@ module LHS::Problems
6
6
  class Base
7
7
  include Enumerable
8
8
 
9
- attr_reader :raw, :messages, :record
9
+ attr_reader :raw, :messages, :codes, :record
10
10
 
11
11
  def include?(attribute)
12
12
  messages[attribute].present?
@@ -55,6 +55,7 @@ module LHS::Problems
55
55
  def clear
56
56
  @raw = nil
57
57
  @messages.clear
58
+ @codes.clear
58
59
  end
59
60
 
60
61
  delegate :values, to: :messages
@@ -73,6 +74,8 @@ module LHS::Problems
73
74
 
74
75
  def add_error(messages, key, value)
75
76
  key = key.to_sym
77
+ codes[key] ||= []
78
+ codes[key].push(value)
76
79
  messages[key] ||= []
77
80
  messages[key].push(generate_message(key, value))
78
81
  end
@@ -6,11 +6,13 @@ module LHS::Problems
6
6
  def initialize(response = nil, record = nil)
7
7
  @raw = response.body if response
8
8
  @record = record
9
+ @codes = {}.with_indifferent_access
9
10
  @messages = messages_from_response(response).with_indifferent_access
10
11
  @message = message_from_response(response)
11
12
  @status_code = response.code if response
12
13
  rescue JSON::ParserError
13
14
  @messages = (messages || {}).with_indifferent_access
15
+ @codes = (codes || {}).with_indifferent_access
14
16
  @message = 'parse error'
15
17
  add_error(@messages, 'body', 'parse error')
16
18
  end
@@ -4,6 +4,7 @@ module LHS::Problems
4
4
  def initialize(raw, record = nil)
5
5
  @raw = raw
6
6
  @record = record
7
+ @codes = {}.with_indifferent_access
7
8
  @messages = warnings_from_raw
8
9
  end
9
10
 
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = '14.3.4'
2
+ VERSION = '14.4.0'
3
3
  end
@@ -0,0 +1,53 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Item do
4
+ context 'error codes' do
5
+ before(:each) do
6
+ I18n.reload!
7
+ I18n.backend.store_translations(:en, YAML.safe_load(translation)) if translation.present?
8
+ class Record < LHS::Record
9
+ endpoint 'http://datastore/records'
10
+ end
11
+ end
12
+
13
+ let(:translation) do
14
+ %q{
15
+ lhs:
16
+ errors:
17
+ fallback_message: 'This value is wrong'
18
+ }
19
+ end
20
+
21
+ it 'provides error codes along side with translated messages' do
22
+ stub_request(:post, 'http://datastore/records')
23
+ .to_return(body: {
24
+ field_errors: [{
25
+ code: 'UNSUPPORTED_PROPERTY_VALUE',
26
+ path: ['gender'],
27
+ message: 'The property value is unsupported.'
28
+ }, {
29
+ code: 'INCOMPLETE_PROPERTY_VALUE',
30
+ path: ['gender'],
31
+ message: 'The property value is incomplete. It misses some data'
32
+ }, {
33
+ code: 'INCOMPLETE_PROPERTY_VALUE',
34
+ path: ['contract', 'entry_id'],
35
+ message: 'The property value is incomplete. It misses some data'
36
+ }]
37
+ }.to_json)
38
+ record = Record.create
39
+ expect(record.errors.messages['gender']).to eq(
40
+ ['This value is wrong', 'This value is wrong']
41
+ )
42
+ expect(record.errors.codes['gender']).to eq(
43
+ ['UNSUPPORTED_PROPERTY_VALUE', 'INCOMPLETE_PROPERTY_VALUE']
44
+ )
45
+ expect(record.errors.messages['contract.entry_id']).to eq(
46
+ ['This value is wrong']
47
+ )
48
+ expect(record.errors.codes['contract.entry_id']).to eq(
49
+ ['INCOMPLETE_PROPERTY_VALUE']
50
+ )
51
+ end
52
+ end
53
+ end
@@ -51,6 +51,8 @@ describe LHS::Item do
51
51
  end
52
52
 
53
53
  before(:each) do
54
+ I18n.reload!
55
+ I18n.backend.store_translations(:en, {}) if defined? translations
54
56
  LHC.config.placeholder(:datastore, datastore)
55
57
  class Record < LHS::Record
56
58
  endpoint ':datastore/:campaign_id/feedbacks'
@@ -0,0 +1,53 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Item do
4
+ context 'error codes' do
5
+ before(:each) do
6
+ I18n.reload!
7
+ I18n.backend.store_translations(:en, YAML.safe_load(translation)) if translation.present?
8
+ class Record < LHS::Record
9
+ endpoint 'http://datastore/records'
10
+ end
11
+ end
12
+
13
+ let(:translation) do
14
+ %q{
15
+ lhs:
16
+ warnings:
17
+ fallback_message: 'This value is problematic'
18
+ }
19
+ end
20
+
21
+ it 'provides error codes along side with translated messages' do
22
+ stub_request(:post, 'http://datastore/records')
23
+ .to_return(body: {
24
+ field_warnings: [{
25
+ code: 'UNSUPPORTED_PROPERTY_VALUE',
26
+ path: ['gender'],
27
+ message: 'The property value is unsupported.'
28
+ }, {
29
+ code: 'INCOMPLETE_PROPERTY_VALUE',
30
+ path: ['gender'],
31
+ message: 'The property value is incomplete. It misses some data'
32
+ }, {
33
+ code: 'INCOMPLETE_PROPERTY_VALUE',
34
+ path: ['contract', 'entry_id'],
35
+ message: 'The property value is incomplete. It misses some data'
36
+ }]
37
+ }.to_json)
38
+ record = Record.create
39
+ expect(record.warnings.messages['gender']).to eq(
40
+ ['This value is problematic', 'This value is problematic']
41
+ )
42
+ expect(record.warnings.codes['gender']).to eq(
43
+ ['UNSUPPORTED_PROPERTY_VALUE', 'INCOMPLETE_PROPERTY_VALUE']
44
+ )
45
+ expect(record.warnings.messages['contract.entry_id']).to eq(
46
+ ['This value is problematic']
47
+ )
48
+ expect(record.warnings.codes['contract.entry_id']).to eq(
49
+ ['INCOMPLETE_PROPERTY_VALUE']
50
+ )
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 14.3.4
4
+ version: 14.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-09 00:00:00.000000000 Z
11
+ date: 2017-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -328,6 +328,7 @@ files:
328
328
  - spec/item/delegate_spec.rb
329
329
  - spec/item/destroy_spec.rb
330
330
  - spec/item/dig_spec.rb
331
+ - spec/item/error_codes_spec.rb
331
332
  - spec/item/errors_spec.rb
332
333
  - spec/item/getter_spec.rb
333
334
  - spec/item/internal_data_structure_spec.rb
@@ -340,6 +341,7 @@ files:
340
341
  - spec/item/translate_errors_spec.rb
341
342
  - spec/item/update_spec.rb
342
343
  - spec/item/validation_spec.rb
344
+ - spec/item/warning_codes_spec.rb
343
345
  - spec/item/warnings_spec.rb
344
346
  - spec/pagination/pages_left_spec.rb
345
347
  - spec/proxy/create_sub_resource_spec.rb
@@ -508,6 +510,7 @@ test_files:
508
510
  - spec/item/delegate_spec.rb
509
511
  - spec/item/destroy_spec.rb
510
512
  - spec/item/dig_spec.rb
513
+ - spec/item/error_codes_spec.rb
511
514
  - spec/item/errors_spec.rb
512
515
  - spec/item/getter_spec.rb
513
516
  - spec/item/internal_data_structure_spec.rb
@@ -520,6 +523,7 @@ test_files:
520
523
  - spec/item/translate_errors_spec.rb
521
524
  - spec/item/update_spec.rb
522
525
  - spec/item/validation_spec.rb
526
+ - spec/item/warning_codes_spec.rb
523
527
  - spec/item/warnings_spec.rb
524
528
  - spec/pagination/pages_left_spec.rb
525
529
  - spec/proxy/create_sub_resource_spec.rb