Class: Backend::Connection
- Inherits:
-
Object
- Object
- Backend::Connection
- Defined in:
- connection.rb
Overview
Class that holds basic HTTP methods for connecting to the backend
Class Method Summary collapse
- .build_query_from_hash(hash, key_list = nil) ⇒ Object
- .delete(path, in_headers = {}) ⇒ Object
- .get(path, in_headers = {}) ⇒ Object
- .post(path, data = nil, in_headers = {}) ⇒ Object
- .put(path, data, in_headers = {}) ⇒ Object
Class Method Details
.build_query_from_hash(hash, key_list = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'connection.rb', line 59 def self.build_query_from_hash(hash, key_list = nil) key_list ||= hash.keys query = key_list.map do |key| next unless hash.key?(key) str = hash[key].to_s str.toutf8 if hash[key].nil? # just a boolean argument ? [hash[key]].flat_map { key }.join('&') else [hash[key]].flat_map { "#{key}=#{CGI.escape(hash[key].to_s)}" }.join('&') end end query.empty? ? '' : "?#{query.compact.join('&')}" end |
.delete(path, in_headers = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'connection.rb', line 45 def self.delete(path, in_headers = {}) Backend::Test.start start_time = Time.now Rails.logger.debug "[backend] DELETE: #{path}" timeout = in_headers.delete('Timeout') || 1000 backend_request = Net::HTTP::Delete.new(path, in_headers) response = Net::HTTP.start(host, port) do |http| http.read_timeout = timeout http.request(backend_request) end Backend::Logger.info('DELETE', host, port, path, response, start_time) handle_response(response) end |
.get(path, in_headers = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'connection.rb', line 12 def self.get(path, in_headers = {}) Backend::Test.start start_time = Time.now Rails.logger.debug "[backend] GET: #{path}" timeout = in_headers.delete('Timeout') || 1000 backend_request = Net::HTTP::Get.new(path, in_headers) response = Net::HTTP.start(host, port) do |http| http.read_timeout = timeout if block_given? http.request(backend_request) do |backend_response| yield(backend_response) end else http.request(backend_request) end end Backend::Logger.info('GET', host, port, path, response, start_time) handle_response(response) end |
.post(path, data = nil, in_headers = {}) ⇒ Object
38 39 40 41 42 43 |
# File 'connection.rb', line 38 def self.post(path, data = nil, in_headers = {}) in_headers = { 'Content-Type' => 'application/octet-stream' }.merge in_headers put_or_post('POST', path, data, in_headers) end |
.put(path, data, in_headers = {}) ⇒ Object
34 35 36 |
# File 'connection.rb', line 34 def self.put(path, data, in_headers = {}) put_or_post('PUT', path, data, in_headers) end |