api-client 0.6.0 → 1.0.0

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/README.md CHANGED
@@ -30,9 +30,17 @@ You can define a more generic rescue that will work for any error:
30
30
 
31
31
  rescue_from ApiClient::Exceptions::Generic, :with => :generic_error
32
32
 
33
+ On Your model, extend ApiClient::Base
34
+
35
+ def User < Apiclient::Base
36
+
33
37
  Then, on your action, just put into it:
34
38
 
35
- @user = ApiClient.get("http://api.example.com/user/3")
39
+ @user = User.get("http://api.example.com/user/3")
40
+
41
+ ## TODO
42
+ Add Support to Typhoeus and Faraday
43
+ Proper Treatment for validation errors
36
44
 
37
45
  ## Contributing
38
46
 
@@ -40,4 +48,4 @@ Then, on your action, just put into it:
40
48
  2. Create your feature branch (`git checkout -b my-new-feature`)
41
49
  3. Commit your changes (`git commit -am 'Added some feature'`)
42
50
  4. Push to the branch (`git push origin my-new-feature`)
43
- 5. Create new Pull Request
51
+ 5. Create new Pull Request
@@ -17,4 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.add_development_dependency "rake"
18
18
  gem.add_development_dependency "fakeweb"
19
19
  gem.add_development_dependency "rspec", "2.9.0"
20
+
21
+ gem.add_runtime_dependency "activemodel"
20
22
  end
@@ -1,38 +1,8 @@
1
1
  require "api-client/version"
2
2
  require "net/http"
3
+ require "active_model"
3
4
 
4
5
  module ApiClient
5
6
  autoload :Exceptions, 'api-client/exceptions'
6
-
7
- def self.get(url = '')
8
- begin
9
- response = Net::HTTP.get_response(URI.parse(url))
10
- rescue Errno::ECONNREFUSED
11
- raise ApiClient::Exceptions::ConnectionRefused
12
- end
13
- raise_exception(response.code)
14
- response.body
15
- end
16
-
17
- def self.post(url = '', args = {})
18
- begin
19
- response = Net::HTTP.post_form(URI.parse(url), args)
20
- rescue Errno::ECONNREFUSED
21
- raise ApiClient::Exceptions::ConnectionRefused
22
- end
23
- raise_exception(response.code)
24
- response.body
25
- end
26
-
27
- def self.raise_exception(code)
28
- case code
29
- when '400' then raise ApiClient::Exceptions::BadRequest
30
- when '401' then raise ApiClient::Exceptions::Unauthorized
31
- when '403' then raise ApiClient::Exceptions::Forbidden
32
- when '404' then raise ApiClient::Exceptions::NotFound
33
- when '500' then raise ApiClient::Exceptions::InternalServerError
34
- when '502' then raise ApiClient::Exceptions::BadGateway
35
- when '503' then raise ApiClient::Exceptions::ServiceUnavailable
36
- end
37
- end
7
+ autoload :Base, 'api-client/base'
38
8
  end
@@ -0,0 +1,46 @@
1
+ class ApiClient::Base
2
+ include ActiveModel::Validations
3
+ include ActiveModel::Conversion
4
+ extend ActiveModel::Naming
5
+
6
+ def initialize(attributes = {})
7
+ attributes.each do |name, value|
8
+ send("#{name}=", value)
9
+ end
10
+ end
11
+
12
+ def persisted?
13
+ false
14
+ end
15
+
16
+ def self.get(url = '')
17
+ call do Net::HTTP.get_response(URI.parse(url)) end
18
+ end
19
+
20
+ def self.post(url = '', args = {})
21
+ call do Net::HTTP.post_form(URI.parse(url), args) end
22
+ end
23
+
24
+ protected
25
+
26
+ def self.call
27
+ begin
28
+ response = yield
29
+ rescue Errno::ECONNREFUSED
30
+ raise ApiClient::Exceptions::ConnectionRefused
31
+ end
32
+ raise_exception(response.code)
33
+ new(JSON.parse(response.body))
34
+ end
35
+
36
+ def self.raise_exception(code)
37
+ case code
38
+ when '401' then raise ApiClient::Exceptions::Unauthorized
39
+ when '403' then raise ApiClient::Exceptions::Forbidden
40
+ when '404' then raise ApiClient::Exceptions::NotFound
41
+ when '500' then raise ApiClient::Exceptions::InternalServerError
42
+ when '502' then raise ApiClient::Exceptions::BadGateway
43
+ when '503' then raise ApiClient::Exceptions::ServiceUnavailable
44
+ end
45
+ end
46
+ end
@@ -7,5 +7,4 @@ module ApiClient::Exceptions
7
7
  autoload :BadGateway, 'api-client/exceptions/bad_gateway'
