web_client 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,7 +18,8 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ client = WebClient::Base.new 'localhost', 3000
22
+ client.get '/users'
22
23
 
23
24
  ## Contributing
24
25
 
data/lib/web_client.rb CHANGED
@@ -3,6 +3,9 @@ require 'active_support/all'
3
3
  require 'logger'
4
4
  require 'json'
5
5
 
6
+ require 'web_client/version'
7
+ require 'web_client/error'
8
+
6
9
  module WebClient
7
10
  HTTP_METHODS = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete].freeze
8
11
 
@@ -16,7 +19,4 @@ module WebClient
16
19
 
17
20
  end
18
21
 
19
- require 'web_client/version'
20
- require 'web_client/error'
21
- require 'web_client/base'
22
- require 'web_client/resource'
22
+ require 'web_client/base'
@@ -16,19 +16,20 @@ module WebClient
16
16
  end
17
17
 
18
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)
19
+ define_method http_method.to_s.demodulize.downcase do |path='/', data=nil, headers={}, &block|
20
+ request(http_method, path, data, headers, &block)
21
21
  end
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def request(method_class, path='/', data=nil)
26
+ def request(method_class, path='/', data=nil, headers={})
27
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}"
28
+ WebClient.logger.debug "[WebClient] #{method_class.to_s.demodulize.upcase} Url: http://#{@http.address}#{(@http.port != 80) ? ":#{@http.port}" : ''}#{path} | Params: #{data} | Headers: #{headers}"
29
29
  request = method_class.new(path)
30
30
  request.set_form_data(data) if data.is_a? Hash
31
31
  request.body = data if data.is_a? String
32
+ headers.each { |k, v| request.send("#{k}=", v) }
32
33
  yield(request, http) if block_given?
33
34
  response = http.request(request)
34
35
  WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
@@ -1,3 +1,3 @@
1
1
  module WebClient
2
- VERSION = "0.0.2"
2
+ VERSION = '0.0.3'
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -36,6 +36,47 @@ describe WebClient::Base do
36
36
 
37
37
  end
38
38
 
39
+ context 'Actions with headers' do
40
+
41
+ it 'get' do
42
+ stub_request(:get, "#{HOST}/get_stub").
43
+ with(headers: {content_type: 'application/json'}).
44
+ to_return(body: 'content')
45
+ response = client.get('/get_stub', nil, content_type: 'application/json')
46
+ response.should be_a Net::HTTPOK
47
+ response.body.should eq 'content'
48
+ end
49
+
50
+ it 'post' do
51
+ stub_request(:post, "#{HOST}/post_stub").
52
+ with(headers: {content_type: 'application/json'}).
53
+ with(body: 'form_data')
54
+ response = client.post('/post_stub', 'form_data', content_type: 'application/json')
55
+ response.should be_a Net::HTTPOK
56
+ response.body.should be_nil
57
+ end
58
+
59
+ it 'put' do
60
+ stub_request(:put, "#{HOST}/put_stub").
61
+ with(headers: {content_type: 'application/json'}).
62
+ with(body: 'form_data')
63
+ response = client.put('/put_stub', 'form_data', content_type: 'application/json')
64
+ response.should be_a Net::HTTPOK
65
+ response.body.should be_nil
66
+ end
67
+
68
+ it 'delete' do
69
+ stub_request(:delete, "#{HOST}/delete_stub").
70
+ with(headers: {content_type: 'application/json'}).
71
+ with(body: 'form_data')
72
+ response = client.delete('/delete_stub', 'form_data', content_type: 'application/json')
73
+ response.should be_a Net::HTTPOK
74
+ response.body.should be_nil
75
+ end
76
+
77
+
78
+ end
79
+
39
80
  context 'Actions with a block' do
40
81
 
