Class: Loquor::HttpAction

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

Direct Known Subclasses

Delete, Get, Post, Put

Defined Under Namespace

Classes: Delete, Get, Post, Put

Instance Method Summary collapse

Constructor Details

#initialize(url, deps) ⇒ HttpAction

Returns a new instance of HttpAction.



5
6
7
8
9
10
# File 'lib/loquor/http_action.rb', line 5

def initialize(url, deps)
  @url = url
  @config = deps[:config]
  @should_cache = deps[:should_cache]
  @retry_count = 0
end

Instance Method Details

#back_off(delay) ⇒ Object



42
43
44
# File 'lib/loquor/http_action.rb', line 42

def back_off(delay)
  sleep(delay)
end

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/loquor/http_action.rb', line 20

def execute
  @config.logger.info "Making HTTP request to: #{full_url}"
  signed_request.execute
rescue RestClient::ServiceUnavailable => e
  @config.logger.error("503 received for request to #{@url}.")
  @retry_count += 1
  if should_retry
    @config.logger.error("Retrying (retry attempt #{@retry_count})")
    back_off(@config.retry_backoff ** @retry_count)
    retry
  else
    @config.logger.error("Abandoning request (service unavailable)")
    raise e
  end
rescue RestClient::ResourceNotFound => e
  @config.logger.error("HTTP 404 when accessing #{full_url}")
  raise
rescue => e
  @config.logger.error("Exception while executing request: #{e.message} <#{e.class}>")
  raise
end

#signed_requestObject



12
13
14
15
16
17
18
# File 'lib/loquor/http_action.rb', line 12

def signed_request
  req = request
  @config.logger.info "Setting user-agent."
  req.headers['User-Agent'] = user_agent
  @config.logger.info "Signing request."
  ApiAuth.sign!(req, @config.access_id, @config.secret_key)
end