api-client 1.5.2 → 1.5.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.
- data/CHANGELOG.md +8 -1
- data/lib/api-client/base.rb +2 -1
- data/lib/api-client/errors.rb +24 -5
- data/lib/api-client/version.rb +1 -1
- data/spec/api-client/base_spec.rb +3 -3
- data/spec/api-client/errors_spec.rb +11 -0
- metadata +3 -3
data/CHANGELOG.md
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## v1.5.3
|
4
|
+
|
5
|
+
* Improving #unique_messages method to work with #to_sentence from ActiveSupport.
|
6
|
+
|
7
|
+
## v1.5.2
|
8
|
+
|
9
|
+
* Fixed return from #add_errors.
|
10
|
+
|
3
11
|
## v1.5.1
|
4
12
|
|
5
13
|
* Fixed Error Setter for erros from an external API.
|
6
14
|
|
7
|
-
|
8
15
|
## v1.5.0
|
9
16
|
|
10
17
|
* Handler of arrays as response.
|
data/lib/api-client/base.rb
CHANGED
@@ -54,7 +54,8 @@ module ApiClient
|
|
54
54
|
#
|
55
55
|
# @param [Hash] errors of the object..
|
56
56
|
def errors=(errors = {})
|
57
|
-
@errors = Errors.new(self)
|
57
|
+
@errors = Errors.new(self)
|
58
|
+
@errors.add_errors(Hash[errors.map{|(key,value)| [key.to_sym,value]}])
|
58
59
|
end
|
59
60
|
|
60
61
|
protected
|
data/lib/api-client/errors.rb
CHANGED
@@ -11,15 +11,34 @@ class ApiClient::Errors < ActiveModel::Errors
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
14
|
+
# Returns a unique message for each array of error messages in a hash.
|
15
15
|
#
|
16
|
-
#
|
17
|
-
#
|
16
|
+
# class Person
|
17
|
+
# validates_presence_of :name, :address, :email
|
18
|
+
# validates_length_of :name, in: 5..30
|
19
|
+
# end
|
18
20
|
#
|
19
|
-
#
|
21
|
+
# person = Person.create(address: '123 First St.')
|
22
|
+
# person.errors.unique_messages
|
23
|
+
# # => { :name => "is too short (minimum is 5 characters) and can't be blank", :address => nil, :email => "can't be blank" }
|
20
24
|
def unique_messages
|
21
25
|
errors = {}
|
22
|
-
|
26
|
+
map { |attribute, messages| errors[attribute] = unique_message(attribute) }
|
23
27
|
errors
|
24
28
|
end
|
29
|
+
|
30
|
+
# Returns a unique message for a given attribute.
|
31
|
+
#
|
32
|
+
# class Person
|
33
|
+
# validates_presence_of :name, :address, :email
|
34
|
+
# validates_length_of :name, in: 5..30
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# person = Person.create(address: '123 First St.')
|
38
|
+
# person.errors.unique_message(:name) # => "is too short (minimum is 5 characters) and can't be blank"
|
39
|
+
# person.errors.unique_message(:address) # => nil
|
40
|
+
def unique_message(attribute)
|
41
|
+
return '' if messages[attribute].blank?
|
42
|
+
messages[attribute].to_sentence
|
43
|
+
end
|
25
44
|
end
|
data/lib/api-client/version.rb
CHANGED
@@ -50,7 +50,7 @@ describe ApiClient::Base do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should return the errors" do
|
53
|
-
@user.errors.should == {:a => :bc}
|
53
|
+
@user.errors.messages.should == {:a => :bc}
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -71,7 +71,7 @@ describe ApiClient::Base do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should set @errors with symbolic keys" do
|
74
|
-
@user.errors.should == { :a => "message", :b => "message" }
|
74
|
+
@user.errors.messages.should == { :a => "message", :b => "message" }
|
75
75
|
end
|
76
76
|
end
|
77
|
-
end
|
77
|
+
end
|
@@ -21,4 +21,15 @@ describe ApiClient::Errors do
|
|
21
21
|
@user.errors.unique_messages.should == { :a => "can't be blank and is not included in the list" }
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
describe "#unique_message" do
|
26
|
+
before :each do
|
27
|
+
@user = User.new
|
28
|
+
@user.valid?
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return a unique message for the given attribute" do
|
32
|
+
@user.errors.unique_message(:a).should == "can't be blank and is not included in the list"
|
33
|
+
end
|
34
|
+
end
|
24
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
segments:
|
162
162
|
- 0
|
163
|
-
hash:
|
163
|
+
hash: 3311950444943388535
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
169
|
version: '0'
|
170
170
|
segments:
|
171
171
|
- 0
|
172
|
-
hash:
|
172
|
+
hash: 3311950444943388535
|
173
173
|
requirements: []
|
174
174
|
rubyforge_project:
|
175
175
|
rubygems_version: 1.8.24
|