class Rockette::Rester

configurable rest-client calls with error handling and auto retries

Constants

VERBS

Public Class Methods

new(headers: {}, meth: "Get", params: {}, url: "https://array/", config: {}) click to toggle source
# File lib/rockette/rester.rb, line 13
def initialize(headers: {}, meth: "Get", params: {}, url: "https://array/", config: {})
  @headers = headers
  @meth    = meth
  @params  = params
  @url     = url
  @config = config
  @config["timeout"] = @config["timeout"] ||= 30
end

Public Instance Methods

make_call() click to toggle source
# File lib/rockette/rester.rb, line 22
def make_call
  response = RestClient::Request.execute(headers: @headers,
                                         method: VERBS[@meth.downcase], payload: @params,
                                         timeout: @config["timeout"], url: @url, verify_ssl: false)
rescue SocketError, IOError => e
  puts "#{e.class}: #{e.message}"
  nil
rescue StandardError => e
  e.response
else
  response
end
rest_try(tries = 3) click to toggle source

use rest-client with retry

# File lib/rockette/rester.rb, line 47
def rest_try(tries = 3)
  tries.times do |i|
    response = make_call
    unless response.nil?
      break response if (200..299).include? response.code
      break response if i > 1
    end
    puts "Failed #{@meth} on #{@url}, retry...#{i + 1}"
    sleep 3 unless i > 1
    return nil if i > 1 # Handles socket errors, etc. where there is no response.
  end
end

Private Instance Methods

error_text(method_name, url, wanted) click to toggle source
# File lib/rockette/rester.rb, line 62
def error_text(method_name, url, wanted)
  {
    "response" =>
      "ERROR: Wrong url for the #{method_name} method.\n"\
      "Sent: #{url}\n"\
      "Expected: \"#{wanted}\" as part of the url.",
    "status" => 400
  }
end
responder(response) click to toggle source
# File lib/rockette/rester.rb, line 72
def responder(response)
  {
    "response" => JSON.parse(response.body),
    "status" => response.code.to_i
  }
end