httprb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- options[:type] = 'GET'
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
- options[:type] = 'HEAD'
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
- options[:type] = 'POST'
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
- options[:type] = 'PUT'
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
- options[:type] = 'DELETE'
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
@@ -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
- super
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
 
@@ -1,6 +1,6 @@
1
1
  module HTTPrb
2
2
  # HTTPrb version
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -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 |req|
25
- assert_not_nil req
26
- assert req.options[:type] == 'GET'
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 |req|
40
- assert_not_nil req
41
- assert req.options[:type] == 'GET'
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 |req|
54
- assert_not_nil req
55
- assert req.options[:type] == 'HEAD'
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 |req|
69
- assert_not_nil req
70
- assert req.options[:type] == 'PUT'
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 |req|
84
- assert_not_nil req
85
- assert req.options[:type] == 'POST'
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
- assert_not_nil req
100
- assert req.options[:type] == 'DELETE'
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 |req|
109
- assert req.parameter "key", "value"
110
- assert req.parameter "key2", "value"
111
- assert req.parameters.include?("key")
112
- assert req.parameters.include?("key2")
113
- assert req.http_request.path.include?("key=value")
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 |req|
119
- assert req.header "x-key", "value"
120
- assert req.headers["x-key"] == "value"
121
- assert req.http_request["x-key"] == "value"
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - thomas metge