api-client 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,8 +39,9 @@ Then, on your action, just put into it:
39
39
  @user = User.get("http://api.example.com/user/3")
40
40
 
41
41
  ## TODO
42
- Add Support to Typhoeus and Faraday
43
- Proper Treatment for validation errors
42
+ * Add Support to Typhoeus and Faraday
43
+ * Proper Treatment for validation errors
44
+ * Add more Response Handlers
44
45
 
45
46
  ## Contributing
46
47
 
@@ -48,4 +49,4 @@ Then, on your action, just put into it:
48
49
  2. Create your feature branch (`git checkout -b my-new-feature`)
49
50
  3. Commit your changes (`git commit -am 'Added some feature'`)
50
51
  4. Push to the branch (`git push origin my-new-feature`)
51
- 5. Create new Pull Request
52
+ 5. Create new Pull Request
data/api-client.gemspec CHANGED
@@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
19
19
  gem.add_development_dependency "rspec", "2.9.0"
20
20
 
21
21
  gem.add_runtime_dependency "activemodel"
22
+ gem.add_runtime_dependency "json_pure"
22
23
  end
data/lib/api-client.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require "api-client/version"
2
- require "net/http"
3
- require "active_model"
4
2
 
5
3
  module ApiClient
6
4
  autoload :Exceptions, 'api-client/exceptions'
7
5
  autoload :Base, 'api-client/base'
6
+ autoload :Dispatcher, 'api-client/dispatcher'
7
+ autoload :Parser, 'api-client/parser'
8
8
  end
@@ -1,8 +1,13 @@
1
+ require "active_model"
2
+
1
3
  class ApiClient::Base
2
4
  include ActiveModel::Validations
3
5
  include ActiveModel::Conversion
4
6
  extend ActiveModel::Naming
5
7
 
8
+ extend ApiClient::Parser
9
+ extend ApiClient::Dispatcher
10
+
6
11
  def initialize(attributes = {})
7
12
  attributes.each do |name, value|
8
13
  send("#{name}=", value)
@@ -14,23 +19,23 @@ class ApiClient::Base
14
19
  end
15
20
 
16
21
  def self.get(url = '')
17
- call do Net::HTTP.get_response(URI.parse(url)) end
22
+ dispatch { _get(url) }
18
23
  end
19
24
 
20
25
  def self.post(url = '', args = {})
21
- call do Net::HTTP.post_form(URI.parse(url), args) end
26
+ dispatch { _post(url, args) }
22
27
  end
23
28
 
24
29
  protected
25
30
 
26
- def self.call
31
+ def self.dispatch
27
32
  begin
28
- response = yield
33
+ code, body = _response(yield)
29
34
  rescue Errno::ECONNREFUSED
30
35
  raise ApiClient::Exceptions::ConnectionRefused
31
36
  end
32
- raise_exception(response.code)
33
- new(JSON.parse(response.body))
37
+ raise_exception(code)
38
+ new(body)
34
39
  end
35
40
 
36
41
  def self.raise_exception(code)
@@ -41,6 +46,7 @@ class ApiClient::Base
41
46
  when '500' then raise ApiClient::Exceptions::InternalServerError
42
47
  when '502' then raise ApiClient::Exceptions::BadGateway
43
48
  when '503' then raise ApiClient::Exceptions::ServiceUnavailable
49
+ else return
44
50
  end
45
51
  end
46
52
  end
@@ -0,0 +1,13 @@
1
+ require "net/http"
2
+
3
+ module ApiClient::Dispatcher
4
+ def _get(url = '')
5
+ Net::HTTP.get_response(URI.parse(url))
6
+ end
7
+
8
+ def _post(url = '', args = {})
9
+ uri = URI(url)
10
+ http = Net::HTTP.new(uri.host)
11
+ http.post(uri.path, args.to_json, { 'Content-Type' => 'application/json' })
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module ApiClient::Parser
2
+ def _response(response)
3
+ begin
4
+ body = JSON.parse(response.body)
5
+ rescue JSON::ParserError
6
+ body = nil
7
+ end
8
+ return response.code, body
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiClient
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -6,7 +6,7 @@ describe ApiClient::Base do
6
6
  describe "#get" do
