Class: Sunpass::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sunpass/client.rb

Constant Summary collapse

LOGIN_URL_DEFAULT =
'https://www.sunpass.com/vector/account/home/accountLogin.do'
TRANSACTIONS_URL_DEFAULT =
'https://www.sunpass.com/vector/account/transactions/webtransactionSearch.do'
TRANSPONDERS_URL_DEFAULT =
'https://www.sunpass.com/vector/account/transponders/tagsandvehiclesList.do'
USERNAME_SELECTORS =

These selectors are intentionally grouped so they are easy to adapt when SunPass changes markup.

[
  '#tt_username1'
].freeze
PASSWORD_SELECTORS =
[
  '#tt_loginPassword1'
].freeze
LOGIN_BUTTON_SELECTORS =
[
  'button:has-text("Log In")',
  'button:has-text("Login")',
  'input[value*="Login"]',
  'input[type="submit"]',
  'button[type="submit"]'
].freeze
TRANSACTION_ROW_SELECTORS =
[
  '#transactionItem > tbody > tr',
  '#transactionItem tbody tr',
  '#transactionTable tbody tr',
  'tr.footable-row',
  'tr[class*="footable"]',
  'tr[id*="transaction"]',
  'table tbody tr'
].freeze
TRANSPONDER_ROW_SELECTORS =
[
  '#resultTransponderid > tbody > tr',
  '#resultTransponderid tbody tr',
  '#transponderItem tbody tr',
  '#transponderTable tbody tr',
  '#vehicleTransponderTable tbody tr',
  'tr[id*="transponder"]',
  'tr[class*="transponder"]',
  '.transponder-item',
  '.transponder-card',
  'table tbody tr'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, headless: true, login_url: LOGIN_URL_DEFAULT, transactions_url: TRANSACTIONS_URL_DEFAULT, transponders_url: TRANSPONDERS_URL_DEFAULT, playwright_cli_executable_path: ENV.fetch('PLAYWRIGHT_CLI_EXECUTABLE_PATH', 'npx playwright'), transaction_parser: TransactionParser.new, transponder_parser: TransponderParser.new, logger: nil) ⇒ Client

Returns a new instance of Client.



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/sunpass/client.rb', line 53

def initialize(
  username:,
  password:,
  headless: true,
  login_url: LOGIN_URL_DEFAULT,
  transactions_url: TRANSACTIONS_URL_DEFAULT,
  transponders_url: TRANSPONDERS_URL_DEFAULT,
  playwright_cli_executable_path: ENV.fetch('PLAYWRIGHT_CLI_EXECUTABLE_PATH', 'npx playwright'),
  transaction_parser: TransactionParser.new,
  transponder_parser: TransponderParser.new,
  logger: nil
)
  @username = username
  @password = password
  @headless = headless
  @login_url = 
  @transactions_url = transactions_url
  @transponders_url = transponders_url
  @playwright_cli_executable_path = playwright_cli_executable_path
  @transaction_parser = transaction_parser
  @transponder_parser = transponder_parser
  @logger = logger
end

Instance Method Details

#fetch_transaction_rows(lookback_days: 30, from_date: nil, to_date: nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sunpass/client.rb', line 86

def fetch_transaction_rows(lookback_days: 30, from_date: nil, to_date: nil)
  log("Launching browser (headless=#{@headless})")
  Playwright.create(playwright_cli_executable_path: @playwright_cli_executable_path) do |playwright|
    browser = playwright.chromium.launch(headless: @headless)
    context = browser.new_context
    page = context.new_page

    (page)
    open_transactions(
      page,
      lookback_days: lookback_days,
      from_date: from_date,
      to_date: to_date
    )

    rows = find_transaction_row_texts(
      page,
      artifact_path: 'tmp/last_transactions_page.html'
    )
    log("Found #{rows.size} transaction row(s)")
    rows
  ensure
    log('Closing browser')
    browser&.close
  end
end

#fetch_transactions(lookback_days: 30, from_date: nil, to_date: nil) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/sunpass/client.rb', line 77

def fetch_transactions(lookback_days: 30, from_date: nil, to_date: nil)
  raw_rows = fetch_transaction_rows(
    lookback_days: lookback_days,
    from_date: from_date,
    to_date: to_date
  )
  @transaction_parser.parse_rows(raw_rows)
end

#fetch_transponder_records(transponders_url: @transponders_url) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sunpass/client.rb', line 143

def fetch_transponder_records(transponders_url: @transponders_url)
  log("Launching browser (headless=#{@headless})")
  Playwright.create(playwright_cli_executable_path: @playwright_cli_executable_path) do |playwright|
    browser = playwright.chromium.launch(headless: @headless)
    context = browser.new_context
    page = context.new_page

    (page)
    open_transponders(page, transponders_url: transponders_url)

    records = extract_transponder_records(page)
    log("Found #{records.size} structured transponder row(s)")
    records
  ensure
    log('Closing browser')
    browser&.close
  end
end

#fetch_transponder_rows(transponders_url: @transponders_url) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sunpass/client.rb', line 121

def fetch_transponder_rows(transponders_url: @transponders_url)
  log("Launching browser (headless=#{@headless})")
  Playwright.create(playwright_cli_executable_path: @playwright_cli_executable_path) do |playwright|
    browser = playwright.chromium.launch(headless: @headless)
    context = browser.new_context
    page = context.new_page

    (page)
    open_transponders(page, transponders_url: transponders_url)

    rows = find_transponder_row_texts(
      page,
      artifact_path: 'tmp/last_transponders_page.html'
    )
    log("Found #{rows.size} transponder row(s)")
    rows
  ensure
    log('Closing browser')
    browser&.close
  end
end

#fetch_transponders(transponders_url: @transponders_url) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/sunpass/client.rb', line 113

def fetch_transponders(transponders_url: @transponders_url)
  records = fetch_transponder_records(transponders_url: transponders_url)
  return @transponder_parser.parse_records(records) unless records.empty?

  raw_rows = fetch_transponder_rows(transponders_url: transponders_url)
  @transponder_parser.parse_rows(raw_rows)
end