Class: PXMyPortal::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/pxmyportal/agent.rb

Instance Method Summary collapse

Instance Method Details

#let_redirectObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pxmyportal/agent.rb', line 27

def let_redirect
  token = request_verification_token
  request = Net::HTTP::Post.new(PXMyPortal::Page::BASEPATH)
  @cookie.provide(request, url: build_url(PXMyPortal::Page::BASEPATH))

  data = { LoginId: @user,
           Password: @password,
           "__RequestVerificationToken" => token }
  request.form_data = data
  response = http.request(request)
  begin
    response => Net::HTTPFound
  rescue => e
    File.write(File.join(PXMyPortal::XDG::CACHE_DIR, "debug", "let_redirect.html"),
               response.body)
    raise e
  end

  @page = PXMyPortal::Page.from_path(response["location"]) \
    or raise PXMyPortal::Error, "unexpected location #{location}"
  @cookie.accept(response, url: build_url(@page.path))
  self
end

#save_payslipsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pxmyportal/agent.rb', line 51

def save_payslips
  existing_payslips = (YAML.load_file(@payslips_path) rescue []) || []
  payslips.each do |payslip|
    if existing_payslips&.find { |candidate| payslip == candidate }
      @logger.info("skipping") { payslip }
      next
    end
    path = @page.confirm_path
    request = Net::HTTP::Post.new(path)
    @cookie.provide(request, url: build_url(path))
    request.form_data = payslip.form_data
    response = http.request(request)
    response => Net::HTTPOK

    FileUtils.mkdir_p(payslip.directory) unless File.directory?(payslip.directory)
    @logger.info("saving payslip...") { payslip.filename }
    File.write(payslip.filename, response.body) unless @test
    existing_payslips << payslip.
  end
  
  File.open(payslips_path, "w") { |file| YAML.dump(existing_payslips, file) } \
    unless @test

  self
end