web_client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,34 +4,39 @@ module WebClient
4
4
 
5
5
  attr_reader :http
6
6
 
7
- def initialize(host, port=nil)
7
+ def initialize(*args)
8
+ if args.first.is_a? Hash
9
+ host = args[0]['host'] || args[0][:host]
10
+ port = args[0]['port'] || args[0][:port]
11
+ else
12
+ host = args[0]
13
+ port = args[1]
14
+ end
8
15
  @http = HTTP.new(host, port)
9
16
  end
10
17
 
11
- def get(path, data={}, &block)
12
- request(HTTP::Get, path, data, &block)
13
- end
14
-
15
- def post(path, data, &block)
16
- request(HTTP::Post, path, data, &block)
17
- end
18
-
19
- def put(path, data, &block)
20
- request(HTTP::Put, path, data, &block)
21
- end
22
-
23
- def delete(path, data={}, &block)
24
- request(HTTP::Delete, path, data, &block)
18
+ WebClient::HTTP_METHODS.each do |http_method|
19
+ define_method http_method.to_s.demodulize.downcase do |path='/', data={}, &block|
20
+ request(http_method, path, data, &block)
21
+ end
25
22
  end
26
23
 
27
24
  private
28
25
 
29
- def request(method_class, path, data=nil)
30
- request = method_class.new(path)
31
- request.set_form_data(data) if data.is_a? Hash
32
- request.body = data if data.is_a? String
33
- yield(request, http) if block_given?
34
- http.request(request)
26
+ def request(method_class, path='/', data=nil)
27
+ begin
28
+ WebClient.logger.debug "[WebClient] #{method_class.to_s.demodulize.upcase} Url: http://#{@http.address}#{(@http.port != 80) ? ":#{@http.port}" : ''}#{path} | Params: #{data}"
29
+ request = method_class.new(path)
30
+ request.set_form_data(data) if data.is_a? Hash
31
+ request.body = data if data.is_a? String
32
+ yield(request, http) if block_given?
33
+ response = http.request(request)
34
+ WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
35
+ response
36
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, HTTPBadResponse, HTTPHeaderSyntaxError, ProtocolError, SocketError, Errno::ECONNREFUSED => e
37
+ WebClient.logger.error "[WebClient] #{e.class}: #{e.message}"
38
+ raise Error, e
39
+ end
35
40
  end
36
41
 
37
42
  end
@@ -0,0 +1,17 @@
1
+ module WebClient
2
+
3
+ class Error < StandardError
4
+ def initialize(inner_error)
5
+ @inner_error = inner_error
6
+ end
7
+
8
+ def type
9
+ @inner_error.class
10
+ end
11
+
12
+ def message
13
+ @inner_error.message
14
+ end
15
+ end
16
+
17
+ end
@@ -7,8 +7,8 @@ module WebClient
7
7
  super(*args)
8
8
  end
9
9
 
10
- def index(&block)
11
- get("/#{resource}.json", &block)
10
+ def index(data={}, &block)
11
+ get("/#{resource}.json", data, &block)
12
12
  end
13
13
 
14
14
  def show(id, &block)
@@ -1,3 +1,3 @@
1
1
  module WebClient
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/web_client.rb CHANGED
@@ -1,7 +1,22 @@
1
1
  require 'net/http'
2
+ require 'active_support/all'
3
+ require 'logger'
4
+ require 'json'
5
+
6
+ module WebClient
7
+ HTTP_METHODS = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete].freeze
8
+
9
+ def self.logger
10
+ @@logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
11
+ end
12
+
13
+ def self.logger=(logger)
14
+ @@logger = logger
15
+ end
16
+
17
+ end
2
18
 
3
19
  require 'web_client/version'
20
+ require 'web_client/error'
4
21
  require 'web_client/base'
5
22
  require 'web_client/resource'
6
-
7
-
data/spec/base_spec.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- HOST = 'www.mock.dev'
4
-
5
3
  describe WebClient::Base do
6
4
 
7
5
  let(:client) { WebClient::Base.new(HOST) }
@@ -74,4 +72,18 @@ describe WebClient::Base do
74
72
 
75
73
  end
76
74
 
75
+ context 'Errors handling' do
76
+
77
+ it 'Invalid host exception' do
78
+ stub_request(:get, /.*/).to_raise(SocketError.new('getaddrinfo: No such host is known.'))
79
+ lambda { client.get }.should raise_error WebClient::Error
80
+ end
81
+
82
+ it 'Timeout exception' do
83
+ stub_request(:get, /.*/).to_timeout
84
+ lambda { client.get }.should raise_error WebClient::Error
85
+ end
86
+
87
+ end
88
+
77
89
  end
@@ -3,42 +3,43 @@ require 'spec_helper'
3
3
 
4
4
  describe WebClient::Resource do
5
5
 
6
- let(:resource) { WebClient::Resource.new(:users, 'localhost', 3000) }
6
+ let(:resource) { WebClient::Resource.new(:users, HOST) }
7
7
 
8
8
  it 'index' do
