Class: NightcrawlerSwift::Gateway
- Inherits:
-
Object
- Object
- NightcrawlerSwift::Gateway
- Defined in:
- lib/nightcrawler_swift/gateway.rb
Constant Summary collapse
- RETRY_BLACKLIST =
[ RestClient::Unauthorized, RestClient::ResourceNotFound, RestClient::UnprocessableEntity ]
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#current_retry_time ⇒ Object
readonly
Returns the value of attribute current_retry_time.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#initialize(url) ⇒ Gateway
constructor
A new instance of Gateway.
- #request(&block) ⇒ Object
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..retries @max_retry_time = NightcrawlerSwift..max_retry_time @resource = RestClient::Resource.new( @url, {timeout: .timeout}.merge() ) end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
4 5 6 |
# File 'lib/nightcrawler_swift/gateway.rb', line 4 def attempts @attempts end |
#current_retry_time ⇒ Object (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 |
#resource ⇒ Object (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 |