7
7
  context "when connection is refused" do
8
8
  before :each do
9
- Net::HTTP.stub(:get_response).and_raise(Errno::ECONNREFUSED)
9
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :exception => Errno::ECONNREFUSED)
10
10
  end
11
11
 
12
12
  it "should return a ConnectionRefused exception" do
@@ -6,7 +6,7 @@ describe ApiClient::Base do
6
6
  describe "#post" do
7
7
  context "when connection is refused" do
8
8
  before :each do
9
- Net::HTTP.stub(:post_form).and_raise(Errno::ECONNREFUSED)
9
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :exception => Errno::ECONNREFUSED)
10
10
  end
11
11
 
12
12
  it "should return a ConnectionRefused exception" do
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient::Dispatcher do
4
+ describe "#_get" do
5
+ before :each do
6
+ FakeWeb.register_uri(:get, "http://api.example.com/user/5", :status => "200")
7
+ end
8
+
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)
11
+ end
12
+ end
13
+
14
+ describe "#_post" do
15
+ before :each do
16
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5", :status => "201")
17
+ end
18
+
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)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient::Parser do
4
+ describe "#_response" do
5
+ context "with a valid json response" do
6
+ before :each do
7
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5",
8
+ :body => { :a => :b }.to_json,
9
+ :status => "201")
10
+ @response = ApiClient::Base._post('http://api.example.com/user/5', {})
11
+ end
12
+
13
+ it "should return the response code and the body parsed" do
14
+ ApiClient::Base._response(@response).should == ['201', { "a" => "b" }]
15
+ end
16
+ end
17
+
18
+ context "with a invalid json response" do
19
+ before :each do
20
+ FakeWeb.register_uri(:post, "http://api.example.com/user/5",
21
+ :body => "wrong",
22
+ :status => "201")
23
+ @response = ApiClient::Base._post('http://api.example.com/user/5', {})
24
+ end
25
+
26
+ it "should return the response code and a nil body" do
27
+ ApiClient::Base._response(@response).should == ['201', nil]
28
+ end
29
+ end
30
+ end
31
+ 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: 1.0.0
4
+ version: 1.1.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-22 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: json_pure
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description:
79
95
  email: plribeiro3000@gmail.com
80
96
  executables: []
@@ -92,6 +108,7 @@ files:
92
108
  - api-client.gemspec
93
109
  - lib/api-client.rb
94
110
  - lib/api-client/base.rb
111
+ - lib/api-client/dispatcher.rb
95
112
  - lib/api-client/exceptions.rb
96
113
  - lib/api-client/exceptions/bad_gateway.rb
97
114
  - lib/api-client/exceptions/connection_refused.rb
@@ -101,10 +118,13 @@ files:
101
118
  - lib/api-client/exceptions/not_found.rb
102
119
  - lib/api-client/exceptions/service_unavailable.rb
103
120
  - lib/api-client/exceptions/unauthorized.rb
121
+ - lib/api-client/parser.rb
104
122
  - lib/api-client/version.rb
105
123
  - spec/api-client/base/get_spec.rb
106
124
  - spec/api-client/base/post_spec.rb
107
125
  - spec/api-client/base_spec.rb
126
+ - spec/api-client/dispatcher_spec.rb
127
+ - spec/api-client/parser_spec.rb
108
128
  - spec/spec_helper.rb
109
129
  homepage: ''
110
130
  licenses: []
@@ -118,12 +138,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
138
  - - ! '>='
119
139
  - !ruby/object:Gem::Version
120
140
  version: '0'
141
+ segments:
142
+ - 0
143
+ hash: -2592261694451152331
121
144
  required_rubygems_version: !ruby/object:Gem::Requirement
122
145
  none: false
123
146
  requirements:
124
147
  - - ! '>='
125
148
  - !ruby/object:Gem::Version
126
149
  version: '0'
150
+ segments:
151
+ - 0
152
+ hash: -2592261694451152331
127
153
  requirements: []
128
154
  rubyforge_project:
129
155
  rubygems_version: 1.8.24