ramaze 0.0.7 → 0.0.8

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.
Files changed (193) hide show
  1. data/Rakefile +52 -19
  2. data/bin/ramaze +19 -6
  3. data/doc/CHANGELOG +33 -0
  4. data/doc/COPYING +1 -1
  5. data/doc/FAQ +92 -0
  6. data/doc/GPL +340 -0
  7. data/doc/INSTALL +34 -0
  8. data/doc/ProjectInfo +53 -0
  9. data/doc/README +187 -110
  10. data/doc/readme_chunks/appendix.txt +13 -0
  11. data/doc/readme_chunks/examples.txt +38 -0
  12. data/doc/readme_chunks/features.txt +82 -0
  13. data/doc/readme_chunks/getting_help.txt +5 -0
  14. data/doc/readme_chunks/getting_started.txt +18 -0
  15. data/doc/readme_chunks/installing.txt +41 -0
  16. data/doc/readme_chunks/introduction.txt +18 -0
  17. data/doc/readme_chunks/principles.txt +41 -0
  18. data/doc/readme_chunks/thanks.txt +59 -0
  19. data/doc/tutorial/todolist.txt +546 -0
  20. data/examples/blog/main.rb +1 -1
  21. data/examples/blog/src/controller.rb +13 -11
  22. data/examples/blog/src/element.rb +11 -6
  23. data/examples/blog/src/model.rb +8 -23
  24. data/examples/blog/template/edit.xhtml +3 -1
  25. data/examples/blog/template/index.xhtml +4 -4
  26. data/examples/caching.rb +4 -4
  27. data/examples/element.rb +10 -7
  28. data/examples/hello.rb +3 -4
  29. data/examples/simple.rb +5 -3
  30. data/examples/templates/template/external.amrita +19 -0
  31. data/examples/templates/template/{external.rmze → external.zmr} +2 -2
  32. data/examples/templates/template_amrita2.rb +48 -0
  33. data/examples/templates/template_erubis.rb +5 -2
  34. data/examples/templates/{template_ramaze.rb → template_ezamar.rb} +13 -7
  35. data/examples/templates/template_haml.rb +4 -1
  36. data/examples/templates/template_liquid.rb +2 -1
  37. data/examples/templates/template_markaby.rb +2 -1
  38. data/examples/todolist/conf/benchmark.yaml +35 -0
  39. data/examples/todolist/conf/debug.yaml +34 -0
  40. data/examples/todolist/conf/live.yaml +33 -0
  41. data/examples/todolist/conf/silent.yaml +31 -0
  42. data/examples/todolist/conf/stage.yaml +33 -0
  43. data/examples/todolist/main.rb +18 -0
  44. data/examples/todolist/public/404.jpg +0 -0
  45. data/examples/todolist/public/css/coderay.css +105 -0
  46. data/examples/todolist/public/css/ramaze_error.css +42 -0
  47. data/{lib/proto → examples/todolist}/public/error.xhtml +0 -0
  48. data/examples/todolist/public/favicon.ico +0 -0
  49. data/examples/todolist/public/js/jquery.js +1923 -0
  50. data/examples/todolist/public/ramaze.png +0 -0
  51. data/examples/todolist/src/controller/main.rb +56 -0
  52. data/examples/todolist/src/element/page.rb +26 -0
  53. data/examples/todolist/src/model.rb +14 -0
  54. data/examples/todolist/template/index.xhtml +17 -0
  55. data/examples/todolist/template/new.xhtml +7 -0
  56. data/examples/todolist/todolist.db +9 -0
  57. data/examples/whywiki/main.rb +3 -8
  58. data/examples/whywiki/template/show.xhtml +4 -0
  59. data/lib/proto/public/error.zmr +77 -0
  60. data/lib/proto/src/controller/main.rb +2 -1
  61. data/lib/proto/src/element/page.rb +2 -1
  62. data/lib/proto/src/model.rb +3 -2
  63. data/lib/ramaze.rb +7 -9
  64. data/lib/ramaze/adapter.rb +51 -0
  65. data/lib/ramaze/adapter/cgi.rb +23 -0
  66. data/lib/ramaze/adapter/fcgi.rb +22 -0
  67. data/lib/ramaze/adapter/mongrel.rb +7 -86
  68. data/lib/ramaze/adapter/webrick.rb +14 -133
  69. data/lib/ramaze/cache/memcached.rb +6 -0
  70. data/lib/ramaze/cache/yaml_store.rb +3 -1
  71. data/lib/ramaze/controller.rb +292 -2
  72. data/lib/ramaze/dispatcher.rb +85 -213
  73. data/lib/ramaze/error.rb +10 -0
  74. data/lib/ramaze/global.rb +8 -0
  75. data/lib/ramaze/helper/aspect.rb +30 -7
  76. data/lib/ramaze/helper/auth.rb +16 -9
  77. data/lib/ramaze/helper/cache.rb +40 -35
  78. data/lib/ramaze/helper/feed.rb +1 -1
  79. data/lib/ramaze/helper/flash.rb +34 -0
  80. data/lib/ramaze/helper/link.rb +8 -2
  81. data/lib/ramaze/helper/openid.rb +63 -0
  82. data/lib/ramaze/helper/redirect.rb +12 -11
  83. data/lib/ramaze/helper/stack.rb +5 -7
  84. data/lib/ramaze/inform.rb +12 -1
  85. data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
  86. data/lib/ramaze/snippets/kernel/{self_method.rb → method.rb} +3 -18
  87. data/lib/ramaze/snippets/kernel/{rescue_require.rb → pretty_inspect.rb} +7 -6
  88. data/lib/ramaze/snippets/method/name.rb +22 -0
  89. data/lib/ramaze/snippets/{kernel → ramaze}/autoreload.rb +0 -0
  90. data/lib/ramaze/snippets/ramaze/caller_info.rb +14 -0
  91. data/lib/ramaze/snippets/{kernel → ramaze}/caller_lines.rb +3 -10
  92. data/lib/ramaze/snippets/rdoc/usage_no_exit.rb +49 -23
  93. data/lib/ramaze/snippets/string/DIVIDE.rb +0 -1
  94. data/lib/ramaze/store/default.rb +58 -2
  95. data/lib/ramaze/store/yaml.rb +161 -0
  96. data/lib/ramaze/template.rb +27 -86
  97. data/lib/ramaze/template/amrita2.rb +14 -19
  98. data/lib/ramaze/template/erubis.rb +15 -38
  99. data/lib/ramaze/template/ezamar.rb +100 -0
  100. data/lib/ramaze/template/ezamar/element.rb +166 -0
  101. data/lib/ramaze/template/ezamar/engine.rb +124 -0
  102. data/lib/ramaze/template/ezamar/morpher.rb +155 -0
  103. data/lib/ramaze/template/haml.rb +16 -43
  104. data/lib/ramaze/template/liquid.rb +11 -51
  105. data/lib/ramaze/template/markaby.rb +44 -42
  106. data/lib/ramaze/tool/mime.rb +18 -0
  107. data/lib/ramaze/tool/mime_types.yaml +615 -0
  108. data/lib/ramaze/trinity/request.rb +20 -196
  109. data/lib/ramaze/trinity/response.rb +4 -33
  110. data/lib/ramaze/trinity/session.rb +150 -72
  111. data/lib/ramaze/version.rb +1 -1
  112. data/spec/adapter_spec.rb +20 -0
  113. data/spec/public/favicon.ico +0 -0
  114. data/spec/public/ramaze.png +0 -0
  115. data/spec/public/test_download.css +141 -0
  116. data/spec/{tc_request.rb → request_tc_helper.rb} +45 -21
  117. data/spec/spec_all.rb +77 -34
  118. data/spec/spec_helper.rb +8 -157
  119. data/spec/spec_helper_context.rb +72 -0
  120. data/spec/spec_helper_requester.rb +52 -0
  121. data/spec/spec_helper_simple_http.rb +433 -0
  122. data/spec/tc_adapter_mongrel.rb +3 -15
  123. data/spec/tc_adapter_webrick.rb +4 -14
  124. data/spec/tc_cache.rb +3 -5
  125. data/spec/tc_controller.rb +22 -12
  126. data/spec/tc_dependencies.rb +13 -0
  127. data/spec/tc_element.rb +8 -7
  128. data/spec/tc_error.rb +13 -7
  129. data/spec/tc_global.rb +16 -18
  130. data/spec/tc_helper_aspect.rb +2 -4
  131. data/spec/tc_helper_auth.rb +15 -14
  132. data/spec/tc_helper_cache.rb +5 -7
  133. data/spec/tc_helper_feed.rb +0 -2
  134. data/spec/tc_helper_flash.rb +103 -0
  135. data/spec/tc_helper_form.rb +4 -6
  136. data/spec/tc_helper_link.rb +1 -3
  137. data/spec/tc_helper_redirect.rb +23 -8
  138. data/spec/tc_helper_stack.rb +31 -15
  139. data/spec/tc_morpher.rb +1 -3
  140. data/spec/tc_params.rb +48 -7
  141. data/spec/tc_request_mongrel.rb +9 -0
  142. data/spec/tc_request_webrick.rb +5 -0
  143. data/spec/tc_session.rb +41 -25
  144. data/spec/tc_store.rb +55 -6
  145. data/spec/tc_store_yaml.rb +71 -0
  146. data/spec/tc_template_amrita2.rb +3 -3
  147. data/spec/tc_template_erubis.rb +2 -3
  148. data/spec/{tc_template_ramaze.rb → tc_template_ezamar.rb} +15 -5
  149. data/spec/tc_template_haml.rb +4 -3
  150. data/spec/tc_template_liquid.rb +3 -4
  151. data/spec/tc_template_markaby.rb +4 -6
  152. data/spec/tc_tidy.rb +1 -3
  153. data/spec/template/amrita2/{data.html → data.amrita} +0 -0
  154. data/spec/template/amrita2/{index.html → index.amrita} +0 -0
  155. data/spec/template/amrita2/{sum.html → sum.amrita} +0 -0
  156. data/spec/template/ezamar/another/long/action.zmr +1 -0
  157. data/spec/template/ezamar/combined.zmr +1 -0
  158. data/spec/template/{ramaze/file_only.rmze → ezamar/file_only.zmr} +0 -0
  159. data/spec/template/{ramaze/index.rmze → ezamar/index.zmr} +0 -0
  160. data/spec/template/{ramaze/nested.rmze → ezamar/nested.zmr} +0 -0
  161. data/spec/template/ezamar/some__long__action.zmr +1 -0
  162. data/spec/template/{ramaze/sum.rmze → ezamar/sum.zmr} +0 -0
  163. metadata +181 -123
  164. data/doc/allison/LICENSE +0 -184
  165. data/doc/allison/README +0 -37
  166. data/doc/allison/allison.css +0 -300
  167. data/doc/allison/allison.gif +0 -0
  168. data/doc/allison/allison.js +0 -307
  169. data/doc/allison/allison.rb +0 -287
  170. data/doc/allison/cache/BODY +0 -588
  171. data/doc/allison/cache/CLASS_INDEX +0 -4
  172. data/doc/allison/cache/CLASS_PAGE +0 -1
  173. data/doc/allison/cache/FILE_INDEX +0 -4
  174. data/doc/allison/cache/FILE_PAGE +0 -1
  175. data/doc/allison/cache/FONTS +0 -1
  176. data/doc/allison/cache/FR_INDEX_BODY +0 -1
  177. data/doc/allison/cache/IMGPATH +0 -1
  178. data/doc/allison/cache/INDEX +0 -1
  179. data/doc/allison/cache/JAVASCRIPT +0 -307
  180. data/doc/allison/cache/METHOD_INDEX +0 -4
  181. data/doc/allison/cache/METHOD_LIST +0 -1
  182. data/doc/allison/cache/SRC_PAGE +0 -1
  183. data/doc/allison/cache/STYLE +0 -322
  184. data/doc/allison/cache/URL +0 -1
  185. data/doc/changes.txt +0 -2021
  186. data/doc/changes.xml +0 -2024
  187. data/lib/ramaze/snippets/kernel/silently.rb +0 -13
  188. data/lib/ramaze/snippets/thread/deadQUESTIONMARK.rb +0 -11
  189. data/lib/ramaze/template/haml/actionview_stub.rb +0 -20
  190. data/lib/ramaze/template/ramaze.rb +0 -177
  191. data/lib/ramaze/template/ramaze/element.rb +0 -166
  192. data/lib/ramaze/template/ramaze/morpher.rb +0 -156
  193. data/spec/tc_test.rb +0 -17
