Class: PXMyPortal::Authentication

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

Instance Method Summary collapse

Constructor Details

#initialize(path:, user:, password:, token:, http:, logger:) ⇒ Authentication

Returns a new instance of Authentication.



21
22
23
24
25
26
27
28
29
30
# File 'lib/pxmyportal/authentication.rb', line 21

def initialize(path:, user:, password:, token:, http:, logger:)
  @path = path
  @user = user
  @password = password
  @token = token
  @http = http
  @logger = logger

  @request = Net::HTTP::Post.new(@path)
end

Instance Method Details

#postObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pxmyportal/authentication.rb', line 32

def post
  @http.provide_cookie(@request)

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

  @logger.debug("response") { response.to_hash }
  page = PXMyPortal::Page.from_path(response["location"] || @path)
  unless page
    @logger.error("location") { response["location"] }
    raise PXMyPortal::Error, "unexpected location"
  end
  @http.accept_cookie(response)
  page
end