8
8
  autoload :ServiceUnavailable, 'api-client/exceptions/service_unavailable'
9
9
  autoload :ConnectionRefused, 'api-client/exceptions/connection_refused'
10
- autoload :BadRequest, 'api-client/exceptions/bad_request'
11
10
  end
@@ -1,3 +1,3 @@
1
1
  module ApiClient
2
- VERSION = "0.6.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient::Base do
4
+ let(:user) { User.new(:a => "a", :b => "b") }
5
+
6
+ describe "#get" do
7
+ context "when connection is refused" do
8
+ before :each do
9
+ Net::HTTP.stub(:get_response).and_raise(Errno::ECONNREFUSED)
10
+ end
11
+
12
+ it "should return a ConnectionRefused exception" do
13
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::ConnectionRefused)
14
+ end
15
+ end
16
+
17
+ context "when response code is 401" do
18
+ before :each do
19
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "401")
20
+ end
21
+
22
+ it "should return a Unauthorized exception" do
23
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Unauthorized)
24
+ end
25
+ end
26
+
27
+ context "when response code is 403" do
28
+ before :each do
29
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "403")
30
+ end
31
+
32
+ it "should return a Forbidden exception" do
33
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Forbidden)
34
+ end
35
+ end
36
+
37
+ context "when response code is 404" do
38
+ before :each do
39
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "404")
40
+ end
41
+
42
+ it "should return a NotFound exception" do
43
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::NotFound)
44
+ end
45
+ end
46
+
47
+ context "when response code is 500" do
48
+ before :each do
49
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "500")
50
+ end
51
+
52
+ it "should return a InternalServerError exception" do
53
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::InternalServerError)
54
+ end
55
+ end
56
+
57
+ context "when response code is 502" do
58
+ before :each do
59
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "502")
60
+ end
61
+
62
+ it "should return a BadGateway exception" do
63
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::BadGateway)
64
+ end
65
+ end
66
+
67
+ context "when response code is 503" do
68
+ before :each do
69
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "503")
70
+ end
71
+
72
+ it "should return a ServiceUnavailable exception" do
73
+ lambda { ApiClient::Base.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::ServiceUnavailable)
74
+ end
75
+ end
76
+
77
+ context "when response code is 2xx" do
78
+ before :each do
79
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "201", :body => '{"a": "a", "b": "b"}')
80
+ User.stub(:new).and_return(user)
81
+ end
82
+
83
+ it "should return a object intialized with the response" do
84
+ User.get('http://api.example.com/user/5').should == user
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient::Base do
4
+ let(:user) { User.new(:a => "a", :b => "b") }
5
+
6
+ describe "#post" do
7
+ context "when connection is refused" do
8
+ before :each do
9
+ Net::HTTP.stub(:post_form).and_raise(Errno::ECONNREFUSED)
10
+ end
11
+
12
+ it "should return a ConnectionRefused exception" do
13
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::ConnectionRefused)
14
+ end
15
+ end
16
+
17
+ context "when response code is 401" do
18
+ before :each do
19
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "401")
20
+ end
21
+
22
+ it "should return a Unauthorized exception" do
23
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Unauthorized)
24
+ end
25
+ end
26
+
27
+ context "when response code is 403" do
28
+ before :each do
29
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "403")
30
+ end
31
+
32
+ it "should return a Forbidden exception" do
33
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Forbidden)
34
+ end
35
+ end
36
+
37
+ context "when response code is 404" do
38
+ before :each do
39
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "404")
40
+ end
41
+
42
+ it "should return a NotFound exception" do
43
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::NotFound)
44
+ end
45
+ end
46
+
47
+ context "when response code is 500" do
48
+ before :each do
49
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "500")
50
+ end
51
+
52
+ it "should return a InternalServerError exception" do
53
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::InternalServerError)
54
+ end
55
+ end
56
+
57
+ context "when response code is 502" do
58
+ before :each do
59
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "502")
60
+ end
61
+
62
+ it "should return a BadGateway exception" do
63
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::BadGateway)
64
+ end
65
+ end
66
+
67
+ context "when response code is 503" do
68
+ before :each do
69
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "503")
70
+ end
71
+
72
+ it "should return a ServiceUnavailable exception" do
73
+ lambda { ApiClient::Base.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::ServiceUnavailable)
74
+ end
75
+ end
76
+
77
+ context "when response code is 2xx" do
78
+ before :each do
79
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "201", :body => '{"a": "a", "b": "b"}')
80
+ User.stub(:new).and_return(user)
81
+ end
82
+
83
+ it "should return a object intialized with the response" do
84
+ User.post('http://api.example.com/user/5', {}).should == user
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient::Base do
4
+ describe "#initialize" do
5
+ context "with a hash {:a => 'a', :b => 'b'}" do
6
+ before :each do
7
+ @user = User.new({:a => "a", :b => "b"})
8
+ end
9
+
10
+ it "should set #a" do
11
+ @user.a.should == "a"
12
+ end
13
+
14
+ it "should set #b" do
15
+ @user.b.should == "b"
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "#persisted?" do
21
+ it "should return false" do
22
+ User.new.should_not be_persisted
23
+ end
24
+ end
25
+ end
@@ -1,4 +1,9 @@
1
1
  require "rspec"
