class Lull

Helper class for making rest calls

Attributes

config[RW]
headers[RW]
meth[RW]
params[RW]
url[RW]

Public Class Methods

new(headers: {}, meth: 'Get', params: {}, url: 'https://test/', config: {}) click to toggle source
# File lib/lull.rb, line 9
def initialize(headers: {}, meth: 'Get', params: {}, url: 'https://test/', config: {})
  @headers = headers
  @meth    = meth.capitalize.to_sym
  @params  = params
  @url     = url
  @config  = config
  @config['timeout'] = @config['timeout'] ||= 30
end

Public Instance Methods

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

use rest-client with retry

# File lib/lull.rb, line 42
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/lull.rb, line 57
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/lull.rb, line 67
def responder(response)
  {
    'response' => JSON.parse(response.body),
    'status' => response.code.to_i
  }
end