Class: ADPDownloader::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/adp-downloader/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ Downloader

Returns a new instance of Downloader.



6
7
8
# File 'lib/adp-downloader/downloader.rb', line 6

def initialize(http_client)
  @http_client = http_client
end

Instance Method Details

#download_or_skip_statement(statement) ⇒ Object



38
39
40
41
42
43
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/adp-downloader/downloader.rb', line 34

def downloaded?(statement)
  File.exists? statement.pdf
end

#full_url(path) ⇒ Object



10
11
12
# File 'lib/adp-downloader/downloader.rb', line 10

def full_url(path)
  "#{BASE_URL}#{path}"
end

#get_statements(type, statements) ⇒ Object



45
46
47
48
49
50
# 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_statementsObject



14
15
16
17
# 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_statementsObject



19
20
21
22
# 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