2
2
  require "fakeweb"
3
+ require "json"
3
4
 
4
- require File.dirname(__FILE__) + "/../lib/api-client"
5
+ require File.dirname(__FILE__) + "/../lib/api-client"
6
+
7
+ class User < ApiClient::Base
8
+ attr_accessor :a, :b
9
+ 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: 0.6.0
4
+ version: 1.0.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-06-04 00:00:00.000000000 Z
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 2.9.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: activemodel
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description:
63
79
  email: plribeiro3000@gmail.com
64
80
  executables: []
@@ -75,9 +91,9 @@ files:
75
91
  - Rakefile
76
92
  - api-client.gemspec
77
93
  - lib/api-client.rb
94
+ - lib/api-client/base.rb
78
95
  - lib/api-client/exceptions.rb
79
96
  - lib/api-client/exceptions/bad_gateway.rb
80
- - lib/api-client/exceptions/bad_request.rb
81
97
  - lib/api-client/exceptions/connection_refused.rb
82
98
  - lib/api-client/exceptions/forbidden.rb
83
99
  - lib/api-client/exceptions/generic.rb
@@ -86,7 +102,9 @@ files:
86
102
  - lib/api-client/exceptions/service_unavailable.rb
87
103
  - lib/api-client/exceptions/unauthorized.rb
88
104
  - lib/api-client/version.rb
89
- - spec/api_client_spec.rb
105
+ - spec/api-client/base/get_spec.rb
106
+ - spec/api-client/base/post_spec.rb
107
+ - spec/api-client/base_spec.rb
90
108
  - spec/spec_helper.rb
91
109
  homepage: ''
92
110
  licenses: []
