api-client 0.2.0 → 0.3.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.rb CHANGED
@@ -6,13 +6,21 @@ module ApiClient
6
6
 
7
7
  def self.get(url = '')
8
8
  response = Net::HTTP.get_response(URI.parse(url))
9
- raise ApiClient::Exceptions::NotFound if response.code == '404'
9
+ raise_exception(response.code)
10
10
  response.body
11
11
  end
12
12
 
13
13
  def self.post(url = '', args = {})
14
14
  response = Net::HTTP.post_form(URI.parse(url), args)
15
- raise ApiClient::Exceptions::NotFound if response.code == '404'
15
+ raise_exception(response.code)
16
16
  response.body
17
17
  end
18
+
19
+ def self.raise_exception(code)
20
+ case code
21
+ when '401' then raise ApiClient::Exceptions::Unauthorized
22
+ when '403' then raise ApiClient::Exceptions::Forbidden
23
+ when '404' then raise ApiClient::Exceptions::NotFound
24
+ end
25
+ end
18
26
  end
@@ -1,4 +1,6 @@
1
1
  module ApiClient::Exceptions
2
2
  autoload :Generic, 'api_client/exceptions/generic'
3
3
  autoload :NotFound, 'api_client/exceptions/not_found'
4
+ autoload :Unauthorized, 'api_client/exceptions/unauthorized'
5
+ autoload :Forbidden, 'api_client/exceptions/forbidden'
4
6
  end
@@ -0,0 +1,5 @@
1
+ class ApiClient::Exceptions::Forbidden < ApiClient::Exceptions::Generic
2
+ def self.initialize
3
+ super("Forbidden!")
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class ApiClient::Exceptions::Unauthorized < ApiClient::Exceptions::Generic
2
+ def self.initialize
3
+ super("Authentication Required!")
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiClient
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -2,6 +2,26 @@ require 'spec_helper'
2
2
 
3
3
  describe ApiClient do
4
4
  describe "#get" do
5
+ context "when response code is 401" do
6
+ before :each do
7
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "401")
8
+ end
9
+
10
+ it "should return a Unauthorized exception" do
11
+ lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Unauthorized)
12
+ end
13
+ end
14
+
15
+ context "when response code is 403" do
16
+ before :each do
17
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "403")
18
+ end
19
+
20
+ it "should return a Forbidden exception" do
21
+ lambda { ApiClient.get('http://api.example.com/user/5') }.should raise_error(ApiClient::Exceptions::Forbidden)
22
+ end
23
+ end
24
+
5
25
  context "when response code is 404" do
6
26
  before :each do
7
27
  FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "404")
@@ -12,7 +32,7 @@ describe ApiClient do
12
32
  end
13
33
  end
14
34
 
15
- context "when response code is not 404" do
35
+ context "when response code is 2xx" do
16
36
  before :each do
17
37
  FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "201", :body => "User#3333")
18
38
  end
@@ -24,6 +44,26 @@ describe ApiClient do
24
44
  end
25
45
 
26
46
  describe "#post" do
47
+ context "when response code is 401" do
48
+ before :each do
49
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "401")
50
+ end
51
+
52
+ it "should return a Unauthorized exception" do
53
+ lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Unauthorized)
54
+ end
55
+ end
56
+
57
+ context "when response code is 403" do
58
+ before :each do
59
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "403")
60
+ end
61
+
62
+ it "should return a Forbidden exception" do
63
+ lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::Forbidden)
64
+ end
65
+ end
66
+
27
67
  context "when response code is 404" do
28
68
  before :each do
29
69
  FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "404")
@@ -34,7 +74,7 @@ describe ApiClient do
34
74
  end
35
75
  end
36
76
 
37
- context "when response code is not 404" do
77
+ context "when response code is 2xx" do
38
78
  before :each do
39
79
  FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "201", :body => "User#3333")
40
80
  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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -76,8 +76,10 @@ files:
76
76
  - api_client.gemspec
77
77
  - lib/api_client.rb
78
78
  - lib/api_client/exceptions.rb
79
+ - lib/api_client/exceptions/forbidden.rb
79
80
  - lib/api_client/exceptions/generic.rb
80
81
  - lib/api_client/exceptions/not_found.rb
82
+ - lib/api_client/exceptions/unauthorized.rb
81
83
  - lib/api_client/version.rb
82
84
  - spec/api_client_spec.rb
83
85
  - spec/spec_helper.rb
@@ -95,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
97
  version: '0'
96
98
  segments:
97
99
  - 0
98
- hash: 343196442322712530
100
+ hash: 2776050447595932195
99
101
  required_rubygems_version: !ruby/object:Gem::Requirement
100
102
  none: false
101
103
  requirements:
@@ -104,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
106
  version: '0'
105
107
  segments:
106
108
  - 0
107
- hash: 343196442322712530
109
+ hash: 2776050447595932195
108
110
  requirements: []
109
111
  rubyforge_project:
110
112
  rubygems_version: 1.8.24