Module: Locomotive::Coal::Resources::Concerns::Request

Included in:
Base, Locomotive::Coal::Resources::CurrentSite, EngineVersion, MyAccount, Token
Defined in:
lib/locomotive/coal/resources/concerns/request.rb

Instance Method Summary collapse

Instance Method Details

#delete(endpoint, parameters = {}, raw = false) ⇒ Object



18
19
20
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 18

def delete(endpoint, parameters = {}, raw = false)
  do_request :delete, endpoint, parameters, raw
end

#do_request(action, endpoint, parameters = {}, raw = false) ⇒ Object



22
23
24
25
26
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
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 22

def do_request(action, endpoint, parameters = {}, raw = false)
  max_count = 5
  response = begin
    # Just uncomment following line if you want to trace protocol between wagon & engine live
    # puts "#{parameters}"
    _do_request(action, "#{uri.path}/#{endpoint}.json", parameters)
  rescue ::Timeout::Error, ::Errno::ETIMEDOUT, Faraday::Error::TimeoutError => e
    if max_count > 0
      puts "\n Warning => timeout detected, attempts left: #{max_count}\n\n"
      max_count -= 1
      retry
    else
      raise Locomotive::Coal::TimeoutError.new(e)
    end
  rescue HTTPClient::KeepAliveDisconnected => e
    if max_count > 0
      puts "\n Warning => HTTPClient::KeepAliveDisconnected exception detected, attempts left: #{max_count}\n\n"
      max_count -= 1
      retry
    else
      puts "\n Error => HTTPClient::KeepAliveDisconnected exception detected, attempts left zero... raising exception\n\n"
      raise HTTPClient::KeepAliveDisconnected.new(e)
    end
  rescue Locomotive::Coal::Error
    raise
  rescue Exception => e
    raise Locomotive::Coal::BadRequestError.new(e)
  end

  if response.success?
    raw ? response : response.body
  else
    raise Locomotive::Coal::Error.from_response(response)
  end
end

#get(endpoint, parameters = {}, raw = false) ⇒ Object



6
7
8
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 6

def get(endpoint, parameters = {}, raw = false)
  do_request :get, endpoint, parameters, raw
end

#post(endpoint, parameters = {}, raw = false) ⇒ Object



10
11
12
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 10

def post(endpoint, parameters = {}, raw = false)
  do_request :post, endpoint, parameters, raw
end

#put(endpoint, parameters = {}, raw = false) ⇒ Object



14
15
16
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 14

def put(endpoint, parameters = {}, raw = false)
  do_request :put, endpoint, parameters, raw
end

#without_authentication(&block) ⇒ Object



58
59
60
61
62
63
# File 'lib/locomotive/coal/resources/concerns/request.rb', line 58

def without_authentication(&block)
  @without_token = true
  yield.tap do
    @without_token = false
  end
end