Class: Lull

Inherits:
Object
  • Object
show all
Defined in:
lib/lull.rb

Overview

Helper class for making rest calls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers: {}, meth: 'Get', params: {}, url: 'https://test/', config: {}) ⇒ Lull

Returns a new instance of Lull.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/lull.rb', line 7

def config
  @config
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/lull.rb', line 7

def headers
  @headers
end

#methObject

Returns the value of attribute meth.



7
8
9
# File 'lib/lull.rb', line 7

def meth
  @meth
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/lull.rb', line 7

def params
  @params
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/lull.rb', line 7

def url
  @url
end

Instance Method Details



30
31
32
33
34
35
36
37
38
39
# File 'lib/lull.rb', line 30

def cookie
  if @url =~ %r{auth/session}
    response = make_call
    raise 'There was an issue getting a cookie!' unless response.code == 200

    (response.cookies.map { |key, val| "#{key}=#{val}" })[0]
  else
    error_text('cookie', @url.to_s, 'auth/session')
  end
end

#make_callObject



18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

use rest-client with retry



42
43
44
45
46
47
48
49
50
51
52
53
# 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