Module: Komrade::HttpHelpers
- Extended by:
- HttpHelpers
- Included in:
- HttpHelpers, Queue
- Defined in:
- lib/komrade-client/http_helpers.rb
Constant Summary collapse
- MAX_RETRY =
4
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
- #http ⇒ Object
- #make_request(req, body = nil) ⇒ Object
- #post(path, body = nil) ⇒ Object
- #put(path, body = nil) ⇒ Object
Instance Method Details
#delete(path) ⇒ Object
23 24 25 |
# File 'lib/komrade-client/http_helpers.rb', line 23 def delete(path) make_request(Net::HTTP::Delete.new(path)) end |
#get(path) ⇒ Object
19 20 21 |
# File 'lib/komrade-client/http_helpers.rb', line 19 def get(path) make_request(Net::HTTP::Get.new(path)) end |
#http ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/komrade-client/http_helpers.rb', line 69 def http Net::HTTP.new(Komrade.url.host, Komrade.url.port).tap do |h| if Komrade.url.scheme == 'https' h.use_ssl = true h.verify_mode = OpenSSL::SSL::VERIFY_PEER end end end |
#make_request(req, body = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/komrade-client/http_helpers.rb', line 27 def make_request(req, body=nil) req.basic_auth(Komrade.url.user, Komrade.url.password) if body begin req.content_type = 'application/json' req.body = JSON.dump(body) rescue JSON => e raise(ArgumentError, "Komrade is unable to convert enqueue payload to JSON.\n" + "payload=#{body}\n") end end attempts = 0 while attempts < MAX_RETRY begin resp = nil RateLimiter.limiter(req.path, 10, 1) do resp = http.request(req) end if (Integer(resp.code) / 100) == 2 return JSON.parse(resp.body) end rescue Net::HTTPError => e next rescue RateLimiter::Unavailable sleep(0.5) ensure attempts += 1 end end case req.class when Net::HTTP::Delete raise(Komrade::Error, "Unable to delete work from Komrade.") when Net::HTTP::Put raise(Komrade::Error, "Unable to send work to Komrade.") when Net::HTTP::Get raise(Komrade::Error, "Unable to get work from Komrade.") default raise(Komrade::Error, "Unable to communicate with Komrade.") end end |
#post(path, body = nil) ⇒ Object
11 12 13 |
# File 'lib/komrade-client/http_helpers.rb', line 11 def post(path, body=nil) make_request(Net::HTTP::Post.new(path), body) end |
#put(path, body = nil) ⇒ Object
15 16 17 |
# File 'lib/komrade-client/http_helpers.rb', line 15 def put(path, body=nil) make_request(Net::HTTP::Put.new(path), body) end |