httprb 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/httprb/dsl.rb +18 -32
- data/lib/httprb/request.rb +13 -2
- data/lib/httprb/version.rb +1 -1
- data/tests/test_dsl.rb +27 -33
- metadata +2 -2
data/lib/httprb/dsl.rb
CHANGED
@@ -6,18 +6,19 @@
|
|
6
6
|
# note that each of the following methods accepts a
|
7
7
|
# block- this is the beauty of the whole thing.
|
8
8
|
|
9
|
-
# if you're wondering about the code duplication, read:
|
10
|
-
#
|
11
|
-
# http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html
|
12
|
-
#
|
13
|
-
# implicit invocation of blocks is just faster. so we
|
14
|
-
# duplicate code. :(
|
15
|
-
|
16
9
|
require 'httprb/request'
|
17
10
|
require 'httprb/http_cache'
|
18
11
|
|
19
12
|
module HTTPrb
|
20
13
|
public
|
14
|
+
def HTTPrb.req(type, url, options = {}, &block)
|
15
|
+
options[:type] = type.upcase
|
16
|
+
req = Request.new(url, options)
|
17
|
+
#yield(req) if block_given?
|
18
|
+
req.evaluate(&block) if block_given?
|
19
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
20
|
+
end
|
21
|
+
|
21
22
|
# get request
|
22
23
|
#
|
23
24
|
# * options are not generally used; HTTPrb::Request
|
@@ -25,11 +26,8 @@ module HTTPrb
|
|
25
26
|
# handling.
|
26
27
|
# * accepts a block, handing off the HTTPrb::Request
|
27
28
|
# object to it, if provided.
|
28
|
-
def HTTPrb.get(url, options = {})
|
29
|
-
|
30
|
-
req = Request.new(url, options)
|
31
|
-
yield(req) if block_given?
|
32
|
-
HTTPrb::HTTPCache.instance.make_request(req)
|
29
|
+
def HTTPrb.get(url, options = {}, &block)
|
30
|
+
HTTPrb.req('GET', url, options, &block)
|
33
31
|
end
|
34
32
|
|
35
33
|
# head request
|
@@ -39,11 +37,8 @@ module HTTPrb
|
|
39
37
|
# handling.
|
40
38
|
# * accepts a block, handing off the HTTPrb::Request
|
41
39
|
# object to it, if provided.
|
42
|
-
def HTTPrb.head(url, options = {})
|
43
|
-
|
44
|
-
req = Request.new(url, options)
|
45
|
-
yield(req) if block_given?
|
46
|
-
HTTPrb::HTTPCache.instance.make_request(req)
|
40
|
+
def HTTPrb.head(url, options = {}, &block)
|
41
|
+
HTTPrb.req('HEAD', url, options, &block)
|
47
42
|
end
|
48
43
|
|
49
44
|
# post request
|
@@ -53,11 +48,8 @@ module HTTPrb
|
|
53
48
|
# handling.
|
54
49
|
# * accepts a block, handing off the HTTPrb::Request
|
55
50
|
# object to it, if provided.
|
56
|
-
def HTTPrb.post(url, options = {})
|
57
|
-
|
58
|
-
req = Request.new(url, options)
|
59
|
-
yield(req) if block_given?
|
60
|
-
HTTPrb::HTTPCache.instance.make_request(req)
|
51
|
+
def HTTPrb.post(url, options = {}, &block)
|
52
|
+
HTTPrb.req('POST', url, options, &block)
|
61
53
|
end
|
62
54
|
|
63
55
|
# put request
|
@@ -67,11 +59,8 @@ module HTTPrb
|
|
67
59
|
# handling.
|
68
60
|
# * accepts a block, handing off the HTTPrb::Request
|
69
61
|
# object to it, if provided.
|
70
|
-
def HTTPrb.put(url, options = {})
|
71
|
-
|
72
|
-
req = Request.new(url, options)
|
73
|
-
yield(req) if block_given?
|
74
|
-
HTTPrb::HTTPCache.instance.make_request(req)
|
62
|
+
def HTTPrb.put(url, options = {}, &block)
|
63
|
+
HTTPrb.req('PUT', url, options, &block)
|
75
64
|
end
|
76
65
|
|
77
66
|
# delete request
|
@@ -81,11 +70,8 @@ module HTTPrb
|
|
81
70
|
# handling.
|
82
71
|
# * accepts a block, handing off the HTTPrb::Request
|
83
72
|
# object to it, if provided.
|
84
|
-
def HTTPrb.delete(url, options = {})
|
85
|
-
|
86
|
-
req = Request.new(url, options)
|
87
|
-
yield(req) if block_given?
|
88
|
-
HTTPrb::HTTPCache.instance.make_request(req)
|
73
|
+
def HTTPrb.delete(url, options = {}, &block)
|
74
|
+
HTTPrb.req('DELETE', url, options, &block)
|
89
75
|
end
|
90
76
|
|
91
77
|
end
|
data/lib/httprb/request.rb
CHANGED
@@ -145,15 +145,26 @@ class Request
|
|
145
145
|
return http_req
|
146
146
|
end
|
147
147
|
|
148
|
+
def evaluate(&block)
|
149
|
+
@self_before_eval = eval "self", block.binding
|
150
|
+
r = instance_eval &block
|
151
|
+
@self_before_eval = nil # clear our state
|
152
|
+
r
|
153
|
+
end
|
154
|
+
|
148
155
|
# yep, you got it. we're messing with your mind
|
149
156
|
# here. nothing to see here, move along...
|
150
|
-
def method_missing(method)
|
157
|
+
def method_missing(method, *args, &block)
|
151
158
|
if method == :"ssl?"
|
152
159
|
return @ssl
|
153
160
|
elsif @options.keys.include?(method)
|
154
161
|
return @options[method]
|
155
162
|
end
|
156
|
-
|
163
|
+
if @self_before_eval
|
164
|
+
@self_before_eval.send method, *args, &block
|
165
|
+
else
|
166
|
+
super
|
167
|
+
end
|
157
168
|
end
|
158
169
|
end
|
159
170
|
|
data/lib/httprb/version.rb
CHANGED
data/tests/test_dsl.rb
CHANGED
@@ -21,10 +21,9 @@ class TestDSL < Test::Unit::TestCase
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_get_block
|
24
|
-
res = get 'http://google.com' do
|
25
|
-
|
26
|
-
assert
|
27
|
-
assert req.uri.host = 'mozy.com'
|
24
|
+
res = get 'http://google.com' do
|
25
|
+
assert type == 'GET'
|
26
|
+
assert uri.host = 'mozy.com'
|
28
27
|
end
|
29
28
|
assert_not_nil res
|
30
29
|
assert res.code =~ /2[0-9].|3[0-9]./
|
@@ -36,10 +35,9 @@ class TestDSL < Test::Unit::TestCase
|
|
36
35
|
end
|
37
36
|
|
38
37
|
def test_get_ssl_block
|
39
|
-
res = get 'https://mozy.com/' do
|
40
|
-
|
41
|
-
assert
|
42
|
-
assert req.uri.host = 'mozy.com'
|
38
|
+
res = get 'https://mozy.com/' do
|
39
|
+
assert type == 'GET'
|
40
|
+
assert uri.host = 'mozy.com'
|
43
41
|
end
|
44
42
|
assert res.code =~ /2[0-9].|3[0-9]./
|
45
43
|
end
|
@@ -50,10 +48,9 @@ class TestDSL < Test::Unit::TestCase
|
|
50
48
|
end
|
51
49
|
|
52
50
|
def test_head_block
|
53
|
-
res = head 'http://google.com' do
|
54
|
-
|
55
|
-
assert
|
56
|
-
assert req.uri.host = 'mozy.com'
|
51
|
+
res = head 'http://google.com' do
|
52
|
+
assert type == 'HEAD'
|
53
|
+
assert uri.host = 'mozy.com'
|
57
54
|
end
|
58
55
|
assert_not_nil res
|
59
56
|
assert res.code =~ /2[0-9].|3[0-9]./
|
@@ -65,10 +62,9 @@ class TestDSL < Test::Unit::TestCase
|
|
65
62
|
end
|
66
63
|
|
67
64
|
def test_put_block
|
68
|
-
res = put 'http://google.com' do
|
69
|
-
|
70
|
-
assert
|
71
|
-
assert req.uri.host = 'mozy.com'
|
65
|
+
res = put 'http://google.com' do
|
66
|
+
assert type == 'PUT'
|
67
|
+
assert uri.host = 'mozy.com'
|
72
68
|
end
|
73
69
|
assert_not_nil res
|
74
70
|
assert res.code =~ /2[0-9].|3[0-9]./
|
@@ -80,10 +76,9 @@ class TestDSL < Test::Unit::TestCase
|
|
80
76
|
end
|
81
77
|
|
82
78
|
def test_post_block
|
83
|
-
res = post 'http://google.com' do
|
84
|
-
|
85
|
-
assert
|
86
|
-
assert req.uri.host = 'mozy.com'
|
79
|
+
res = post 'http://google.com' do
|
80
|
+
assert type == 'POST'
|
81
|
+
assert uri.host = 'mozy.com'
|
87
82
|
end
|
88
83
|
assert_not_nil res
|
89
84
|
assert res.code =~ /2[0-9].|3[0-9]./
|
@@ -96,29 +91,28 @@ class TestDSL < Test::Unit::TestCase
|
|
96
91
|
|
97
92
|
def test_delete_block
|
98
93
|
res = delete 'http://google.com' do |req|
|
99
|
-
|
100
|
-
assert
|
101
|
-
assert req.uri.host = 'mozy.com'
|
94
|
+
assert type == 'DELETE'
|
95
|
+
assert uri.host = 'mozy.com'
|
102
96
|
end
|
103
97
|
assert_not_nil res
|
104
98
|
assert res.code =~ /2[0-9].|3[0-9]./
|
105
99
|
end
|
106
100
|
|
107
101
|
def test_params
|
108
|
-
res = get 'http://mozy.com' do
|
109
|
-
assert
|
110
|
-
assert
|
111
|
-
assert
|
112
|
-
assert
|
113
|
-
assert
|
102
|
+
res = get 'http://mozy.com' do
|
103
|
+
assert parameter "key", "value"
|
104
|
+
assert parameter "key2", "value"
|
105
|
+
assert parameters.include?("key")
|
106
|
+
assert parameters.include?("key2")
|
107
|
+
assert http_request.path.include?("key=value")
|
114
108
|
end
|
115
109
|
end
|
116
110
|
|
117
111
|
def test_headers
|
118
|
-
res = get 'http://mozy.com' do
|
119
|
-
assert
|
120
|
-
assert
|
121
|
-
assert
|
112
|
+
res = get 'http://mozy.com' do
|
113
|
+
assert header "x-key", "value"
|
114
|
+
assert headers["x-key"] == "value"
|
115
|
+
assert http_request["x-key"] == "value"
|
122
116
|
end
|
123
117
|
end
|
124
118
|
end
|