Class: NightcrawlerSwift::Gateway

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

Constant Summary collapse

RETRY_BLACKLIST =
[
  RestClient::Unauthorized,
  RestClient::ResourceNotFound,
  RestClient::UnprocessableEntity
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Gateway

Returns a new instance of Gateway.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nightcrawler_swift/gateway.rb', line 12

def initialize url
  @url = url
  @attempts = 0
  @current_retry_time = 1
  @retries = NightcrawlerSwift.options.retries
  @max_retry_time = NightcrawlerSwift.options.max_retry_time

  @resource = RestClient::Resource.new(
    @url,
    {timeout: options.timeout}.merge(ssl_options)
  )
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



4
5
6
# File 'lib/nightcrawler_swift/gateway.rb', line 4

def attempts
  @attempts
end

#current_retry_timeObject (readonly)

Returns the value of attribute current_retry_time.



4
5
6
# File 'lib/nightcrawler_swift/gateway.rb', line 4

def current_retry_time
  @current_retry_time
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/nightcrawler_swift/gateway.rb', line 4

def resource
  @resource
end

Instance Method Details

#request(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nightcrawler_swift/gateway.rb', line 25

def request &block
  begin
    @attempts += 1
    block.call(resource)

  rescue => e
    raise e unless recoverable?(e)
    wait(e) and retry
  end

rescue RestClient::Unauthorized => e
  raise Exceptions::UnauthorizedError.new(e)

rescue RestClient::ResourceNotFound => e
  raise Exceptions::NotFoundError.new(e)

rescue RestClient::UnprocessableEntity => e
  raise Exceptions::ValidationError.new(e)

rescue => e
  raise Exceptions::ConnectionError.new(e)
end