benschwarz-smoke 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (245) hide show
  1. data/Rakefile +47 -0
  2. data/VERSION.yml +2 -2
  3. data/lib/core_ext/string.rb +6 -0
  4. data/lib/smoke/cache.rb +38 -23
  5. data/lib/smoke/request.rb +4 -8
  6. data/lib/smoke.rb +7 -2
  7. data/rdoc/classes/Smoke/Origin.html +340 -0
  8. data/rdoc/classes/Smoke/Source/Data.html +126 -0
  9. data/rdoc/classes/Smoke/Source/Feed.html +117 -0
  10. data/rdoc/classes/Smoke/Source/YQL.html +223 -0
  11. data/rdoc/classes/Smoke.html +260 -0
  12. data/rdoc/created.rid +1 -0
  13. data/rdoc/files/README_markdown.html +180 -0
  14. data/rdoc/files/lib/core_ext/hash_rb.html +49 -0
  15. data/rdoc/files/lib/smoke/origin_rb.html +49 -0
  16. data/rdoc/files/lib/smoke/request_rb.html +49 -0
  17. data/rdoc/files/lib/smoke/source/data_rb.html +49 -0
  18. data/rdoc/files/lib/smoke/source/feed_rb.html +49 -0
  19. data/rdoc/files/lib/smoke/source/join_rb.html +49 -0
  20. data/rdoc/files/lib/smoke/source/yql_rb.html +49 -0
  21. data/rdoc/files/lib/smoke_rb.html +65 -0
  22. data/rdoc/fr_class_index.html +21 -0
  23. data/rdoc/fr_file_index.html +28 -0
  24. data/rdoc/fr_method_index.html +4459 -0
  25. data/rdoc/index.html +15 -0
  26. data/rdoc/rdoc-style.css +319 -0
  27. data/spec/smoke/cache_spec.rb +28 -6
  28. data/vendor/crack/History +15 -0
  29. data/vendor/crack/LICENSE +20 -0
  30. data/vendor/crack/README.rdoc +42 -0
  31. data/vendor/crack/Rakefile +49 -0
  32. data/vendor/crack/VERSION.yml +4 -0
  33. data/vendor/crack/crack.gemspec +61 -0
  34. data/vendor/crack/lib/crack/core_extensions.rb +128 -0
  35. data/vendor/crack/lib/crack/json.rb +68 -0
  36. data/vendor/crack/lib/crack/xml.rb +214 -0
  37. data/vendor/crack/lib/crack.rb +7 -0
  38. data/vendor/crack/test/crack_test.rb +4 -0
  39. data/vendor/crack/test/data/twittersearch-firefox.json +1 -0
  40. data/vendor/crack/test/data/twittersearch-ie.json +1 -0
  41. data/vendor/crack/test/hash_test.rb +56 -0
  42. data/vendor/crack/test/json_test.rb +66 -0
  43. data/vendor/crack/test/string_test.rb +31 -0
  44. data/vendor/crack/test/test_helper.rb +12 -0
  45. data/vendor/crack/test/xml_test.rb +489 -0
  46. data/vendor/dependencies/README.markdown +113 -0
  47. data/vendor/dependencies/Rakefile +5 -0
  48. data/vendor/dependencies/bin/dep +83 -0
  49. data/vendor/dependencies/dependencies.gemspec +15 -0
  50. data/vendor/dependencies/dependencies.gemspec.erb +27 -0
  51. data/vendor/dependencies/lib/dependencies/dep.rb +99 -0
  52. data/vendor/dependencies/lib/dependencies.rb +5 -0
  53. data/vendor/dependencies/test/dependencies_test.rb +228 -0
  54. data/vendor/dependencies/test/foobaz-0.3.gem +0 -0
  55. data/vendor/fakeweb/CHANGELOG +163 -0
  56. data/vendor/fakeweb/LICENSE.txt +281 -0
  57. data/vendor/fakeweb/README.rdoc +193 -0
  58. data/vendor/fakeweb/Rakefile +76 -0
  59. data/vendor/fakeweb/fakeweb.gemspec +21 -0
  60. data/vendor/fakeweb/lib/fake_web/ext/net_http.rb +71 -0
  61. data/vendor/fakeweb/lib/fake_web/registry.rb +103 -0
  62. data/vendor/fakeweb/lib/fake_web/responder.rb +113 -0
  63. data/vendor/fakeweb/lib/fake_web/response.rb +10 -0
  64. data/vendor/fakeweb/lib/fake_web/stub_socket.rb +15 -0
  65. data/vendor/fakeweb/lib/fake_web/utility.rb +22 -0
  66. data/vendor/fakeweb/lib/fake_web.rb +172 -0
  67. data/vendor/fakeweb/lib/fakeweb.rb +2 -0
  68. data/vendor/fakeweb/test/fixtures/google_response_from_curl +12 -0
  69. data/vendor/fakeweb/test/fixtures/google_response_with_transfer_encoding +17 -0
  70. data/vendor/fakeweb/test/fixtures/google_response_without_transfer_encoding +11 -0
  71. data/vendor/fakeweb/test/fixtures/test_example.txt +1 -0
  72. data/vendor/fakeweb/test/fixtures/test_txt_file +3 -0
  73. data/vendor/fakeweb/test/test_allow_net_connect.rb +85 -0
  74. data/vendor/fakeweb/test/test_deprecations.rb +54 -0
  75. data/vendor/fakeweb/test/test_fake_authentication.rb +92 -0
  76. data/vendor/fakeweb/test/test_fake_web.rb +535 -0
  77. data/vendor/fakeweb/test/test_fake_web_open_uri.rb +58 -0
  78. data/vendor/fakeweb/test/test_helper.rb +74 -0
  79. data/vendor/fakeweb/test/test_missing_open_uri.rb +25 -0
  80. data/vendor/fakeweb/test/test_precedence.rb +51 -0
  81. data/vendor/fakeweb/test/test_query_string.rb +45 -0
  82. data/vendor/fakeweb/test/test_regexes.rb +103 -0
  83. data/vendor/fakeweb/test/test_response_headers.rb +73 -0
  84. data/vendor/fakeweb/test/test_trailing_slashes.rb +53 -0
  85. data/vendor/fakeweb/test/test_utility.rb +70 -0
  86. data/vendor/json-1.1.3/CHANGES +93 -0
  87. data/vendor/json-1.1.3/GPL +340 -0
  88. data/vendor/json-1.1.3/README +78 -0
  89. data/vendor/json-1.1.3/RUBY +58 -0
  90. data/vendor/json-1.1.3/Rakefile +309 -0
  91. data/vendor/json-1.1.3/TODO +1 -0
  92. data/vendor/json-1.1.3/VERSION +1 -0
  93. data/vendor/json-1.1.3/benchmarks/benchmark.txt +133 -0
  94. data/vendor/json-1.1.3/benchmarks/benchmark_generator.rb +48 -0
  95. data/vendor/json-1.1.3/benchmarks/benchmark_parser.rb +26 -0
  96. data/vendor/json-1.1.3/benchmarks/benchmark_rails.rb +26 -0
  97. data/vendor/json-1.1.3/bin/edit_json.rb +10 -0
  98. data/vendor/json-1.1.3/bin/prettify_json.rb +76 -0
  99. data/vendor/json-1.1.3/data/example.json +1 -0
  100. data/vendor/json-1.1.3/data/index.html +38 -0
  101. data/vendor/json-1.1.3/data/prototype.js +4184 -0
  102. data/vendor/json-1.1.3/ext/json/ext/generator/extconf.rb +9 -0
  103. data/vendor/json-1.1.3/ext/json/ext/generator/generator.c +875 -0
  104. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.c +182 -0
  105. data/vendor/json-1.1.3/ext/json/ext/generator/unicode.h +53 -0
  106. data/vendor/json-1.1.3/ext/json/ext/parser/extconf.rb +9 -0
  107. data/vendor/json-1.1.3/ext/json/ext/parser/parser.c +1758 -0
  108. data/vendor/json-1.1.3/ext/json/ext/parser/parser.rl +638 -0
  109. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.c +154 -0
  110. data/vendor/json-1.1.3/ext/json/ext/parser/unicode.h +58 -0
  111. data/vendor/json-1.1.3/install.rb +26 -0
  112. data/vendor/json-1.1.3/lib/json/Array.xpm +21 -0
  113. data/vendor/json-1.1.3/lib/json/FalseClass.xpm +21 -0
  114. data/vendor/json-1.1.3/lib/json/Hash.xpm +21 -0
  115. data/vendor/json-1.1.3/lib/json/Key.xpm +73 -0
  116. data/vendor/json-1.1.3/lib/json/NilClass.xpm +21 -0
  117. data/vendor/json-1.1.3/lib/json/Numeric.xpm +28 -0
  118. data/vendor/json-1.1.3/lib/json/String.xpm +96 -0
  119. data/vendor/json-1.1.3/lib/json/TrueClass.xpm +21 -0
  120. data/vendor/json-1.1.3/lib/json/add/core.rb +135 -0
  121. data/vendor/json-1.1.3/lib/json/add/rails.rb +58 -0
  122. data/vendor/json-1.1.3/lib/json/common.rb +354 -0
  123. data/vendor/json-1.1.3/lib/json/editor.rb +1362 -0
  124. data/vendor/json-1.1.3/lib/json/ext.rb +13 -0
  125. data/vendor/json-1.1.3/lib/json/json.xpm +1499 -0
  126. data/vendor/json-1.1.3/lib/json/pure/generator.rb +394 -0
  127. data/vendor/json-1.1.3/lib/json/pure/parser.rb +259 -0
  128. data/vendor/json-1.1.3/lib/json/pure.rb +75 -0
  129. data/vendor/json-1.1.3/lib/json/version.rb +9 -0
  130. data/vendor/json-1.1.3/lib/json.rb +235 -0
  131. data/vendor/json-1.1.3/tests/fixtures/fail1.json +1 -0
  132. data/vendor/json-1.1.3/tests/fixtures/fail10.json +1 -0
  133. data/vendor/json-1.1.3/tests/fixtures/fail11.json +1 -0
  134. data/vendor/json-1.1.3/tests/fixtures/fail12.json +1 -0
  135. data/vendor/json-1.1.3/tests/fixtures/fail13.json +1 -0
  136. data/vendor/json-1.1.3/tests/fixtures/fail14.json +1 -0
  137. data/vendor/json-1.1.3/tests/fixtures/fail18.json +1 -0
  138. data/vendor/json-1.1.3/tests/fixtures/fail19.json +1 -0
  139. data/vendor/json-1.1.3/tests/fixtures/fail2.json +1 -0
  140. data/vendor/json-1.1.3/tests/fixtures/fail20.json +1 -0
  141. data/vendor/json-1.1.3/tests/fixtures/fail21.json +1 -0
  142. data/vendor/json-1.1.3/tests/fixtures/fail22.json +1 -0
  143. data/vendor/json-1.1.3/tests/fixtures/fail23.json +1 -0
  144. data/vendor/json-1.1.3/tests/fixtures/fail24.json +1 -0
  145. data/vendor/json-1.1.3/tests/fixtures/fail25.json +1 -0
  146. data/vendor/json-1.1.3/tests/fixtures/fail27.json +2 -0
  147. data/vendor/json-1.1.3/tests/fixtures/fail28.json +2 -0
  148. data/vendor/json-1.1.3/tests/fixtures/fail3.json +1 -0
  149. data/vendor/json-1.1.3/tests/fixtures/fail4.json +1 -0
  150. data/vendor/json-1.1.3/tests/fixtures/fail5.json +1 -0
  151. data/vendor/json-1.1.3/tests/fixtures/fail6.json +1 -0
  152. data/vendor/json-1.1.3/tests/fixtures/fail7.json +1 -0
  153. data/vendor/json-1.1.3/tests/fixtures/fail8.json +1 -0
  154. data/vendor/json-1.1.3/tests/fixtures/fail9.json +1 -0
  155. data/vendor/json-1.1.3/tests/fixtures/pass1.json +56 -0
  156. data/vendor/json-1.1.3/tests/fixtures/pass15.json +1 -0
  157. data/vendor/json-1.1.3/tests/fixtures/pass16.json +1 -0
  158. data/vendor/json-1.1.3/tests/fixtures/pass17.json +1 -0
  159. data/vendor/json-1.1.3/tests/fixtures/pass2.json +1 -0
  160. data/vendor/json-1.1.3/tests/fixtures/pass26.json +1 -0
  161. data/vendor/json-1.1.3/tests/fixtures/pass3.json +6 -0
  162. data/vendor/json-1.1.3/tests/runner.rb +25 -0
  163. data/vendor/json-1.1.3/tests/test_json.rb +293 -0
  164. data/vendor/json-1.1.3/tests/test_json_addition.rb +161 -0
  165. data/vendor/json-1.1.3/tests/test_json_fixtures.rb +30 -0
  166. data/vendor/json-1.1.3/tests/test_json_generate.rb +100 -0
  167. data/vendor/json-1.1.3/tests/test_json_rails.rb +118 -0
  168. data/vendor/json-1.1.3/tests/test_json_unicode.rb +61 -0
  169. data/vendor/json-1.1.3/tools/fuzz.rb +140 -0
  170. data/vendor/json-1.1.3/tools/server.rb +62 -0
  171. data/vendor/moneta/LICENSE +20 -0
  172. data/vendor/moneta/README +51 -0
  173. data/vendor/moneta/Rakefile +60 -0
  174. data/vendor/moneta/TODO +4 -0
  175. data/vendor/moneta/benchmarks/various.rb +234 -0
  176. data/vendor/moneta/lib/moneta/basic_file.rb +111 -0
  177. data/vendor/moneta/lib/moneta/berkeley.rb +53 -0
  178. data/vendor/moneta/lib/moneta/couch.rb +63 -0
  179. data/vendor/moneta/lib/moneta/datamapper.rb +117 -0
  180. data/vendor/moneta/lib/moneta/file.rb +91 -0
  181. data/vendor/moneta/lib/moneta/lmc.rb +52 -0
  182. data/vendor/moneta/lib/moneta/memcache.rb +53 -0
  183. data/vendor/moneta/lib/moneta/memory.rb +11 -0
  184. data/vendor/moneta/lib/moneta/mongodb.rb +58 -0
  185. data/vendor/moneta/lib/moneta/redis.rb +49 -0
  186. data/vendor/moneta/lib/moneta/rufus.rb +41 -0
  187. data/vendor/moneta/lib/moneta/s3.rb +162 -0
  188. data/vendor/moneta/lib/moneta/sdbm.rb +33 -0
  189. data/vendor/moneta/lib/moneta/tyrant.rb +58 -0
  190. data/vendor/moneta/lib/moneta/xattr.rb +58 -0
  191. data/vendor/moneta/lib/moneta.rb +76 -0
  192. data/vendor/moneta/moneta.gemspec +32 -0
  193. data/vendor/moneta/script/destroy +14 -0
  194. data/vendor/moneta/script/generate +14 -0
  195. data/vendor/moneta/spec/moneta_basic_file_spec.rb +50 -0
  196. data/vendor/moneta/spec/moneta_berkeley_spec.rb +20 -0
  197. data/vendor/moneta/spec/moneta_couch_spec.rb +22 -0
  198. data/vendor/moneta/spec/moneta_datamapper_spec.rb +79 -0
  199. data/vendor/moneta/spec/moneta_file_spec.rb +21 -0
  200. data/vendor/moneta/spec/moneta_lmc_spec.rb +24 -0
  201. data/vendor/moneta/spec/moneta_memcache_spec.rb +16 -0
  202. data/vendor/moneta/spec/moneta_memory_spec.rb +12 -0
  203. data/vendor/moneta/spec/moneta_mongodb_spec.rb +16 -0
  204. data/vendor/moneta/spec/moneta_redis_spec.rb +16 -0
  205. data/vendor/moneta/spec/moneta_rufus_spec.rb +15 -0
  206. data/vendor/moneta/spec/moneta_s3_spec.rb +19 -0
  207. data/vendor/moneta/spec/moneta_sdbm_spec.rb +21 -0
  208. data/vendor/moneta/spec/moneta_tyrant_spec.rb +15 -0
  209. data/vendor/moneta/spec/moneta_xattr_spec.rb +21 -0
  210. data/vendor/moneta/spec/shared.rb +122 -0
  211. data/vendor/moneta/spec/spec_helper.rb +7 -0
  212. data/vendor/rest-client/README.rdoc +151 -0
  213. data/vendor/rest-client/Rakefile +58 -0
  214. data/vendor/rest-client/VERSION +1 -0
  215. data/vendor/rest-client/bin/restclient +87 -0
  216. data/vendor/rest-client/lib/rest_client.rb +2 -0
  217. data/vendor/rest-client/lib/restclient/exceptions.rb +88 -0
  218. data/vendor/rest-client/lib/restclient/mixin/response.rb +43 -0
  219. data/vendor/rest-client/lib/restclient/raw_response.rb +30 -0
  220. data/vendor/rest-client/lib/restclient/request.rb +238 -0
  221. data/vendor/rest-client/lib/restclient/resource.rb +146 -0
  222. data/vendor/rest-client/lib/restclient/response.rb +20 -0
  223. data/vendor/rest-client/lib/restclient.rb +99 -0
  224. data/vendor/rest-client/rest-client.gemspec +66 -0
  225. data/vendor/rest-client/spec/base.rb +4 -0
  226. data/vendor/rest-client/spec/exceptions_spec.rb +65 -0
  227. data/vendor/rest-client/spec/mixin/response_spec.rb +46 -0
  228. data/vendor/rest-client/spec/raw_response_spec.rb +17 -0
  229. data/vendor/rest-client/spec/request_spec.rb +476 -0
  230. data/vendor/rest-client/spec/resource_spec.rb +75 -0
  231. data/vendor/rest-client/spec/response_spec.rb +16 -0
  232. data/vendor/rest-client/spec/restclient_spec.rb +53 -0
  233. data/vendor/simple-rss/LICENSE +429 -0
  234. data/vendor/simple-rss/README +43 -0
  235. data/vendor/simple-rss/Rakefile +212 -0
  236. data/vendor/simple-rss/install.rb +40 -0
  237. data/vendor/simple-rss/lib/simple-rss.rb +150 -0
  238. data/vendor/simple-rss/simple-rss.gemspec +12 -0
  239. data/vendor/simple-rss/test/base/base_test.rb +51 -0
  240. data/vendor/simple-rss/test/data/atom.xml +45 -0
  241. data/vendor/simple-rss/test/data/not-rss.xml +8 -0
  242. data/vendor/simple-rss/test/data/rss09.rdf +79 -0
  243. data/vendor/simple-rss/test/data/rss20.xml +818 -0
  244. data/vendor/simple-rss/test/test_helper.rb +4 -0
  245. metadata +314 -40
