Module: Genesis::RetryingFetcher

Defined in:
lib/retryingfetcher.rb

Constant Summary collapse

FETCH_RETRY_INTERVALS =
[0,1,5,30,60,90,180,300,300,300,300,300,1800,3600]

Class Method Summary collapse

Class Method Details

.get(what, base_url = '', fetch_intervals = FETCH_RETRY_INTERVALS) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/retryingfetcher.rb', line 7

def self.get what, base_url = '', fetch_intervals = FETCH_RETRY_INTERVALS
  what = File.join(base_url, what) unless base_url.empty?
  fetch_intervals.each_with_index do |sleep_interval, index|
    Kernel.sleep(sleep_interval) 
    puts "Fetching '%s' (Attempt #%d)..." % [what, index+1]
  
    begin 
      response = HTTParty.get(what)
      next unless response.code == 200
      yield response.body if block_given?
      return response.body
    rescue  => e
      puts "RetyingFetcher.get error: %s" % e.message
    end
  end
  nil
end