api-client 1.1.1 → 1.2.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/lib/api-client/base.rb +16 -4
- data/lib/api-client/dispatcher.rb +28 -6
- data/lib/api-client/version.rb +1 -1
- data/spec/api-client/base/delete_spec.rb +88 -0
- data/spec/api-client/base/patch_spec.rb +99 -0
- data/spec/api-client/base/put_spec.rb +99 -0
- data/spec/api-client/dispatcher_spec.rb +32 -2
- data/spec/api-client/parser_spec.rb +2 -2
- metadata +11 -2
data/lib/api-client/base.rb
CHANGED
@@ -18,12 +18,24 @@ class ApiClient::Base
|
|
18
18
|
false
|
19
19
|
end
|
20
20
|
|
21
|
-
def self.get(url = '')
|
22
|
-
dispatch { _get(url) }
|
21
|
+
def self.get(url = '', header = {})
|
22
|
+
dispatch { _get(url, header) }
|
23
23
|
end
|
24
24
|
|
25
|
-
def self.post(url = '', args = {})
|
26
|
-
dispatch { _post(url, args) }
|
25
|
+
def self.post(url = '', args = {}, header = {})
|
26
|
+
dispatch { _post(url, args, header) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.put(url = '', args = {}, header = {})
|
30
|
+
dispatch { _put(url, args, header) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.patch(url = '', args = {}, header = {})
|
34
|
+
dispatch { _patch(url, args, header) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.delete(url = '', header = {})
|
38
|
+
dispatch { _delete(url, header) }
|
27
39
|
end
|
28
40
|
|
29
41
|
protected
|
@@ -1,13 +1,35 @@
|
|
1
1
|
require "net/http"
|
2
2
|
|
3
3
|
module ApiClient::Dispatcher
|
4
|
-
def _get(url
|
5
|
-
|
4
|
+
def _get(url, header)
|
5
|
+
initialize_connection(url)
|
6
|
+
@http.get(@uri.path, header)
|
6
7
|
end
|
7
8
|
|
8
|
-
def _post(url
|
9
|
-
|
10
|
-
http
|
11
|
-
|
9
|
+
def _post(url, args, header)
|
10
|
+
initialize_connection(url)
|
11
|
+
@http.post(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header))
|
12
|
+
end
|
13
|
+
|
14
|
+
def _put(url, args, header)
|
15
|
+
initialize_connection(url)
|
16
|
+
@http.put(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header))
|
17
|
+
end
|
18
|
+
|
19
|
+
def _patch(url, args, header)
|
20
|
+
initialize_connection(url)
|
21
|
+
@http.patch(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header))
|
22
|
+
end
|
23
|
+
|
24
|
+
def _delete(url, header)
|
25
|
+
initialize_connection(url)
|
26
|
+
@http.delete(@uri.path, header)
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def initialize_connection(url = '')
|
32
|
+
@uri = URI(url)
|
33
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
12
34
|
end
|
13
35
|
end
|
data/lib/api-client/version.rb
CHANGED
@@ -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 "#delete" do
|
7
|
+
context "when connection is refused" do
|
8
|
+
before :each do
|
9
|
+
FakeWeb.register_uri(:delete, "http://api.example.com/user/5", :exception => Errno::ECONNREFUSED)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return a ConnectionRefused exception" do
|
13
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "401")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a Unauthorized exception" do
|
23
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "403")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a Forbidden exception" do
|
33
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "404")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a NotFound exception" do
|
43
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "500")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return a InternalServerError exception" do
|
53
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "502")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return a BadGateway exception" do
|
63
|
+
lambda { ApiClient::Base.delete('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(:delete, "http://api.example.com/user/5", :status => "503")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return a ServiceUnavailable exception" do
|
73
|
+
lambda { ApiClient::Base.delete('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(:delete, "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.delete('http://api.example.com/user/5').should == user
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
let(:user) { User.new(:a => "a", :b => "b") }
|
5
|
+
|
6
|
+
describe "#patch" do
|
7
|
+
context "when connection is refused" do
|
8
|
+
before :each do
|
9
|
+
FakeWeb.register_uri(:patch, "http://api.example.com/user/5", :exception => Errno::ECONNREFUSED)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return a ConnectionRefused exception" do
|
13
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "401")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a Unauthorized exception" do
|
23
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "403")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a Forbidden exception" do
|
33
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "404")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a NotFound exception" do
|
43
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "500")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return a InternalServerError exception" do
|
53
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "502")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return a BadGateway exception" do
|
63
|
+
lambda { ApiClient::Base.patch('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(:patch, "http://api.example.com/user/5", :status => "503")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return a ServiceUnavailable exception" do
|
73
|
+
lambda { ApiClient::Base.patch('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(:patch, "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 initialized with the response" do
|
84
|
+
User.patch('http://api.example.com/user/5', {}).should == user
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "with a specified port" do
|
89
|
+
before :each do
|
90
|
+
FakeWeb.register_uri(:patch, "http://api.example.com:3001/user/5", :status => "201", :body => '{"a": "a", "b": "b"}')
|
91
|
+
User.stub(:new).and_return(user)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should return a object initialized with the response" do
|
95
|
+
User.patch('http://api.example.com:3001/user/5', {}).should == user
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiClient::Base do
|
4
|
+
let(:user) { User.new(:a => "a", :b => "b") }
|
5
|
+
|
6
|
+
describe "#put" do
|
7
|
+
context "when connection is refused" do
|
8
|
+
before :each do
|
9
|
+
FakeWeb.register_uri(:put, "http://api.example.com/user/5", :exception => Errno::ECONNREFUSED)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return a ConnectionRefused exception" do
|
13
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "401")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a Unauthorized exception" do
|
23
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "403")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return a Forbidden exception" do
|
33
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "404")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return a NotFound exception" do
|
43
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "500")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return a InternalServerError exception" do
|
53
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "502")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return a BadGateway exception" do
|
63
|
+
lambda { ApiClient::Base.put('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(:put, "http://api.example.com/user/5", :status => "503")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return a ServiceUnavailable exception" do
|
73
|
+
lambda { ApiClient::Base.put('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(:put, "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 initialized with the response" do
|
84
|
+
User.put('http://api.example.com/user/5', {}).should == user
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "with a specified port" do
|
89
|
+
before :each do
|
90
|
+
FakeWeb.register_uri(:put, "http://api.example.com:3001/user/5", :status => "201", :body => '{"a": "a", "b": "b"}')
|
91
|
+
User.stub(:new).and_return(user)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should return a object initialized with the response" do
|
95
|
+
User.put('http://api.example.com:3001/user/5', {}).should == user
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -7,7 +7,7 @@ describe ApiClient::Dispatcher do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should return the response code and body" do
|
10
|
-
ApiClient::Base._get("http://api.example.com/user/5").should be_a(Net::HTTPOK)
|
10
|
+
ApiClient::Base._get("http://api.example.com/user/5", {}).should be_a(Net::HTTPOK)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -17,7 +17,37 @@ describe ApiClient::Dispatcher do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should return the response code and body" do
|
20
|
-
ApiClient::Base._post("http://api.example.com/user/5", {}).should be_a(Net::HTTPCreated)
|
20
|
+
ApiClient::Base._post("http://api.example.com/user/5", {}, {}).should be_a(Net::HTTPCreated)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#_put" do
|
25
|
+
before :each do
|
26
|
+
FakeWeb.register_uri(:put, "http://api.example.com/user/5", :status => "200")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return the response code and body" do
|
30
|
+
ApiClient::Base._put("http://api.example.com/user/5", {}, {}).should be_a(Net::HTTPOK)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#_patch" do
|
35
|
+
before :each do
|
36
|
+
FakeWeb.register_uri(:patch, "http://api.example.com/user/5", :status => "200")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return the response code and body" do
|
40
|
+
ApiClient::Base._patch("http://api.example.com/user/5", {}, {}).should be_a(Net::HTTPOK)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#_delete" do
|
45
|
+
before :each do
|
46
|
+
FakeWeb.register_uri(:delete, "http://api.example.com/user/5", :status => "200")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return the response code and body" do
|
50
|
+
ApiClient::Base._delete("http://api.example.com/user/5", {}).should be_a(Net::HTTPOK)
|
21
51
|
end
|
22
52
|
end
|
23
53
|
end
|
@@ -7,7 +7,7 @@ describe ApiClient::Parser do
|
|
7
7
|
FakeWeb.register_uri(:post, "http://api.example.com/user/5",
|
8
8
|
:body => { :a => :b }.to_json,
|
9
9
|
:status => "201")
|
10
|
-
@response = ApiClient::Base._post('http://api.example.com/user/5', {})
|
10
|
+
@response = ApiClient::Base._post('http://api.example.com/user/5', {}, {})
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return the response code and the body parsed" do
|
@@ -20,7 +20,7 @@ describe ApiClient::Parser do
|
|
20
20
|
FakeWeb.register_uri(:post, "http://api.example.com/user/5",
|
21
21
|
:body => "wrong",
|
22
22
|
:status => "201")
|
23
|
-
@response = ApiClient::Base._post('http://api.example.com/user/5', {})
|
23
|
+
@response = ApiClient::Base._post('http://api.example.com/user/5', {}, {})
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return the response code and a nil body" do
|
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.2.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-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -120,8 +120,11 @@ files:
|
|
120
120
|
- lib/api-client/exceptions/unauthorized.rb
|
121
121
|
- lib/api-client/parser.rb
|
122
122
|
- lib/api-client/version.rb
|
123
|
+
- spec/api-client/base/delete_spec.rb
|
123
124
|
- spec/api-client/base/get_spec.rb
|
125
|
+
- spec/api-client/base/patch_spec.rb
|
124
126
|
- spec/api-client/base/post_spec.rb
|
127
|
+
- spec/api-client/base/put_spec.rb
|
125
128
|
- spec/api-client/base_spec.rb
|
126
129
|
- spec/api-client/dispatcher_spec.rb
|
127
130
|
- spec/api-client/parser_spec.rb
|
@@ -138,12 +141,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
141
|
- - ! '>='
|
139
142
|
- !ruby/object:Gem::Version
|
140
143
|
version: '0'
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
hash: 2002336376780002209
|
141
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
148
|
none: false
|
143
149
|
requirements:
|
144
150
|
- - ! '>='
|
145
151
|
- !ruby/object:Gem::Version
|
146
152
|
version: '0'
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
hash: 2002336376780002209
|
147
156
|
requirements: []
|
148
157
|
rubyforge_project:
|
149
158
|
rubygems_version: 1.8.24
|