Class: Shoptet::Request
- Inherits:
-
Object
- Object
- Shoptet::Request
- Defined in:
- lib/shoptet/request.rb
Class Method Summary collapse
- .delete(url, headers) ⇒ Object
- .get(url, headers, logger = nil) ⇒ Object
- .patch(url, body, headers) ⇒ Object
- .post(url, body) ⇒ Object
- .post_binary(url, body, headers) ⇒ Object
- .post_json(url, body, headers) ⇒ Object
Class Method Details
.delete(url, headers) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/shoptet/request.rb', line 101 def self.delete(url, headers) request = Net::HTTP::Delete.new(url) headers.each do |key, value| request[key] = value end response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| handle_connection_errors { http.request(request) } end JSON.parse(response.body) end |
.get(url, headers, logger = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/shoptet/request.rb', line 6 def self.get(url, headers, logger = nil) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.open_timeout = 30 http.read_timeout = 30 http.write_timeout = 30 http.ssl_timeout = 30 request = Net::HTTP::Get.new(url) headers.each do |key, value| request[key] = value end if logger logger.debug("GET #{url}") end response = handle_connection_errors { http.request(request) } body = response.body if logger logger.debug("RESPONSE #{body}") end parsed_body = JSON.parse(body) unless parsed_body = "Status code: #{response.code}, url: #{url}" fail Shoptet::EmptyResponse.new() end parsed_body end |
.patch(url, body, headers) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/shoptet/request.rb', line 84 def self.patch(url, body, headers) request = Net::HTTP::Patch.new(url) request.body = body.to_json headers.each do |key, value| request[key] = value end response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| handle_connection_errors { http.request(request) } end JSON.parse(response.body) rescue JSON::ParserError => e = "Original message: #{e.}, response body: #{response.body}, code :#{response.code}, headers: #{response.to_hash.to_s}" fail Shoptet::NonJSONResponse.new() end |
.post(url, body) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/shoptet/request.rb', line 38 def self.post(url, body) request = Net::HTTP::Post.new(url) request.set_form_data(body) response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| handle_connection_errors { http.request(request) } end JSON.parse(response.body) rescue JSON::ParserError => e = "Original message: #{e.}, response body: #{response.body}, code :#{response.code}, headers: #{response.to_hash.to_s}" fail Shoptet::NonJSONResponse.new() end |
.post_binary(url, body, headers) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/shoptet/request.rb', line 52 def self.post_binary(url, body, headers) request = Net::HTTP::Post.new(url) request.body = body request.content_type = 'appliaction/octet-stream' headers.each do |key, value| request[key] = value end response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| handle_connection_errors { http.request(request) } end JSON.parse(response.body) end |
.post_json(url, body, headers) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/shoptet/request.rb', line 67 def self.post_json(url, body, headers) request = Net::HTTP::Post.new(url) request.body = body.to_json headers.each do |key, value| request[key] = value end response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| handle_connection_errors { http.request(request) } end JSON.parse(response.body) rescue JSON::ParserError => e = "Original message: #{e.}, response body: #{response.body}, code :#{response.code}, headers: #{response.to_hash.to_s}" fail Shoptet::NonJSONResponse.new() end |