41
82
  it 'get' do
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &20747148 !ruby/object:Gem::Requirement
16
+ requirement: &25457700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20747148
24
+ version_requirements: *25457700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &20746608 !ruby/object:Gem::Requirement
27
+ requirement: &25412508 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20746608
35
+ version_requirements: *25412508
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: webmock
38
- requirement: &20745984 !ruby/object:Gem::Requirement
38
+ requirement: &25389876 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20745984
46
+ version_requirements: *25389876
47
47
  description: Net::HTTP wrapper easy to use
48
48
  email:
49
49
  - gabynaiman@gmail.com
@@ -58,10 +58,8 @@ files:
58
58
  - lib/web_client.rb
59
59
  - lib/web_client/base.rb
60
60
  - lib/web_client/error.rb
61
- - lib/web_client/resource.rb
62
61
  - lib/web_client/version.rb
63
62
  - spec/base_spec.rb
64
- - spec/resource_spec.rb
65
63
  - spec/spec_helper.rb
66
64
  - web_client.gemspec
67
65
  homepage: https://github.com/gabynaiman/web_client
@@ -1,31 +0,0 @@
1
- module WebClient
2
- class Resource < Base
3
- attr_reader :resource
4
-
5
- def initialize(resource, *args)
6
- @resource = resource
7
- super(*args)
8
- end
9
-
10
- def index(data={}, &block)
11
- get("/#{resource}.json", data, &block)
12
- end
13
-
14
- def show(id, &block)
15
- get("/#{resource}/#{id}.json", &block)
16
- end
17
-
18
- def create(data, &block)
19
- post("/#{resource}.json", data, &block)
20
- end
21
-
22
- def update(id, data, &block)
23
- put("/#{resource}/#{id}.json", data, &block)
24
- end
25
-
26
- def destroy(id, &block)
27
- delete("/#{resource}/#{id}.json", &block)
28
- end
29
-
30
- end
31
- end
@@ -1,85 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe WebClient::Resource do
5
-
6
- let(:resource) { WebClient::Resource.new(:users, HOST) }
7
-
8
- it 'index' do
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
-
12
- response = resource.index
13
-
14
- response.should be_a Net::HTTPOK
15
- data = JSON.parse(response.body)
16
- data.should be_a Array
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
- end
23
-
24
- it 'show' do
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
-
28
- response = resource.show(1)
29
-
30
- response.should be_a Net::HTTPOK
31
- data = JSON.parse(response.body)
32
- data.should be_a Hash
33
- data['id'].should eq 1
34
- data['email'].should eq 'jperez@mail.com'
35
- data['first_name'].should eq 'Juan'
36
- data['last_name'].should eq 'Perez'
37
- data['organization'].should eq 'Test'
38
- end
39
-
40
- it 'create' do
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)
43
-
44
- params = {
45
- 'user[first_name]' => 'Juan',
46
- 'user[last_name]' => 'Perez',
47
- 'user[email]' => 'jperez@mail.com',
48
- 'user[organization]' => 'Test'
49
- }
50
- response = resource.create(params)
51
-
52
- response.should be_a Net::HTTPCreated
53
- data = JSON.parse(response.body)
54
- data.should be_a Hash
55
- data['id'].should eq 1
56
- data['email'].should eq 'jperez@mail.com'
57
- data['first_name'].should eq 'Juan'
58
- data['last_name'].should eq 'Perez'
59
- data['organization'].should eq 'Test'
60
- end
61
-
62
- it 'update' do
63
- stub_request(:put, "http://#{HOST}/users/1.json").to_return(status: 204)
64
-
65
- params = {
66
- 'user[first_name]' => 'Juan',
67
- 'user[last_name]' => 'Perez',
68
- 'user[email]' => 'jperez@mail.com',
69
- 'user[organization]' => 'Test'
70
- }
71
- response = resource.update(1, params)
72
-
73
- response.should be_a Net::HTTPNoContent
74
- response.body.should be_nil
75
- end
76
-
77
- it 'destroy' do
78
- stub_request(:delete, "http://#{HOST}/users/1.json").to_return(status: 204)
79
-
80
- response = resource.destroy(1)
81
-
82
- response.should be_a Net::HTTPNoContent
83
- response.body.should be_nil
84
- end
85
- end