@@ -1,5 +0,0 @@
1
- class ApiClient::Exceptions::BadRequest < ApiClient::Exceptions::Generic
2
- def self.initialize
3
- super("Bad Request!")
4
- end
5
- end
@@ -1,187 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ApiClient do
4
- describe "#get" do
5
- context "when connection is refused" do
6
- before :each do
7
- Net::HTTP.stub(:get_response).and_raise(Errno::ECONNREFUSED)
8
- end
9
-
10
- it "should return a ConnectionRefused exception" do
11
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::ConnectionRefused)
12
- end
13
- end
14
-
15
- context "when response code is 400" do
16
- before :each do
17
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "400")
18
- end
19
-
20
- it "should return a BadRequest exception" do
21
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::BadRequest)
22
- end
23
- end
24
-
25
- context "when response code is 401" do
26
- before :each do
27
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "401")
28
- end
29
-
30
- it "should return a Unauthorized exception" do
31
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Unauthorized)
32
- end
33
- end
34
-
35
- context "when response code is 403" do
36
- before :each do
37
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "403")
38
- end
39
-
40
- it "should return a Forbidden exception" do
41
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Forbidden)
42
- end
43
- end
44
-
45
- context "when response code is 404" do
46
- before :each do
47
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "404")
48
- end
49
-
50
- it "should return a NotFound exception" do
51
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::NotFound)
52
- end
53
- end
54
-
55
- context "when response code is 500" do
56
- before :each do
57
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "500")
58
- end
59
-
60
- it "should return a InternalServerError exception" do
61
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::InternalServerError)
62
- end
63
- end
64
-
65
- context "when response code is 502" do
66
- before :each do
67
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "502")
68
- end
69
-
70
- it "should return a BadGateway exception" do
71
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::BadGateway)
72
- end
73
- end
74
-
75
- context "when response code is 503" do
76
- before :each do
77
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "503")
78
- end
79
-
80
- it "should return a ServiceUnavailable exception" do
81
- lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::ServiceUnavailable)
82
- end
83
- end
84
-
85
- context "when response code is 2xx" do
86
- before :each do
87
- FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "201", :body => "User#3333")
88
- end
89
-
90
- it "should return the response body" do
91
- ApiClient.get('http://api.example.com/user/5').should == "User#3333"
92
- end
93
- end
94
- end
95
-
96
- describe "#post" do
97
- context "when connection is refused" do
98
- before :each do
99
- Net::HTTP.stub(:post_form).and_raise(Errno::ECONNREFUSED)
100
- end
101
-
102
- it "should return a ConnectionRefused exception" do
103
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::ConnectionRefused)
104
- end
105
- end
106
-
107
- context "when response code is 400" do
108
- before :each do
109
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "400")
110
- end
111
-
112
- it "should return a BadRequest exception" do
113
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::BadRequest)
114
- end
115
- end
116
-
117
- context "when response code is 401" do
118
- before :each do
119
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "401")
120
- end
121
-
122
- it "should return a Unauthorized exception" do
123
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Unauthorized)
124
- end
125
- end
126
-
127
- context "when response code is 403" do
128
- before :each do
129
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "403")
130
- end
131
-
132
- it "should return a Forbidden exception" do
133
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Forbidden)
134
- end
135
- end
136
-
137
- context "when response code is 404" do
138
- before :each do
139
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "404")
140
- end
141
-
142
- it "should return a NotFound exception" do
143
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::NotFound)
144
- end
145
- end
146
-
147
- context "when response code is 500" do
148
- before :each do
149
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "500")
150
- end
151
-
152
- it "should return a InternalServerError exception" do
153
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::InternalServerError)
154
- end
155
- end
156
-
157
- context "when response code is 502" do
158
- before :each do
159
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "502")
160
- end
161
-
162
- it "should return a BadGateway exception" do
163
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::BadGateway)
164
- end
165
- end
166
-
167
- context "when response code is 503" do
168
- before :each do
169
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "503")
170
- end
171
-
172
- it "should return a ServiceUnavailable exception" do
173
- lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::ServiceUnavailable)
174
- end
175
- end
176
-
177
- context "when response code is 2xx" do
178
- before :each do
179
- FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "201", :body => "User#3333")
180
- end
181
-
182
- it "should return the response body" do
183
- ApiClient.post('http://api.example.com/user/5', {}).should == "User#3333"
184
- end
185
- end
186
- end
187
- end