api-client 1.8.1 → 1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/CHANGELOG.md +4 -0
- data/README.md +22 -1
- data/examples/controllers/application_controller.rb +10 -3
- data/lib/api-client/base.rb +5 -9
- data/lib/api-client/dispatcher/net-http.rb +6 -7
- data/lib/api-client/parser.rb +4 -2
- data/lib/api-client/version.rb +1 -1
- data/spec/api-client/base_spec.rb +4 -22
- metadata +2 -8
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,30 @@
|
|
1
|
-
# ApiClient
|
1
|
+
# ApiClient
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/zertico/api-client.png?branch=master)](http://travis-ci.org/zertico/api-client) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/zertico/api-client)
|
4
4
|
|
5
|
+
ApiClient handle all the logic necessary to call Some API, catch the response and initialize an object with it for you.
|
6
|
+
It is possible to use Typhoeus or the native Ruby Library Net::Http.
|
7
|
+
It works very well with rails, so you can exchange your ActiveRecord based models without any concern and your application will make Api calls transparently.
|
5
8
|
It supports ruby 1.8.7, 1.9.2, 1.9.3, jruby and ree out of the box.
|
6
9
|
|
10
|
+
## Useful Links
|
11
|
+
|
12
|
+
### Mailing List
|
13
|
+
|
14
|
+
If you have any questions, comments, or concerns, please use the Google Group instead of the Github issue tracker:
|
15
|
+
|
16
|
+
[https://groups.google.com/forum/#!forum/zertico-api-client](https://groups.google.com/forum/#!forum/zertico-api-client)
|
17
|
+
|
18
|
+
### Documentation
|
19
|
+
|
20
|
+
You can find the code documentation for the last version generated by Yard on this link:
|
21
|
+
|
22
|
+
[http://rdoc.info/gems/api-client/frames](http://rdoc.info/gems/api-client/frames)
|
23
|
+
|
24
|
+
### Changelog
|
25
|
+
|
26
|
+
[https://github.com/zertico/api-client/blob/master/CHANGELOG.md](https://github.com/zertico/api-client/blob/master/CHANGELOG.md)
|
27
|
+
|
7
28
|
## Installation
|
8
29
|
|
9
30
|
Add this line to your application's Gemfile:
|
@@ -3,16 +3,23 @@ class ApplicationController < ActionController::Base
|
|
3
3
|
respond_to :html
|
4
4
|
|
5
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::Unauthorized, :with => :unauthorized_access
|
6
7
|
rescue_from ApiClient::Exceptions::NotFound, :with => :not_found
|
7
8
|
rescue_from ApiClient::Exceptions::Generic, :with => :generic
|
8
9
|
|
9
10
|
# This code is only for example purposes.
|
10
11
|
# Any behavior can be executed here. Just write your own.
|
12
|
+
def unauthorized_access
|
13
|
+
redirect_to "http://www.example.com/sign_in?redirect_uri=#{request.url}"
|
14
|
+
end
|
15
|
+
|
11
16
|
def not_found
|
12
17
|
ApplicationMailer.send_not_found_notice.deliver
|
18
|
+
render :file => "/public/404.html", :status => 404
|
13
19
|
end
|
14
20
|
|
15
|
-
def generic
|
16
|
-
ApplicationMailer.send_generic_error.deliver
|
21
|
+
def generic(exception)
|
22
|
+
ApplicationMailer.send_generic_error(exception).deliver
|
23
|
+
render :file => "/public/404.html", :status => 404
|
17
24
|
end
|
18
|
-
end
|
25
|
+
end
|
data/lib/api-client/base.rb
CHANGED
@@ -15,11 +15,15 @@ module ApiClient
|
|
15
15
|
# @return [Hash] the request response.
|
16
16
|
attr_accessor :response
|
17
17
|
|
18
|
+
# @return [Hash] the errors object.
|
19
|
+
attr_reader :errors
|
20
|
+
|
18
21
|
# Initialize an object based on a hash of attributes.
|
19
22
|
#
|
20
23
|
# @param [Hash] attributes object attributes.
|
21
24
|
# @return [Base] the object initialized.
|
22
25
|
def initialize(attributes = {})
|
26
|
+
@errors = Errors.new(self)
|
23
27
|
attributes.each do |name, value|
|
24
28
|
send("#{name.to_s}=", value)
|
25
29
|
end
|
@@ -94,19 +98,11 @@ module ApiClient
|
|
94
98
|
|
95
99
|
alias_method :to_hash, :attributes
|
96
100
|
|
97
|
-
# Return the hash of errors if existent, otherwise instantiate a new ApiClient::Errors object with self.
|
98
|
-
#
|
99
|
-
# @return [ApiClient::Errors] the validation errors.
|
100
|
-
def errors
|
101
|
-
@errors ||= Errors.new(self)
|
102
|
-
end
|
103
|
-
|
104
101
|
# Set the hash of errors, making keys symbolic.
|
105
102
|
#
|
106
103
|
# @param [Hash] errors of the object.
|
107
104
|
def errors=(errs = {})
|
108
|
-
|
109
|
-
@errors.add_errors(Hash[errs.map{|(key,value)| [key.to_sym,value]}])
|
105
|
+
errors.add_errors(Hash[errs.map{|(key,value)| [key.to_sym,value]}])
|
110
106
|
end
|
111
107
|
|
112
108
|
protected
|
@@ -10,7 +10,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
10
10
|
# @return [HTTP] the response object.
|
11
11
|
def self.get(url, header = {})
|
12
12
|
initialize_connection(url)
|
13
|
-
call { @http.get(@uri.
|
13
|
+
call { @http.get(@uri.request_uri, { 'Content-Type' => 'application/json' }.merge(header)) }
|
14
14
|
end
|
15
15
|
|
16
16
|
# Make a post request and returns it.
|
@@ -21,7 +21,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
21
21
|
# @return [HTTP] the response object.
|
22
22
|
def self.post(url, args, header = {})
|
23
23
|
initialize_connection(url)
|
24
|
-
call { @http.post(@uri.
|
24
|
+
call { @http.post(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
|
25
25
|
end
|
26
26
|
|
27
27
|
# Make a put request and returns it.
|
@@ -32,7 +32,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
32
32
|
# @return [HTTP] the response object.
|
33
33
|
def self.put(url, args, header = {})
|
34
34
|
initialize_connection(url)
|
35
|
-
call { @http.put(@uri.
|
35
|
+
call { @http.put(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
|
36
36
|
end
|
37
37
|
|
38
38
|
# Make a patch request and returns it.
|
@@ -43,7 +43,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
43
43
|
# @return [HTTP] the response object.
|
44
44
|
def self.patch(url, args, header = {})
|
45
45
|
initialize_connection(url)
|
46
|
-
call { @http.patch(@uri.
|
46
|
+
call { @http.patch(@uri.request_uri, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
|
47
47
|
end
|
48
48
|
|
49
49
|
# Make a delete request and returns it.
|
@@ -53,15 +53,14 @@ module ApiClient::Dispatcher::NetHttp
|
|
53
53
|
# @return [HTTP] the response object.
|
54
54
|
def self.delete(url, header = {})
|
55
55
|
initialize_connection(url)
|
56
|
-
call { @http.delete(@uri.
|
56
|
+
call { @http.delete(@uri.request_uri, header) }
|
57
57
|
end
|
58
58
|
|
59
59
|
protected
|
60
60
|
|
61
61
|
def self.initialize_connection(url = '')
|
62
62
|
@uri = URI(url)
|
63
|
-
@
|
64
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
63
|
+
@http = Net::HTTP.start(@uri.host, @uri.port)
|
65
64
|
end
|
66
65
|
|
67
66
|
def self.call
|
data/lib/api-client/parser.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
# ApiClient::Parser provides a method to parse the request response.
|
2
4
|
module ApiClient::Parser
|
3
5
|
# Parse the JSON response.
|
@@ -7,8 +9,8 @@ module ApiClient::Parser
|
|
7
9
|
def self.response(response)
|
8
10
|
raise_exception(response.code)
|
9
11
|
begin
|
10
|
-
object = JSON.parse(response.body)
|
11
|
-
rescue JSON::ParserError, TypeError
|
12
|
+
object = ::JSON.parse(response.body)
|
13
|
+
rescue ::JSON::ParserError, TypeError
|
12
14
|
object = {}
|
13
15
|
end
|
14
16
|
object
|
data/lib/api-client/version.rb
CHANGED
@@ -14,6 +14,10 @@ describe ApiClient::Base do
|
|
14
14
|
it "should set #b" do
|
15
15
|
@user.b.should == "b"
|
16
16
|
end
|
17
|
+
|
18
|
+
it "should initialize errors" do
|
19
|
+
@user.errors.should be_an_instance_of(ApiClient::Errors)
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -69,28 +73,6 @@ describe ApiClient::Base do
|
|
69
73
|
end
|
70
74
|
end
|
71
75
|
|
72
|
-
describe "#errors" do
|
73
|
-
context "when @errors is not nil" do
|
74
|
-
before :each do
|
75
|
-
@user = User.new(:errors => {:a => :invalid})
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should return the errors" do
|
79
|
-
@user.errors.messages.should == {:a => ['is invalid']}
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "when @errors is nil" do
|
84
|
-
before :each do
|
85
|
-
@user = User.new
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should instantiate a new instance of ApiClient::Errors" do
|
89
|
-
@user.errors.should be_an_instance_of(ApiClient::Errors)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
76
|
describe "#errors=" do
|
95
77
|
before :each do
|
96
78
|
@user = User.new(:errors => { "a" => "message", "b" => "message" })
|
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.8.
|
4
|
+
version: 1.8.2
|
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-10-
|
12
|
+
date: 2012-10-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -165,18 +165,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
165
|
- - ! '>='
|
166
166
|
- !ruby/object:Gem::Version
|
167
167
|
version: '0'
|
168
|
-
segments:
|
169
|
-
- 0
|
170
|
-
hash: 1498468142604583220
|
171
168
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
169
|
none: false
|
173
170
|
requirements:
|
174
171
|
- - ! '>='
|
175
172
|
- !ruby/object:Gem::Version
|
176
173
|
version: '0'
|
177
|
-
segments:
|
178
|
-
- 0
|
179
|
-
hash: 1498468142604583220
|
180
174
|
requirements: []
|
181
175
|
rubyforge_project:
|
182
176
|
rubygems_version: 1.8.24
|