Class: Rest
- Inherits:
-
Object
- Object
- Rest
- Defined in:
- lib/clouder/rest.rb
Class Method Summary collapse
- .copy(uri, destination) ⇒ Object
- .custom(method, uri, headers = {}) ⇒ Object
- .delete(uri, headers = {}) ⇒ Object
- .get(uri) ⇒ Object
- .head(uri, headers = {}) ⇒ Object
- .move(uri, destination) ⇒ Object
- .paramify_url(url, params = {}) ⇒ Object
- .parse(response, opts = {}) ⇒ Object
- .post(uri, doc = nil, headers = {}) ⇒ Object
-
.proxy(url) ⇒ Object
set proxy for RestClient to use.
- .put(uri, doc = nil, headers = {}) ⇒ Object
Class Method Details
.copy(uri, destination) ⇒ Object
29 30 31 |
# File 'lib/clouder/rest.rb', line 29 def copy uri, destination parse(RestClient.copy(uri, {'Destination' => destination})) end |
.custom(method, uri, headers = {}) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/clouder/rest.rb', line 41 def custom method, uri, headers = {} response = nil url = URI.parse(uri) Net::HTTP.start(url.host, url.port) { |http| response = http.send(method, url.path, headers) } response.to_hash end |
.delete(uri, headers = {}) ⇒ Object
25 26 27 |
# File 'lib/clouder/rest.rb', line 25 def delete uri, headers = {} parse(RestClient.delete(uri, headers)) end |
.get(uri) ⇒ Object
16 17 18 |
# File 'lib/clouder/rest.rb', line 16 def get uri parse(RestClient.get(uri), :max_nesting => false) end |
.head(uri, headers = {}) ⇒ Object
37 38 39 |
# File 'lib/clouder/rest.rb', line 37 def head uri, headers = {} custom(:head, uri, headers) end |
.move(uri, destination) ⇒ Object
33 34 35 |
# File 'lib/clouder/rest.rb', line 33 def move uri, destination parse(RestClient.move(uri, {'Destination' => destination})) end |
.paramify_url(url, params = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/clouder/rest.rb', line 59 def paramify_url url, params = {} if params && !params.empty? query = params.collect do |k,v| v = v.to_json if %w{key startkey endkey}.include?(k.to_s) "#{k}=#{CGI.escape(v.to_s)}" end.join("&") url = "#{url}?#{query}" end url end |
.parse(response, opts = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/clouder/rest.rb', line 50 def parse(response, opts = {}) if response json = JSON.parse(response.body, opts) json.extend(ResponseHeaders) json.headers = response.headers json end end |
.post(uri, doc = nil, headers = {}) ⇒ Object
20 21 22 23 |
# File 'lib/clouder/rest.rb', line 20 def post uri, doc = nil, headers = {} payload = doc.to_json if doc parse(RestClient.post(uri, payload, headers)) end |
.proxy(url) ⇒ Object
set proxy for RestClient to use
7 8 9 |
# File 'lib/clouder/rest.rb', line 7 def proxy url RestClient.proxy = url end |
.put(uri, doc = nil, headers = {}) ⇒ Object
11 12 13 14 |
# File 'lib/clouder/rest.rb', line 11 def put uri, doc = nil, headers = {} payload = doc.to_json if doc parse(RestClient.put(uri, payload, headers)) end |