web_client 0.0.2 → 0.0.3
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/README.md +2 -1
- data/lib/web_client.rb +4 -4
- data/lib/web_client/base.rb +5 -4
- data/lib/web_client/version.rb +1 -1
- data/spec/base_spec.rb +41 -0
- metadata +8 -10
- data/lib/web_client/resource.rb +0 -31
- data/spec/resource_spec.rb +0 -85
data/README.md
CHANGED
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/
|
20
|
-
require 'web_client/error'
|
21
|
-
require 'web_client/base'
|
22
|
-
require 'web_client/resource'
|
22
|
+
require 'web_client/base'
|
data/lib/web_client/base.rb
CHANGED
@@ -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}"
|
data/lib/web_client/version.rb
CHANGED
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.
|
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-
|
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: &
|
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: *
|
24
|
+
version_requirements: *25457700
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
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: *
|
35
|
+
version_requirements: *25412508
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: webmock
|
38
|
-
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: *
|
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
|
data/lib/web_client/resource.rb
DELETED
@@ -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
|
data/spec/resource_spec.rb
DELETED
@@ -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
|