Class: HttpJob
- Inherits:
-
Object
- Object
- HttpJob
- Defined in:
- lib/piscina/http_job.rb
Constant Summary collapse
- HTTP_DELETE =
"DELETE"
- HTTP_GET =
"GET"
- HTTP_POST =
"POST"
- HTTP_PUT =
"PUT"
- HTTP_METHODS =
[ HTTP_DELETE, HTTP_GET, HTTP_POST, HTTP_PUT ]
Class Method Summary collapse
- .create_request(method, uri, headers, body) ⇒ Object
- .get_response(req, uri) ⇒ Object
- .perform(msg) ⇒ Object
Class Method Details
.create_request(method, uri, headers, body) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/piscina/http_job.rb', line 38 def self.create_request(method, uri, headers, body) raise "http_method or uri not defined" unless method && uri raise "#{method} is not a valid HTTP method" unless HTTP_METHODS.include?(method) begin uri_obj = URI.parse(uri) rescue => e raise "There was an error parsing #{uri}. ErrorMessage: #{e.}" end req = "Net::HTTP::#{method.capitalize}".constantize.new(uri_obj.request_uri) if headers headers.each do |header, val| req[header] = val end end if [HTTP_PUT, HTTP_POST].include?(method) && body req.body = body end req end |
.get_response(req, uri) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/piscina/http_job.rb', line 63 def self.get_response(req, uri) uri = URI.parse(uri) Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| http.request(req) end end |
.perform(msg) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/piscina/http_job.rb', line 17 def self.perform(msg) params = JSON.parse(msg.body) method = params["http_method"] uri = params["uri"] headers = params["headers"] body = params["body"] req = HttpJob.create_request(method, uri, headers, body) res = HttpJob.get_response(req, uri) # Net::HTTPSuccess covers all 2XX responses unless res.is_a?(Net::HTTPSuccess) # Messages not explicitly deleted will be placed back into the queue. DLQ policies will take # effect if a message cannot be processed a number of times. raise "Could not perform request to:#{uri.to_s}" end msg.delete end |