resto 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,8 @@ module Resto
16
16
  include Resto::Request::Uri
17
17
  include Resto::Request::Option
18
18
 
19
- delegate :head, :get, :post, :put, :delete, :to => :@request
19
+ delegate :head, :head!, :get, :get!, :post, :post!,
20
+ :put, :put!, :delete, :delete!, :to => :@request
20
21
 
21
22
  def initialize(request=Resto::Request::Factory)
22
23
  @request_klass = request
@@ -13,40 +13,76 @@ module Resto
13
13
 
14
14
  def initialize(request)
15
15
  @request = request
16
+ @debug_request = false
17
+ end
18
+
19
+ def head!
20
+ @debug_request = true
21
+ head.tap { |response| print_response(response) }
16
22
  end
17
23
 
18
24
  def head
19
25
  http.start do |http|
20
- http.request(Net::HTTP::Head.new(composed_path, composed_headers))
26
+ request = Net::HTTP::Head.new(composed_path, composed_headers)
27
+ print_request(request)
28
+ http.request(request)
21
29
  end
22
30
  end
23
31
 
32
+ def get!
33
+ @debug_request = true
34
+ get.tap { |response| print_response(response) }
35
+ end
36
+
24
37
  def get
25
38
  http.start do |http|
26
- http.request(Net::HTTP::Get.new(composed_path, composed_headers))
39
+ request = Net::HTTP::Get.new(composed_path, composed_headers)
40
+ print_request(request)
41
+ http.request(request)
27
42
  end
28
43
  end
29
44
 
45
+ def post!
46
+ @debug_request = true
47
+ post.tap { |response| print_response(response) }
48
+ end
49
+
30
50
  def post
31
51
  http.start do |http|
32
- http.request(Net::HTTP::Post.new(composed_path, composed_headers),
33
- read_body)
52
+ request = Net::HTTP::Post.new(composed_path, composed_headers)
53
+ print_request(request)
54
+ http.request(request, read_body)
34
55
  end
35
56
  end
36
57
 
58
+ def put!
59
+ @debug_request = true
60
+ put.tap { |response| print_response(response) }
61
+ end
62
+
37
63
  def put
38
64
  http.start do |http|
39
- http.request(Net::HTTP::Put.new(composed_path, composed_headers),
40
- read_body)
65
+ request = Net::HTTP::Put.new(composed_path, composed_headers)
66
+ print_request(request)
67
+ http.request(request, read_body)
41
68
  end
42
69
  end
43
70
 
71
+ def delete!
72
+ @debug_request = true
73
+ delete.tap { |response| print_response(response) }
74
+ end
75
+
44
76
  def delete
45
77
  http.start do |http|
46
- http.request(Net::HTTP::Delete.new(composed_path, composed_headers))
78
+ request = Net::HTTP::Delete.new(composed_path, composed_headers)
79
+ print_request(request)
80
+ http.request(request)
47
81
  end
48
82
  end
49
83
 
84
+ private
85
+
50
86
  def http
51
87
  ::Net::HTTP.new(read_host, read_port).tap do |http|
52
88
 
@@ -61,6 +97,47 @@ module Resto
61
97
  end
62
98
  end
63
99
  end
100
+
101
+ def print_request(request)
102
+ if @debug_request
103
+ url = ["#{request.method} #{scheme}://#{read_host}"]
104
+ url[0] += "#{request.path}:#{read_port}"
105
+ out(title("Request"), url, content(request))
106
+ end
107
+ end
108
+
109
+ def print_response(response)
110
+ if @debug_request
111
+ out(["\n"], title("Response"), content(response))
112
+ end
113
+ end
114
+
115
+ def title(title)
116
+ out = []
117
+ out << "\n==============================="
118
+ out << " #{title.upcase}"
119
+ out << "==============================="
120
+ end
121
+
122
+ def content(result)
123
+ out = []
124
+ result.each_header do |key, value|
125
+ if key == "status"
126
+ out.unshift("header['#{key}'] : #{value}")
127
+ else
128
+ out.push("header['#{key}'] : #{value}")
129
+ end
130
+ end
131
+ out.unshift("\n*** Header ***")
132
+
133
+ body = result.body.to_s
134
+ length = body.length
135
+ out << "\n*** Body (#{length}) ***\n#{body}"
136
+ end
137
+
138
+ def out(*out)
139
+ puts (out.fetch(0, []) + out.fetch(1, []) + out.fetch(2, [])).join("\n")
140
+ end
64
141
  end
65
142
  end
66
143
  end
data/lib/resto/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Resto
4
- VERSION = "0.0.6"
4
+ VERSION = "0.0.7"
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: resto
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.6
5
+ version: 0.0.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Anders T\xC3\xB6rnqvist"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-08 00:00:00 Z
13
+ date: 2011-05-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: yajl-ruby