class ADPDownloader::Downloader

Public Class Methods

new(http_client) click to toggle source
# File lib/adp-downloader/downloader.rb, line 6
def initialize(http_client)
  @http_client = http_client
end

Public Instance Methods

download_or_skip_statement(statement) click to toggle source
# File lib/adp-downloader/downloader.rb, line 38
def download_or_skip_statement(statement)
  if not downloaded? statement
    puts "Saving #{statement.date} - #{statement.id}..." unless Config.quiet?
    download_statement_files(statement)
  end
end
download_statement_files(statement) click to toggle source
# File lib/adp-downloader/downloader.rb, line 24
def download_statement_files(statement)
  if statement.json_uri
    json = _get_or_log(statement) if statement.json_uri
    _save(JSON.pretty_generate(statement.merge(json)), statement.json)
  end

  pdf = @http_client.download(full_url(statement.pdf_uri))
  _save(pdf, statement.pdf)
end
downloaded?(statement) click to toggle source
# File lib/adp-downloader/downloader.rb, line 34
def downloaded?(statement)
  File.exists? statement.pdf
end
full_url(path) click to toggle source
# File lib/adp-downloader/downloader.rb, line 10
def full_url(path)
  "#{BASE_URL}#{path}"
end
get_statements(type, statements) click to toggle source
# File lib/adp-downloader/downloader.rb, line 45
def get_statements(type, statements)
  puts "Downloading all #{type} statements..." unless Config.quiet?
  Parallel.each(statements) do |statement|
    download_or_skip_statement(statement)
  end
end
pay_statements() click to toggle source
# File lib/adp-downloader/downloader.rb, line 14
def pay_statements
  response = @http_client.get(full_url(PAY_STATEMENTS_PATH))
  response.fetch("payStatements", []).map { |json| PayStatement.new(json) }
end
tax_statements() click to toggle source
# File lib/adp-downloader/downloader.rb, line 19
def tax_statements
  response = @http_client.get(full_url(TAX_STATEMENTS_PATH))
  response.fetch("workerTaxStatements", []).map { |json| TaxStatement.new(json) }
end

Private Instance Methods

_get_or_log(statement) click to toggle source
# File lib/adp-downloader/downloader.rb, line 53
def _get_or_log(statement)
  begin
    @http_client.get(full_url(statement.json_uri))
  rescue
    puts "Error downloading statement: #{statement.date} - #{statement.id}"
    raise
  end
end
_save(contents, path) click to toggle source
# File lib/adp-downloader/downloader.rb, line 62
def _save(contents, path)
  FileUtils.mkpath(File.dirname(path))
  File.open(path, "w") { |f| f.write(contents) }
end