@@ -0,0 +1,43 @@
1
+ module RestClient
2
+ module Mixin
3
+ module Response
4
+ attr_reader :net_http_res
5
+
6
+ # HTTP status code, always 200 since RestClient throws exceptions for
7
+ # other codes.
8
+ def code
9
+ @code ||= @net_http_res.code.to_i
10
+ end
11
+
12
+ # A hash of the headers, beautified with symbols and underscores.
13
+ # e.g. "Content-type" will become :content_type.
14
+ def headers
15
+ @headers ||= self.class.beautify_headers(@net_http_res.to_hash)
16
+ end
17
+
18
+ # Hash of cookies extracted from response headers
19
+ def cookies
20
+ @cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
21
+ key, val = raw_c.split('=')
22
+ unless %w(expires domain path secure).member?(key)
23
+ out[key] = val
24
+ end
25
+ out
26
+ end
27
+ end
28
+
29
+ def self.included(receiver)
30
+ receiver.extend(RestClient::Mixin::Response::ClassMethods)
31
+ end
32
+
33
+ module ClassMethods
34
+ def beautify_headers(headers)
35
+ headers.inject({}) do |out, (key, value)|
36
+ out[key.gsub(/-/, '_').to_sym] = value.first
37
+ out
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/mixin/response'
2
+
3
+ module RestClient
4
+ # The response from RestClient on a raw request looks like a string, but is
5
+ # actually one of these. 99% of the time you're making a rest call all you
6
+ # care about is the body, but on the occassion you want to fetch the
7
+ # headers you can:
8
+ #
9
+ # RestClient.get('http://example.com').headers[:content_type]
10
+ #
11
+ # In addition, if you do not use the response as a string, you can access
12
+ # a Tempfile object at res.file, which contains the path to the raw
13
+ # downloaded request body.
14
+ class RawResponse
15
+ include RestClient::Mixin::Response
16
+
17
+ attr_reader :file
18
+
19
+ def initialize(tempfile, net_http_res)
20
+ @net_http_res = net_http_res
21
+ @file = tempfile
22
+ end
23
+
24
+ def to_s
25
+ @file.open
26
+ @file.read
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,238 @@
1
+ require 'tempfile'
2
+
3
+ module RestClient
4
+ # This class is used internally by RestClient to send the request, but you can also
5
+ # access it internally if you'd like to use a method not directly supported by the
6
+ # main API. For example:
7
+ #
8
+ # RestClient::Request.execute(:method => :head, :url => 'http://example.com')
9
+ #
10
+ class Request
11
+ attr_reader :method, :url, :payload, :headers,
12
+ :cookies, :user, :password, :timeout, :open_timeout,
13
+ :verify_ssl, :ssl_client_cert, :ssl_client_key, :ssl_ca_file,
14
+ :raw_response
15
+
16
+ def self.execute(args)
17
+ new(args).execute
18
+ end
19
+
20
+ def initialize(args)
21
+ @method = args[:method] or raise ArgumentError, "must pass :method"
22
+ @url = args[:url] or raise ArgumentError, "must pass :url"
23
+ @headers = args[:headers] || {}
24
+ @cookies = @headers.delete(:cookies) || args[:cookies] || {}
25
+ @payload = process_payload(args[:payload])
26
+ @user = args[:user]
27
+ @password = args[:password]
28
+ @timeout = args[:timeout]
29
+ @open_timeout = args[:open_timeout]
30
+ @raw_response = args[:raw_response] || false
31
+ @verify_ssl = args[:verify_ssl] || false
32
+ @ssl_client_cert = args[:ssl_client_cert] || nil
33
+ @ssl_client_key = args[:ssl_client_key] || nil
34
+ @ssl_ca_file = args[:ssl_ca_file] || nil
35
+ @tf = nil # If you are a raw request, this is your tempfile
36
+ end
37
+
38
+ def execute
39
+ execute_inner
40
+ rescue Redirect => e
41
+ @url = e.url
42
+ @method = :get
43
+ @payload = nil
44
+ execute
45
+ end
46
+
47
+ def execute_inner
48
+ uri = parse_url_with_auth(url)
49
+ transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload
50
+ end
51
+
52
+ def make_headers(user_headers)
53
+ unless @cookies.empty?
54
+ user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
55
+ end
56
+
57
+ default_headers.merge(user_headers).inject({}) do |final, (key, value)|
58
+ final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s
59
+ final
60
+ end
61
+ end
62
+
63
+ def net_http_class
64
+ if RestClient.proxy
65
+ proxy_uri = URI.parse(RestClient.proxy)
66
+ Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
67
+ else
68
+ Net::HTTP
69
+ end
70
+ end
71
+
72
+ def net_http_request_class(method)
73
+ Net::HTTP.const_get(method.to_s.capitalize)
74
+ end
75
+
76
+ def parse_url(url)
77
+ url = "http://#{url}" unless url.match(/^http/)
78
+ URI.parse(url)
79
+ end
80
+
81
+ def parse_url_with_auth(url)
82
+ uri = parse_url(url)
83
+ @user = uri.user if uri.user
84
+ @password = uri.password if uri.password
85
+ uri
86
+ end
87
+
88
+ def process_payload(p=nil, parent_key=nil)
89
+ unless p.is_a?(Hash)
90
+ p
91
+ else
92
+ @headers[:content_type] ||= 'application/x-www-form-urlencoded'
93
+ p.keys.map do |k|
94
+ key = parent_key ? "#{parent_key}[#{k}]" : k
95
+ if p[k].is_a? Hash
96
+ process_payload(p[k], key)
97
+ else
98
+ value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
99
+ "#{key}=#{value}"
100
+ end
101
+ end.join("&")
102
+ end
103
+ end
104
+
105
+ def transmit(uri, req, payload)
106
+ setup_credentials(req)
107
+
108
+ net = net_http_class.new(uri.host, uri.port)
109
+ net.use_ssl = uri.is_a?(URI::HTTPS)
110
+ if @verify_ssl == false
111
+ net.verify_mode = OpenSSL::SSL::VERIFY_NONE
112
+ elsif @verify_ssl.is_a? Integer
113
+ net.verify_mode = @verify_ssl
114
+ end
115
+ net.cert = @ssl_client_cert if @ssl_client_cert
116
+ net.key = @ssl_client_key if @ssl_client_key
117
+ net.ca_file = @ssl_ca_file if @ssl_ca_file
118
+ net.read_timeout = @timeout if @timeout
119
+ net.open_timeout = @open_timeout if @open_timeout
120
+
121
+ display_log request_log
122
+
123
+ net.start do |http|
124
+ res = http.request(req, payload) { |http_response| fetch_body(http_response) }
125
+ result = process_result(res)
126
+ display_log response_log(res)
127
+
128
+ if result.kind_of?(String) or @method == :head
129
+ Response.new(result, res)
130
+ elsif @raw_response
131
+ RawResponse.new(@tf, res)
132
+ else
133
+ nil
134
+ end
135
+ end
136
+ rescue EOFError
137
+ raise RestClient::ServerBrokeConnection
138
+ rescue Timeout::Error
139
+ raise RestClient::RequestTimeout
140
+ end
141
+
142
+ def setup_credentials(req)
143
+ req.basic_auth(user, password) if user
144
+ end
145
+
146
+ def fetch_body(http_response)
147
+ if @raw_response
148
+ # Taken from Chef, which as in turn...
149
+ # Stolen from http://www.ruby-forum.com/topic/166423
150
+ # Kudos to _why!
151
+ @tf = Tempfile.new("rest-client")
152
+ size, total = 0, http_response.header['Content-Length'].to_i
153
+ http_response.read_body do |chunk|
154
+ @tf.write(chunk)
155
+ size += chunk.size
156
+ if size == 0
157
+ display_log("#{@method} #{@url} done (0 length file)")
158
+ elsif total == 0
159
+ display_log("#{@method} #{@url} (zero content length)")
160
+ else
161
+ display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total])
162
+ end
163
+ end
164
+ @tf.close
165
+ @tf
166
+ else
167
+ http_response.read_body
168
+ end
169
+ http_response
170
+ end
171
+
172
+ def process_result(res)
173
+ if res.code =~ /\A2\d{2}\z/
174
+ # We don't decode raw requests
175
+ unless @raw_response
176
+ self.class.decode res['content-encoding'], res.body if res.body
177
+ end
178
+ elsif %w(301 302 303).include? res.code
179
+ url = res.header['Location']
180
+
181
+ if url !~ /^http/
182
+ uri = URI.parse(@url)
183
+ uri.path = "/#{url}".squeeze('/')
184
+ url = uri.to_s
185
+ end
186
+
187
+ raise Redirect, url
188
+ elsif res.code == "304"
189
+ raise NotModified, res
190
+ elsif res.code == "401"
191
+ raise Unauthorized, res
192
+ elsif res.code == "404"
193
+ raise ResourceNotFound, res
194
+ else
195
+ raise RequestFailed, res
196
+ end
197
+ end
198
+
199
+ def self.decode(content_encoding, body)
200
+ if content_encoding == 'gzip' and not body.empty?
201
+ Zlib::GzipReader.new(StringIO.new(body)).read
202
+ elsif content_encoding == 'deflate'
203
+ Zlib::Inflate.new.inflate(body)
204
+ else
205
+ body
206
+ end
207
+ end
208
+
209
+ def request_log
210
+ out = []
211
+ out << "RestClient.#{method} #{url.inspect}"
212
+ out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload
213
+ out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty?
214
+ out.join(', ')
215
+ end
216
+
217
+ def response_log(res)
218
+ size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size)
219
+ "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes"
220
+ end
221
+
222
+ def display_log(msg)
223
+ return unless log_to = RestClient.log
224
+
225
+ if log_to == 'stdout'
226
+ STDOUT.puts msg
227
+ elsif log_to == 'stderr'
228
+ STDERR.puts msg
229
+ else
230
+ File.open(log_to, 'a') { |f| f.puts msg }
231
+ end
232
+ end
233
+
234
+ def default_headers
235
+ { :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' }
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,146 @@
1
+ module RestClient
2
+ # A class that can be instantiated for access to a RESTful resource,
3
+ # including authentication.
4
+ #
5
+ # Example:
6
+ #
7
+ # resource = RestClient::Resource.new('http://some/resource')
8
+ # jpg = resource.get(:accept => 'image/jpg')
9
+ #
10
+ # With HTTP basic authentication:
11
+ #
12
+ # resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
13
+ # resource.delete
14
+ #
15
+ # With a timeout (seconds):
16
+ #
17
+ # RestClient::Resource.new('http://slow', :timeout => 10)
18
+ #
19
+ # With an open timeout (seconds):
20
+ #
21
+ # RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
22
+ #
23
+ # You can also use resources to share common headers. For headers keys,
24
+ # symbols are converted to strings. Example:
25
+ #
26
+ # resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
27
+ #
28
+ # This header will be transported as X-Client-Version (notice the X prefix,
29
+ # capitalization and hyphens)
30
+ #
31
+ # Use the [] syntax to allocate subresources:
32
+ #
33
+ # site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
34
+ # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
35
+ #
36
+ class Resource
37
+ attr_reader :url, :options
38
+
39
+ def initialize(url, options={}, backwards_compatibility=nil)
40
+ @url = url
41
+ if options.class == Hash
42
+ @options = options
43
+ else # compatibility with previous versions
44
+ @options = { :user => options, :password => backwards_compatibility }
45
+ end
46
+ end
47
+
48
+ def get(additional_headers={})
49
+ Request.execute(options.merge(
50
+ :method => :get,
51
+ :url => url,
52
+ :headers => headers.merge(additional_headers)
53
+ ))
54
+ end
55
+
56
+ def post(payload, additional_headers={})
57
+ Request.execute(options.merge(
58
+ :method => :post,
59
+ :url => url,
60
+ :payload => payload,
61
+ :headers => headers.merge(additional_headers)
62
+ ))
63
+ end
64
+
65
+ def put(payload, additional_headers={})
66
+ Request.execute(options.merge(
67
+ :method => :put,
68
+ :url => url,
69
+ :payload => payload,
70
+ :headers => headers.merge(additional_headers)
71
+ ))
72
+ end
73
+
74
+ def delete(additional_headers={})
75
+ Request.execute(options.merge(
76
+ :method => :delete,
77
+ :url => url,
78
+ :headers => headers.merge(additional_headers)
79
+ ))
80
+ end
81
+
82
+ def to_s
83
+ url
84
+ end
85
+
86
+ def user
87
+ options[:user]
88
+ end
89
+
90
+ def password
91
+ options[:password]
92
+ end
93
+
94
+ def headers
95
+ options[:headers] || {}
96
+ end
97
+
98
+ def timeout
99
+ options[:timeout]
100
+ end
101
+
102
+ def open_timeout
103
+ options[:open_timeout]
104
+ end
105
+
106
+ # Construct a subresource, preserving authentication.
107
+ #
108
+ # Example:
109
+ #
110
+ # site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
111
+ # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
112
+ #
113
+ # This is especially useful if you wish to define your site in one place and
114
+ # call it in multiple locations:
115
+ #
116
+ # def orders
117
+ # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
118
+ # end
119
+ #
120
+ # orders.get # GET http://example.com/orders
121
+ # orders['1'].get # GET http://example.com/orders/1
122
+ # orders['1/items'].delete # DELETE http://example.com/orders/1/items
123
+ #
124
+ # Nest resources as far as you want:
125
+ #
126
+ # site = RestClient::Resource.new('http://example.com')
127
+ # posts = site['posts']
128
+ # first_post = posts['1']
129
+ # comments = first_post['comments']
130
+ # comments.post 'Hello', :content_type => 'text/plain'
131
+ #
132
+ def [](suburl)
133
+ self.class.new(concat_urls(url, suburl), options)
134
+ end
135
+
136
+ def concat_urls(url, suburl) # :nodoc:
137
+ url = url.to_s
138
+ suburl = suburl.to_s
139
+ if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
140
+ url + suburl
141
+ else
142
+ "#{url}/#{suburl}"
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/mixin/response'
2
+
3
+ module RestClient
4
+ # The response from RestClient looks like a string, but is actually one of
5
+ # these. 99% of the time you're making a rest call all you care about is
6
+ # the body, but on the occassion you want to fetch the headers you can:
7
+ #
8
+ # RestClient.get('http://example.com').headers[:content_type]
9
+ #
10
+ class Response < String
11
+
12
+ include RestClient::Mixin::Response
13
+
14
+ def initialize(string, net_http_res)
15
+ @net_http_res = net_http_res
16
+ super(string || "")
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,99 @@
1
+ require 'uri'
2
+ require 'zlib'
3
+ require 'stringio'
4
+
5
+ begin
6
+ require 'net/https'
7
+ rescue LoadError => e
8
+ raise e unless RUBY_PLATFORM =~ /linux/
9
+ raise LoadError, "no such file to load -- net/https. Try running apt-get install libopenssl-ruby"
10
+ end
11
+
12
+ require File.dirname(__FILE__) + '/restclient/request'
13
+ require File.dirname(__FILE__) + '/restclient/mixin/response'
14
+ require File.dirname(__FILE__) + '/restclient/response'
15
+ require File.dirname(__FILE__) + '/restclient/raw_response'
16
+ require File.dirname(__FILE__) + '/restclient/resource'
17
+ require File.dirname(__FILE__) + '/restclient/exceptions'
18
+
19
+ # This module's static methods are the entry point for using the REST client.
20
+ #
21
+ # # GET
22
+ # xml = RestClient.get 'http://example.com/resource'
23
+ # jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
24
+ #
25
+ # # authentication and SSL
26
+ # RestClient.get 'https://user:password@example.com/private/resource'
27
+ #
28
+ # # POST or PUT with a hash sends parameters as a urlencoded form body
29
+ # RestClient.post 'http://example.com/resource', :param1 => 'one'
30
+ #
31
+ # # nest hash parameters
32
+ # RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
33
+ #
34
+ # # POST and PUT with raw payloads
35
+ # RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
36
+ # RestClient.post 'http://example.com/resource.xml', xml_doc
37
+ # RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
38
+ #
39
+ # # DELETE
40
+ # RestClient.delete 'http://example.com/resource'
41
+ #
42
+ # # retreive the response http code and headers
43
+ # res = RestClient.get 'http://example.com/some.jpg'
44
+ # res.code # => 200
45
+ # res.headers[:content_type] # => 'image/jpg'
46
+ #
47
+ # # HEAD
48
+ # RestClient.head('http://example.com').headers
49
+ #
50
+ # To use with a proxy, just set RestClient.proxy to the proper http proxy:
51
+ #
52
+ # RestClient.proxy = "http://proxy.example.com/"
53
+ #
54
+ # Or inherit the proxy from the environment:
55
+ #
56
+ # RestClient.proxy = ENV['http_proxy']
57
+ #
58
+ # For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
59
+ #
60
+ # >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
61
+ # => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
62
+ #
63
+ module RestClient
64
+ def self.get(url, headers={})
65
+ Request.execute(:method => :get, :url => url, :headers => headers)
66
+ end
67
+
68
+ def self.post(url, payload, headers={})
69
+ Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers)
70
+ end
71
+
72
+ def self.put(url, payload, headers={})
73
+ Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers)
74
+ end
75
+
76
+ def self.delete(url, headers={})
77
+ Request.execute(:method => :delete, :url => url, :headers => headers)
78
+ end
79
+
80
+ def self.head(url, headers={})
81
+ Request.execute(:method => :head, :url => url, :headers => headers)
82
+ end
83
+
84
+ class << self
85
+ attr_accessor :proxy
86
+ end
87
+
88
+ # Print log of RestClient calls. Value can be stdout, stderr, or a filename.
89
+ # You can also configure logging by the environment variable RESTCLIENT_LOG.
90
+ def self.log=(log)
91
+ @@log = log
92
+ end
93
+
94
+ def self.log # :nodoc:
95
+ return ENV['RESTCLIENT_LOG'] if ENV['RESTCLIENT_LOG']
96
+ return @@log if defined? @@log
97
+ nil
98
+ end
99
+ end
@@ -0,0 +1,66 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rest-client}
5
+ s.version = "1.0.4"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Adam Wiggins"]
9
+ s.date = %q{2009-07-29}
10
+ s.default_executable = %q{restclient}
11
+ s.description = %q{A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete.}
12
+ s.email = %q{adam@heroku.com}
13
+ s.executables = ["restclient"]
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ "README.rdoc",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "bin/restclient",
22
+ "lib/rest_client.rb",
23
+ "lib/restclient.rb",
24
+ "lib/restclient/exceptions.rb",
25
+ "lib/restclient/mixin/response.rb",
26
+ "lib/restclient/raw_response.rb",
27
+ "lib/restclient/request.rb",
28
+ "lib/restclient/resource.rb",
29
+ "lib/restclient/response.rb",
30
+ "spec/base.rb",
31
+ "spec/exceptions_spec.rb",
32
+ "spec/mixin/response_spec.rb",
33
+ "spec/raw_response_spec.rb",
34
+ "spec/request_spec.rb",
35
+ "spec/resource_spec.rb",
36
+ "spec/response_spec.rb",
37
+ "spec/restclient_spec.rb"
38
+ ]
39
+ s.has_rdoc = true
40
+ s.homepage = %q{http://rest-client.heroku.com/}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubyforge_project = %q{rest-client}
44
+ s.rubygems_version = %q{1.3.1}
45
+ s.summary = %q{Simple REST client for Ruby, inspired by microframework syntax for specifying actions.}
46
+ s.test_files = [
47
+ "spec/base.rb",
48
+ "spec/exceptions_spec.rb",
49
+ "spec/mixin/response_spec.rb",
50
+ "spec/raw_response_spec.rb",
51
+ "spec/request_spec.rb",
52
+ "spec/resource_spec.rb",
53
+ "spec/response_spec.rb",
54
+ "spec/restclient_spec.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 2
60
+
61
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
+ else
63
+ end
64
+ else
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/restclient'