Class: Lull
- Inherits:
-
Object
- Object
- Lull
- Defined in:
- lib/lull.rb
Overview
Helper class for making rest calls
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#meth ⇒ Object
Returns the value of attribute meth.
-
#params ⇒ Object
Returns the value of attribute params.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #cookie ⇒ Object
-
#initialize(headers: {}, meth: 'Get', params: {}, url: 'https://test/', config: {}) ⇒ Lull
constructor
A new instance of Lull.
- #make_call ⇒ Object
-
#rest_try(tries = 3) ⇒ Object
use rest-client with retry.
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
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/lull.rb', line 7 def config @config end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/lull.rb', line 7 def headers @headers end |
#meth ⇒ Object
Returns the value of attribute meth.
7 8 9 |
# File 'lib/lull.rb', line 7 def meth @meth end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/lull.rb', line 7 def params @params end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/lull.rb', line 7 def url @url end |
Instance Method Details
#cookie ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lull.rb', line 30 def if @url =~ %r{auth/session} response = make_call raise 'There was an issue getting a cookie!' unless response.code == 200 (response..map { |key, val| "#{key}=#{val}" })[0] else error_text('cookie', @url.to_s, 'auth/session') end end |
#make_call ⇒ Object
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.}" 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 |