api-client 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/api-client.rb CHANGED
@@ -5,13 +5,21 @@ module ApiClient
5
5
  autoload :Exceptions, 'api-client/exceptions'
6
6
 
7
7
  def self.get(url = '')
8
- response = Net::HTTP.get_response(URI.parse(url))
8
+ begin
9
+ response = Net::HTTP.get_response(URI.parse(url))
10
+ rescue Errno::ECONNREFUSED
11
+ raise ApiClient::Exceptions::ConnectionRefused
12
+ end
9
13
  raise_exception(response.code)
10
14
  response.body
11
15
  end
12
16
 
13
17
  def self.post(url = '', args = {})
14
- response = Net::HTTP.post_form(URI.parse(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
15
23
  raise_exception(response.code)
16
24
  response.body
17
25
  end
@@ -6,4 +6,5 @@ module ApiClient::Exceptions
6
6
  autoload :InternalServerError, 'api-client/exceptions/internal_server_error'
7
7
  autoload :BadGateway, 'api-client/exceptions/bad_gateway'
8
8
  autoload :ServiceUnavailable, 'api-client/exceptions/service_unavailable'
9
+ autoload :ConnectionRefused, 'api-client/exceptions/connection_refused'
9
10
  end
@@ -0,0 +1,5 @@
1
+ class ApiClient::Exceptions::ConnectionRefused < ApiClient::Exceptions::Generic
2
+ def self.initialize
3
+ super("Connection Refused!")
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiClient
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -2,6 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe ApiClient do
4
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
+
5
15
  context "when response code is 401" do
6
16
  before :each do
7
17
  FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "401")
@@ -74,6 +84,16 @@ describe ApiClient do
74
84
  end
75
85
 
76
86
  describe "#post" do
87
+ context "when connection is refused" do
88
+ before :each do
89
+ Net::HTTP.stub(:post_form).and_raise(Errno::ECONNREFUSED)
90
+ end
91
+
92
+ it "should return a ConnectionRefused exception" do
93
+ lambda { ApiClient.post('http://api.example.com/user/5', {}) }.should raise_error(ApiClient::Exceptions::ConnectionRefused)
94
+ end
95
+ end
96
+
77
97
  context "when response code is 401" do
78
98
  before :each do
79
99
  FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "401")
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.4.1
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -77,6 +77,7 @@ files:
77
77
  - lib/api-client.rb
78
78
  - lib/api-client/exceptions.rb
79
79
  - lib/api-client/exceptions/bad_gateway.rb
80
+ - lib/api-client/exceptions/connection_refused.rb
80
81
  - lib/api-client/exceptions/forbidden.rb
81
82
  - lib/api-client/exceptions/generic.rb
82
83
  - lib/api-client/exceptions/internal_server_error.rb
@@ -100,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
101
  version: '0'
101
102
  segments:
102
103
  - 0
103
- hash: -3754382529348412598
104
+ hash: 3031241521538613176
104
105
  required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  none: false
106
107
  requirements:
@@ -109,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  version: '0'
110
111
  segments:
111
112
  - 0
112
- hash: -3754382529348412598
113
+ hash: 3031241521538613176
113
114
  requirements: []
114
115
  rubyforge_project:
115
116
  rubygems_version: 1.8.24