api-client 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/examples/controllers/application_controller.rb +18 -0
- data/examples/controllers/user_controller.rb +8 -0
- data/examples/models/user.rb +4 -0
- data/lib/api-client.rb +1 -0
- data/lib/api-client/base.rb +12 -2
- data/lib/api-client/errors.rb +17 -0
- data/lib/api-client/version.rb +2 -2
- data/spec/api-client/base_spec.rb +22 -0
- data/spec/api-client/errors_spec.rb +14 -0
- data/spec/spec_helper.rb +3 -0
- metadata +7 -8
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
protect_from_forgery
|
3
|
+
respond_to :html
|
4
|
+
|
5
|
+
# NotFound errors will be redirected to the action not_found while any other will be redirected to the action generic.
|
6
|
+
rescue_from ApiClient::Exceptions::NotFound, :with => :not_found
|
7
|
+
rescue_from ApiClient::Exceptions::Generic, :with => :generic
|
8
|
+
|
9
|
+
# This code is only for example purposes.
|
10
|
+
# Any behavior can be executed here. Just write your own.
|
11
|
+
def not_found
|
12
|
+
ApplicationMailer.send_not_found_notice.deliver
|
13
|
+
end
|
14
|
+
|
15
|
+
def generic
|
16
|
+
ApplicationMailer.send_generic_error.deliver
|
17
|
+
end
|
18
|
+
end
|
data/lib/api-client.rb
CHANGED
@@ -3,6 +3,7 @@ require "api-client/version"
|
|
3
3
|
# High Level Namespace of the library ApiClient.
|
4
4
|
module ApiClient
|
5
5
|
autoload :Exceptions, 'api-client/exceptions'
|
6
|
+
autoload :Errors, 'api-client/errors'
|
6
7
|
autoload :Base, 'api-client/base'
|
7
8
|
autoload :Dispatcher, 'api-client/dispatcher'
|
8
9
|
autoload :Parser, 'api-client/parser'
|
data/lib/api-client/base.rb
CHANGED
@@ -14,7 +14,10 @@ class ApiClient::Base
|
|
14
14
|
extend ApiClient::Parser
|
15
15
|
extend ApiClient::Dispatcher
|
16
16
|
|
17
|
-
#
|
17
|
+
# @params [Hash] Hash of errors.
|
18
|
+
attr_writer :errors
|
19
|
+
|
20
|
+
# Initialize an object based on a hash of attributes.
|
18
21
|
#
|
19
22
|
# @param [Hash] attributes object attributes.
|
20
23
|
# @return [Base] the object initialized.
|
@@ -26,11 +29,18 @@ class ApiClient::Base
|
|
26
29
|
|
27
30
|
# Return if a object is persisted on the database or not.
|
28
31
|
#
|
29
|
-
# @return [False]
|
32
|
+
# @return [False] always return false.
|
30
33
|
def persisted?
|
31
34
|
false
|
32
35
|
end
|
33
36
|
|
37
|
+
# Return the an array of errors if existent, otherwise instantiate a new ApiClient::Errors object with self.
|
38
|
+
#
|
39
|
+
# @return [ApiClient::Errors] the validation errors.
|
40
|
+
def errors
|
41
|
+
@errors ||= ApiClient::Errors.new(self)
|
42
|
+
end
|
43
|
+
|
34
44
|
# Make a get request and returns a new instance with te response attributes.
|
35
45
|
#
|
36
46
|
# @param [String] url of the api request.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "active_model"
|
2
|
+
|
3
|
+
# ApiClient::Errors provide extra functionality to ActiveModel::Errors.
|
4
|
+
class ApiClient::Errors < ActiveModel::Errors
|
5
|
+
# Create a hash of attributes with unique validation error messages.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# user.errors.unique_messages #=> { :name => [ can't be empty and is invalid ]}
|
9
|
+
#
|
10
|
+
# @return [Hash] The hash of attributes with a unique error message.
|
11
|
+
#
|
12
|
+
def unique_messages
|
13
|
+
errors = {}
|
14
|
+
to_hash.each do |attribute, messages| errors[attribute] = messages.join(" and ") end
|
15
|
+
errors
|
16
|
+
end
|
17
|
+
end
|
data/lib/api-client/version.rb
CHANGED
@@ -22,4 +22,26 @@ describe ApiClient::Base do
|
|
22
22
|
User.new.should_not be_persisted
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
describe "#errors" do
|
27
|
+
context "when @errors is not nil" do
|
28
|
+
before :each do
|
29
|
+
@user = User.new(:errors => {:a => :bc})
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return the errors" do
|
33
|
+
@user.errors.should == {:a => :bc}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when @errors is nil" do
|
38
|
+
before :each do
|
39
|
+
@user = User.new
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should instantiate a new instance of ApiClient::Errors" do
|
43
|
+
@user.errors.should be_an_instance_of(ApiClient::Errors)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
25
47
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiClient::Errors do
|
4
|
+
describe "#unique_messages" do
|
5
|
+
before :each do
|
6
|
+
@user = User.new
|
7
|
+
@user.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should set #a" do
|
11
|
+
@user.errors.unique_messages.should == { :a => "can't be blank and is not included in the list" }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 1.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -123,9 +123,13 @@ files:
|
|
123
123
|
- README.md
|
124
124
|
- Rakefile
|
125
125
|
- api-client.gemspec
|
126
|
+
- examples/controllers/application_controller.rb
|
127
|
+
- examples/controllers/user_controller.rb
|
128
|
+
- examples/models/user.rb
|
126
129
|
- lib/api-client.rb
|
127
130
|
- lib/api-client/base.rb
|
128
131
|
- lib/api-client/dispatcher.rb
|
132
|
+
- lib/api-client/errors.rb
|
129
133
|
- lib/api-client/exceptions.rb
|
130
134
|
- lib/api-client/exceptions/bad_gateway.rb
|
131
135
|
- lib/api-client/exceptions/connection_refused.rb
|
@@ -144,6 +148,7 @@ files:
|
|
144
148
|
- spec/api-client/base/put_spec.rb
|
145
149
|
- spec/api-client/base_spec.rb
|
146
150
|
- spec/api-client/dispatcher_spec.rb
|
151
|
+
- spec/api-client/errors_spec.rb
|
147
152
|
- spec/api-client/parser_spec.rb
|
148
153
|
- spec/spec_helper.rb
|
149
154
|
homepage: ''
|
@@ -158,18 +163,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
163
|
- - ! '>='
|
159
164
|
- !ruby/object:Gem::Version
|
160
165
|
version: '0'
|
161
|
-
segments:
|
162
|
-
- 0
|
163
|
-
hash: -2404782696775397333
|
164
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
167
|
none: false
|
166
168
|
requirements:
|
167
169
|
- - ! '>='
|
168
170
|
- !ruby/object:Gem::Version
|
169
171
|
version: '0'
|
170
|
-
segments:
|
171
|
-
- 0
|
172
|
-
hash: -2404782696775397333
|
173
172
|
requirements: []
|
174
173
|
rubyforge_project:
|
175
174
|
rubygems_version: 1.8.24
|