@@ -0,0 +1,72 @@
1
+ class Context
2
+ attr_reader :cookie
3
+
4
+ def initialize(url = '/', base = '/', &block)
5
+ @base = base
6
+ @history = []
7
+ @http = SimpleHttp.new(url2uri(url))
8
+
9
+ get url
10
+
11
+ story(&block) if block_given?
12
+ end
13
+
14
+ def story(&block)
15
+ instance_eval(&block) if block_given?
16
+ end
17
+
18
+ def get url = '/', hash = {}
19
+ request(:get, url, hash)
20
+ end
21
+
22
+ def post url = '/', hash = {}
23
+ request(:post, url, hash)
24
+ end
25
+
26
+ def erequest method, url, hash = {}
27
+ response = request(method, url, hash)
28
+ eval(response)
29
+ rescue Object => ex
30
+ p :response => response
31
+ ex.message
32
+ end
33
+
34
+ def epost url = '/', hash = {}
35
+ erequest(:post, url, hash)
36
+ end
37
+
38
+ def eget url = '/', hash = {}
39
+ erequest(:get, url, hash)
40
+ end
41
+
42
+ def request method, url, hash = {}
43
+ @http.uri = url2uri(url)
44
+ @http.request_headers['referer'] = @history.last.path rescue '/'
45
+
46
+ if method == :get and not hash.empty?
47
+ @http.uri.query = hash.inject([]){|s,(k,v)| s << "#{k}=#{v}"}.join('&')
48
+ hash.clear
49
+ end
50
+
51
+ puts "#{method.to_s.upcase} => #{@http.uri}"
52
+ response = @http.send(method, hash).strip
53
+ @history << @http.uri
54
+ get_cookie
55
+
56
+ response
57
+ end
58
+
59
+ def get_cookie
60
+ @cookie = @http.response_headers['set-cookie']
61
+ @http.request_headers['cookie'] = @cookie
62
+ end
63
+
64
+ def url2uri url
65
+ uri = URI.parse(url)
66
+ uri.scheme = 'http'
67
+ uri.host = 'localhost'
68
+ uri.port = Ramaze::Global.port
69
+ uri.path = "/#{@base}/#{url}".squeeze('/')
70
+ uri
71
+ end
72
+ end
@@ -0,0 +1,52 @@
1
+ module Requester
2
+ def get url, hash = {}
3
+ http = SimpleHttp.new(url2uri(url))
4
+ p :GET => http.uri
5
+ response = http.get(hash).strip
6
+ end
7
+
8
+ def get url = '/', hash = {}
9
+ request(:get, url, hash)
10
+ end
11
+
12
+ def post url = '/', hash = {}
13
+ request(:post, url, hash)
14
+ end
15
+
16
+ def request method, url, hash = {}
17
+ http = SimpleHttp.new(url2uri(url))
18
+ if method == :get and not hash.empty?
19
+ http.uri.query = hash.inject([]){|s,(k,v)| s << "#{k}=#{v}"}.join('&')
20
+ hash = {}
21
+ end
22
+
23
+ puts "#{method.to_s.upcase} => #{http.uri}"
24
+ http.send(method, hash).strip
25
+ end
26
+
27
+ def epost url = '/', hash = {}
28
+ erequest(:post, url, hash)
29
+ end
30
+
31
+ def eget url = '/', hash = {}
32
+ erequest(:get, url, hash)
33
+ end
34
+
35
+ def erequest method, url, hash = {}
36
+ response = request(method, url, hash)
37
+ eval(response)
38
+ rescue Object => ex
39
+ p :response => response
40
+ ex.message
41
+ end
42
+
43
+ def url2uri url
44
+ uri = URI.parse(url)
45
+ #p uri.methods.sort.grep(/=/)
46
+ uri.scheme = 'http'
47
+ uri.host = 'localhost'
48
+ uri.port = Ramaze::Global.port
49
+ uri.path = "/#{url}".squeeze('/')
50
+ uri
51
+ end
52
+ end
@@ -0,0 +1,433 @@
1
+ =begin
2
+ This package is copyrighted free software by Tim Becker <tim@kuriositaet.de>.
3
+ You can redistribute it and/or modify it under either the terms of the GPL
4
+ (see COPYING.txt file), or the conditions below:
5
+
6
+ 1. You may make and give away verbatim copies of the source form of the
7
+ software without restriction, provided that you duplicate all of the
8
+ original copyright notices and associated disclaimers.
9
+
10
+ 2. You may modify your copy of the software in any way, provided that
11
+ you do at least ONE of the following:
12
+
13
+ a) place your modifications in the Public Domain or otherwise
14
+ make them Freely Available, such as by posting said
15
+ modifications to Usenet or an equivalent medium, or by allowing
16
+ the author to include your modifications in the software.
17
+
18
+ b) use the modified software only within your corporation or
19
+ organization.
20
+
21
+ c) rename any non-standard executables so the names do not conflict
22
+ with standard executables, which must also be provided.
23
+
24
+ d) make other distribution arrangements with the author.
25
+
26
+ 3. You may distribute the software in object code or executable
27
+ form, provided that you do at least ONE of the following:
28
+
29
+ a) distribute the executables and library files of the software,
30
+ together with instructions (in the manual page or equivalent)
31
+ on where to get the original distribution.
32
+
33
+ b) accompany the distribution with the machine-readable source of
34
+ the software.
35
+
36
+ c) give non-standard executables non-standard names, with
37
+ instructions on where to get the original software distribution.
38
+
39
+ d) make other distribution arrangements with the author.
40
+
41
+ 4. You may modify and include the part of the software into any other
42
+ software (possibly commercial). But some files in the distribution
43
+ are not written by the author, so that they are not under this terms.
44
+
45
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
46
+ files under the ./missing directory. See each file for the copying
47
+ condition.
48
+
49
+ 5. The scripts and library files supplied as input to or produced as
50
+ output from the software do not automatically fall under the
51
+ copyright of the software, but belong to whomever generated them,
52
+ and may be sold commercially, and may be aggregated with this
53
+ software.
54
+
55
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
56
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
57
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58
+ PURPOSE.
59
+ =end
60
+
61
+ require 'net/http'
62
+ require 'net/https'
63
+ require 'uri'
64
+ require 'cgi'
65
+ require 'base64'
66
+
67
+
68
+ # Wrapper around ruby's standard net/http classes. Currently, only GET
69
+ # and POST https methods are supported. `SimpleHttp` provides class
70
+ # methods `get` and `post` to handle basic functionality. In case more
71
+ # complicated requests need to be made or default settings need to be
72
+ # overriden, it's possible to instantiate `SimpleHttp` and use instance
73
+ # methods `get` and `put`.
74
+ #
75
+ #
76
+ # Features:
77
+ #
78
+ # * Handles Redirects automatically
79
+ # * Proxy used transparently if http_proxy environment variable is
80
+ # set.
81
+ # * SSL handled automatically
82
+ # * fault tolerant uri, e.g. all of these would work:
83
+ # "www.example.com", "www.example.com/", "http://www.example.com"
84
+ #
85
+ # Some usage examples:
86
+ # # plain GET (using class methods)
87
+ # SimpleHttp.get "www.example.com"
88
+ #
89
+ # # POST using the instance methods
90
+ # uri = URI.parse "https://www.example.com/index.html"
91
+ # sh = SimpleHttp uri
92
+ # sh.set_proxy "my.proxy", "8080"
93
+ # sh.post {"query" => "query_data"}
94
+ #
95
+ # # POST using class methods.
96
+ # binaryData = getImage
97
+ # SimpleData.post binaryData, "image/png"
98
+ #
99
+ # # GET requst with a custom request_header
100
+ # sh = SimpleHttp.new "http://www.example.com"
101
+ # sh.request_headers= {'X-Special-Http-Header'=>'my-value'}
102
+ # sh.get
103
+ class SimpleHttp
104
+
105
+ VERSION='0.1.1'
106
+
107
+ attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_pwd, :uri, :request_headers, :response_headers, :response_handlers, :follow_num_redirects
108
+
109
+ RESPONSE_HANDLERS = {
110
+ Net::HTTPResponse => lambda { |request, response, http|
111
+ response.each_header {|key, value|
112
+ http.response_headers[key]=value
113
+ }
114
+ raise response.to_s
115
+ },
116
+ Net::HTTPSuccess => lambda { |request, response, http|
117
+ response.each_header {|key, value|
118
+ http.response_headers[key]=value
119
+ }
120
+ return response.body
121
+ },
122
+ Net::HTTPRedirection => lambda { |request, response, http|
123
+ raise "too many redirects!" unless http.follow_num_redirects > 0
124
+
125
+ # create a new SimpleHttp for the location
126
+ # refered to decreasing the remaining redirects
127
+ # by one.
128
+ sh = SimpleHttp.new response['location']
129
+ sh.follow_num_redirects = http.follow_num_redirects-1
130
+
131
+ # copy the response handlers used in the current
132
+ # request in case they were non standard.
133
+ sh.response_handlers = http.response_handlers
134
+
135
+ # copy the request headers
136
+ sh.request_headers = http.request_headers
137
+ sh.response_headers = http.response_headers
138
+
139
+ # copy host and port
140
+ sh.uri.host = http.uri.host
141
+ sh.uri.port = http.uri.port
142
+
143
+ # http doesn't permit redirects for methods
144
+ # other than GET of HEAD, so we complain in case
145
+ # we get them in response to a POST request. (Or
146
+ # anything other than GET, for that matter.)
147
+
148
+ case request
149
+ when Net::HTTP::Get : sh.get
150
+ when Net::HTTP::Post : sh.post
151
+ else
152
+ raise "Not a valid HTTP method for redirection: #{request.class}"
153
+ end
154
+ }
155
+
156
+ }
157
+
158
+ # SimpleHttp can either be used directly through the +get+ and
159
+ # +post+ class methods or be instantiated, in case you need to
160
+ # to add custom behaviour to the requests.
161
+ #
162
+ # @param may be a URI or a String.
163
+ #
164
+ # Example:
165
+ # http = SimpleHttp.new(URI.parse("http://www.example.com"))
166
+ # http = SimpleHttp.new "www.example.com"
167
+ # http = SimpleHttp.new "http://usr:pwd@www.example.com:1234"
168
+ def initialize uri
169
+ set_proxy ENV['http_proxy'] if ENV['http_proxy']
170
+
171
+ if uri.class == String
172
+
173
+ unless uri =~ /^https?:\/\//
174
+ uri = "http://#{uri}"
175
+ end
176
+
177
+ uri = URI.parse uri
178
+
179
+ end
180
+ @uri = uri
181
+ if !@uri.path || "" == @uri.path.strip
182
+ @uri.path="/"
183
+ end
184
+
185
+
186
+ @request_headers={}
187
+ @response_headers={}
188
+ @response_handlers=RESPONSE_HANDLERS.clone
189
+ @follow_num_redirects=3
190
+
191
+ if @uri.user
192
+ basic_authentication @uri.user, @uri.password
193
+ end
194
+
195
+ end
196
+
197
+ #
198
+ # Provides facilities to perform http basic authentication. You
199
+ # don't need to provide +usr+ and +pwd+ if they are already included
200
+ # in the uri, i.e. http://user:password@www.example.com/
201
+ #
202
+
203
+ def basic_authentication usr, pwd
204
+ str = Base64.encode64("#{usr}:#{pwd}")
205
+ str = "Basic #{str}"
206
+ @request_headers["Authorization"]=str
207
+ end
208
+
209
+ #
210
+ # this method can be used to register response handlers for specific
211
+ # http responses in case you need to override the default behaviour.
212
+ # Defaults are:
213
+ #
214
+ # HTTPSuccess : return the body of the response
215
+ # HTTPRedirection : follow the redirection until success
216
+ # Others : raise an exception
217
+ #
218
+ # `clazz` is the subclass of HTTPResponse (or HTTPResponse in case you
219
+ # want to define "default" behaviour) that you are registering the
220
+ # handler for.
221
+ #
222
+ # `block` is the handler itself, if a response of the appropriate class
223
+ # is received, `block` is called with three parameters: the the
224
+ # Net::HTTPRequest, the actual HTTPResponse object that was received
225
+ # and a reference to the instance of `SimpleHttp` that is executing the
226
+ # call.
227
+ #
228
+ # example:
229
+ #
230
+ # # to override the default action of following a HTTP
231
+ # # redirect, you could register the folllowing handler:
232
+ #
233
+ # sh = SimpleHttp "www.example.com"
234
+ # sh.register_response_handler Net::HTTPRedirection {|request, response, shttp|
235
+ # response['location']
236
+ # }
237
+ #
238
+
239
+ def register_response_handler clazz, &block
240
+ c = clazz
241
+ while c != Object
242
+ # completely unnecessary sanity check to make sure parameter
243
+ # `clazz` is in fact a HTTPResponse ...
244
+ if c == Net::HTTPResponse
245
+ @response_handlers[clazz]=block
246
+ return
247
+ end
248
+ c = c.superclass
249
+ end
250
+
251
+ raise "Trying to register a response handler for non-response class: #{clazz}"
252
+ end
253
+
254
+ #
255
+ # Set the proxy to use for the http request.
256
+ # Note that you don't need to set the proxy in case the
257
+ # `http_proxy` environment variable is set. To override
258
+ # previous proxy settings and connect directly, call
259
+ # `set_proxy nil`
260
+ #
261
+ # usage:
262
+ # http = SimpleHttp.new "www.example.com"
263
+ #
264
+ # http.set_proxy "http://proxy:8000"
265
+ # or:
266
+ # http.set_proxy(URI.parse("http://proxy:8000"))
267
+ # or:
268
+ # http.set_proxy 'proxy', '8000', 'my_user', 'secret'
269
+ # or:
270
+ # http.set_proxy nil # to override previous proxy
271
+ # settings and make the request directly.
272
+ #
273
+
274
+
275
+ def set_proxy proxy, port=nil, user=nil, pwd=nil
276
+
277
+
278
+ if !proxy
279
+ @proxy_host=@proxy_port=@proxy_user=@proxy_pwd=nil
280
+ return
281
+ end
282
+
283
+ if proxy.class == String
284
+ if !port && !user && !pwd
285
+ proxy = URI.parse(proxy)
286
+ else
287
+ @proxy_host= host
288
+ @proxy_port= port
289
+ @proxy_user= user
290
+ @proxy_pwd = pwd
291
+ end
292
+ end
293
+
294
+ if proxy.class == URI::HTTP
295
+ @proxy_host= proxy.host
296
+ @proxy_port= proxy.port
297
+ @proxy_user= proxy.user
298
+ @proxy_pwd = proxy.password
299
+ end
300
+ end
301
+
302
+ # interal
303
+ # Takes a HTTPResponse (or subclass) and determines how to
304
+ # handle the response. Default behaviour is:
305
+ #
306
+ # HTTPSuccess : return the body of the response
307
+ # HTTPRedirection : follow the redirect until success.
308
+ # default : raise the HTTPResponse.
309
+ #
310
+ # the default behaviour can be overidden by registering a
311
+ # response handler using the `register_response_handler` method.
312
+ #
313
+
314
+ def handle_response http_request, http_response
315
+ raise "Not a Net::HTTPResponse" unless http_response.is_a? Net::HTTPResponse
316
+
317
+ c = http_response.class
318
+ while c!=Object
319
+ # the response_handlers hash contains a handler
320
+ # for the specific response class.
321
+ if @response_handlers[c]
322
+ return @response_handlers[c].call(http_request, http_response, self)
323
+ end
324
+
325
+ c=c.superclass
326
+ end
327
+
328
+ # if we reached this place, no handler was registered
329
+ # for this response. default is to return the response.
330
+
331
+ return http_response
332
+ end
333
+
334
+ # internal
335
+ def do_http request
336
+ response = nil
337
+
338
+ http = Net::HTTP.new(@uri.host, @uri.port, @proxy_host,
339
+ @proxy_port, @proxy_user, @proxy_pwd)
340
+ http.use_ssl = @uri.scheme == 'https'
341
+
342
+ # add custom request headers.
343
+
344
+ @request_headers.each {|key,value|
345
+ request[key]=value;
346
+ }
347
+
348
+ handle_response(request, http.request(request));
349
+ end
350
+
351
+ # internal
352
+ def make_query query
353
+ return query unless query && query.class == Hash
354
+ query.inject([]) do |s, (key, value)|
355
+ s + [CGI::escape(key) + "=" + CGI::escape(value)]
356
+ end.join('&')
357
+ end
358
+
359
+ # Make a simple GET request to the provided URI.
360
+ #
361
+ # Example:
362
+ # puts(SimpleHttp.get("www.example.com"))
363
+ def self.get uri, query=nil
364
+ http = SimpleHttp.new uri
365
+ http.get query
366
+ end
367
+
368
+ # Make a POST request to the provided URI.
369
+ #
370
+ # Example:
371
+ # puts(SimpleHttp.post("www.example.com", "query"=>"my_query"))
372
+ #
373
+ # Alternatively, you can post any sort of data, but will have to
374
+ # set the appriate content_type:
375
+ #
376
+ # SimpleHttp.post("http://www.example.com/", binary_data, "img/png")
377
+
378
+ def self.post uri, query=nil, content_type='application/x-www-form-urlencoded'
379
+ http = SimpleHttp.new uri
380
+ http.post query, content_type
381
+ end
382
+
383
+ # Call the +get+ method as an instance method if you need to
384
+ # modify the default behaviour of the library, or set special
385
+ # headers:
386
+ #
387
+ # http = SimpleHttp.new "www.example.com"
388
+ # http.request_headers["X-Special"]="whatever"
389
+ # str = http.get
390
+ def get query = nil
391
+ if (query = make_query query)
392
+ @uri.query = @uri.query ? @uri.query+"&"+query : query
393
+ end
394
+ full_path = @uri.path + (@uri.query ? "?#{@uri.query}" : "")
395
+
396
+ req = Net::HTTP::Get.new(full_path)
397
+ # puts Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pwd).get(@uri)
398
+ do_http req
399
+ end
400
+
401
+ #
402
+ # Post the query data to the url.
403
+ # The body of the request remains empty if query=nil.
404
+ # In case `query` is a `Hash`, it's assumed that we are
405
+ # sending a form.
406
+ # In case `query` is a `String`, it's also assumed that a
407
+ # form is being sent, UNLESS the `content_type` parameter
408
+ # is set.
409
+ #
410
+ def post query=nil, content_type='application/x-www-form-urlencoded'
411
+ req = Net::HTTP::Post.new(@uri.path)
412
+
413
+ req.body= make_query query if query
414
+ req.content_type=content_type if query
415
+ req.content_length=query ? req.body.length : 0
416
+
417
+ do_http req
418
+ end
419
+
420
+ end
421
+
422
+ #ht = SimpleHttp.new "http://www.google.com/aldfksjaldskjfalskjfdlk"
423
+ ##ht.register_response_handler(Net::HTTPRedirection) {|req, res, ht| puts res['location']}
424
+ #puts ht.get.class
425
+ ##puts(ht.get("q"=>"bla"))
426
+ #
427
+ ##puts (SimpleHttp.get "http://www.google.com")
428
+ #
429
+ #['http://www.google.com/', 'www.google.com', 'https://www.google.com'].each {|u|
430
+ # SimpleHttp.new u
431
+ #}
432
+ ##puts ht.post
433
+