Module: Relaxo::Client
- Defined in:
- lib/relaxo/client.rb
Constant Summary collapse
- DEFAULT_GET_HEADERS =
{:accept => :json}
- DEFAULT_PUT_HEADERS =
{:accept => :json, :content_type => :json}
- @@debug =
false
Class Method Summary collapse
- .debug=(flag) ⇒ Object
- .delete(url) ⇒ Object
- .encode_parameters(parameters) ⇒ Object
- .encode_url(url, parameters = {}) ⇒ Object
- .escape(component) ⇒ Object
- .escape_id(id) ⇒ Object
- .execute(request) ⇒ Object
- .get(url) ⇒ Object
- .head(url) ⇒ Object
- .post(url, document) ⇒ Object
- .put(url, document) ⇒ Object
Class Method Details
.debug=(flag) ⇒ Object
29 30 31 |
# File 'lib/relaxo/client.rb', line 29 def self.debug= flag @@debug = flag end |
.delete(url) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/relaxo/client.rb', line 78 def self.delete(url) execute({ :method => :delete, :url => url, :headers => DEFAULT_PUT_HEADERS }) end |
.encode_parameters(parameters) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/relaxo/client.rb', line 90 def self.encode_parameters(parameters) query = [] parameters.each do |key, value| key_string = key.to_s if Symbol === value || key_string.end_with?("docid") query << escape(key_string) + '=' + escape(value.to_s) else query << escape(key_string) + '=' + escape(value.to_json) end end return '?' + query.join('&') end |
.encode_url(url, parameters = {}) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/relaxo/client.rb', line 106 def self.encode_url(url, parameters = {}) if parameters.empty? url else url + encode_parameters(parameters) end end |
.escape(component) ⇒ Object
86 87 88 |
# File 'lib/relaxo/client.rb', line 86 def self.escape(component) return CGI.escape(component) end |
.escape_id(id) ⇒ Object
114 115 116 |
# File 'lib/relaxo/client.rb', line 114 def self.escape_id(id) /^_design\/(.*)/ =~ id ? "_design/#{escape($1)}" : escape(id) end |
.execute(request) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/relaxo/client.rb', line 36 def self.execute(request) # Poor mans debugging: $stderr.puts "Relaxo Client: #{request[:method]} #{request[:url]}" if @@debug response = RestClient::Request.execute(request) return JSON.parse(response) end |
.get(url) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/relaxo/client.rb', line 44 def self.get(url) execute({ :method => :get, :url => url, :headers => DEFAULT_GET_HEADERS }) end |
.head(url) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/relaxo/client.rb', line 52 def self.head(url) execute({ :method => :head, :url => url, :headers => DEFAULT_GET_HEADERS }) end |
.post(url, document) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/relaxo/client.rb', line 69 def self.post(url, document) execute({ :method => :post, :url => url, :headers => DEFAULT_PUT_HEADERS, :payload => JSON.generate(document) }) end |
.put(url, document) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/relaxo/client.rb', line 60 def self.put(url, document) execute({ :method => :put, :url => url, :headers => DEFAULT_PUT_HEADERS, :payload => JSON.generate(document) }) end |