9
- content = '[{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}]'
10
- stub_request(:get, "http://localhost:3000/users.json").to_return(body: content)
9
+ content = '[{"id":1,"email":"jperez@mail.com","first_name":"Juan","last_name":"Perez","organization":"Test"}]'
10
+ stub_request(:get, "http://#{HOST}/users.json").to_return(body: content)
11
11
 
12
12
  response = resource.index
13
13
 
14
14
  response.should be_a Net::HTTPOK
15
15
  data = JSON.parse(response.body)
16
16
  data.should be_a Array
17
- data.first['id'].should_not be_nil
18
- data.first['first_name'].should_not be_nil
19
- data.first['last_name'].should_not be_nil
20
- data.first['email'].should_not be_nil
21
- data.first['organization'].should_not be_nil
17
+ data.first['id'].should eq 1
18
+ data.first['email'].should eq 'jperez@mail.com'
19
+ data.first['first_name'].should eq 'Juan'
20
+ data.first['last_name'].should eq 'Perez'
21
+ data.first['organization'].should eq 'Test'
22
22
  end
23
23
 
24
24
  it 'show' do
25
- content = '{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}'
26
- stub_request(:get, "http://localhost:3000/users/1.json").to_return(body: content)
25
+ content = '{"id":1,"email":"jperez@mail.com","first_name":"Juan","last_name":"Perez","organization":"Test"}'
26
+ stub_request(:get, "http://#{HOST}/users/1.json").to_return(body: content)
27
27
 
28
28
  response = resource.show(1)
29
29
 
30
30
  response.should be_a Net::HTTPOK
31
31
  data = JSON.parse(response.body)
32
- data['id'].should_not be_nil
32
+ data.should be_a Hash
33
+ data['id'].should eq 1
34
+ data['email'].should eq 'jperez@mail.com'
33
35
  data['first_name'].should eq 'Juan'
34
36
  data['last_name'].should eq 'Perez'
35
- data['email'].should eq 'jperez@mail.com'
36
37
  data['organization'].should eq 'Test'
37
38
  end
38
39
 
39
40
  it 'create' do
40
- content = '{"email":"jperez@mail.com","first_name":"Juan","id":1,"last_name":"Perez","organization":"Test"}'
41
- stub_request(:post, "http://localhost:3000/users.json").to_return(body: content, status: 201)
41
+ content = '{"id":1,"email":"jperez@mail.com","first_name":"Juan","last_name":"Perez","organization":"Test"}'
42
+ stub_request(:post, "http://#{HOST}/users.json").to_return(body: content, status: 201)
42
43
 
43
44
  params = {
44
45
  'user[first_name]' => 'Juan',
@@ -50,15 +51,16 @@ describe WebClient::Resource do
50
51
 
51
52
  response.should be_a Net::HTTPCreated
52
53
  data = JSON.parse(response.body)
53
- data['id'].should_not be_nil
54
+ data.should be_a Hash
55
+ data['id'].should eq 1
56
+ data['email'].should eq 'jperez@mail.com'
54
57
  data['first_name'].should eq 'Juan'
55
58
  data['last_name'].should eq 'Perez'
56
- data['email'].should eq 'jperez@mail.com'
57
59
  data['organization'].should eq 'Test'
58
60
  end
59
61
 
60
62
  it 'update' do
61
- stub_request(:put, "http://localhost:3000/users/1.json").to_return(status: 204)
63
+ stub_request(:put, "http://#{HOST}/users/1.json").to_return(status: 204)
62
64
 
63
65
  params = {
64
66
  'user[first_name]' => 'Juan',
@@ -73,7 +75,7 @@ describe WebClient::Resource do
73
75
  end
74
76
 
75
77
  it 'destroy' do
76
- stub_request(:delete, "http://localhost:3000/users/1.json").to_return(status: 204)
78
+ stub_request(:delete, "http://#{HOST}/users/1.json").to_return(status: 204)
77
79
 
78
80
  response = resource.destroy(1)
79
81
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'webmock/rspec'
2
2
  require 'json'
3
3
  require 'web_client'
4
+
5
+ HOST = 'www.mock.dev'
data/web_client.gemspec CHANGED
@@ -15,6 +15,8 @@ Gem::Specification.new do |s|
15
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
16
  s.require_paths = ["lib"]
17
17
 
18
+ s.add_dependency 'activesupport', '>= 3.0.0'
19
+
18
20
  s.add_development_dependency 'rspec'
19
21
  s.add_development_dependency 'webmock'
20
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000 Z
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &20747148 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *20747148
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rspec
16
- requirement: &20428260 !ruby/object:Gem::Requirement
27
+ requirement: &20746608 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: '0'
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *20428260
35
+ version_requirements: *20746608
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: webmock
27
- requirement: &20427120 !ruby/object:Gem::Requirement
38
+ requirement: &20745984 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,7 +43,7 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *20427120
46
+ version_requirements: *20745984
36
47
  description: Net::HTTP wrapper easy to use
37
48
  email:
38
49
  - gabynaiman@gmail.com
@@ -46,6 +57,7 @@ files:
46
57
  - Rakefile
47
58
  - lib/web_client.rb
48
59
  - lib/web_client/base.rb
60
+ - lib/web_client/error.rb
49
61
  - lib/web_client/resource.rb
50
62
  - lib/web_client/version.rb
51
63
  - spec/base_spec.rb