Module: WifiWand::CaptivePortalProbeHelper
- Extended by:
- StringPredicates
- Defined in:
- lib/wifi_wand/services/captive_portal_probe_helper.rb
Overview
Encapsulates the argument-parsing and probe-execution logic for the captive portal helper script. Keeping this logic in a module that can be +require+d directly allows the spec suite (and SimpleCov) to exercise the code without spawning a subprocess, while the thin CLI entry-point at the bottom of this file preserves the existing runtime contract.
Class Method Summary collapse
-
.parse_argv(argv) ⇒ Hash
Parses command-line arguments into an endpoint hash.
-
.run(argv, output: $stdout, checker: nil) ⇒ Object
Parses
argv, performs a captive portal probe, and writes a JSON result hash tooutput.
Methods included from StringPredicates
string_nil_or_blank?, string_nil_or_empty?
Class Method Details
.parse_argv(argv) ⇒ Hash
Parses command-line arguments into an endpoint hash.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/wifi_wand/services/captive_portal_probe_helper.rb', line 21 def self.parse_argv(argv) url, expected_code_arg, expected_body = argv raise ArgumentError, 'url argument is required' if string_nil_or_blank?(url) if string_nil_or_blank?(expected_code_arg) raise ArgumentError, 'expected_code argument is required' end { url: url, expected_code: Integer(expected_code_arg), expected_body: string_nil_or_blank?(expected_body) ? nil : expected_body, } end |
.run(argv, output: $stdout, checker: nil) ⇒ Object
Parses argv, performs a captive portal probe, and writes a JSON result
hash to output. Any ArgumentError raised during argument parsing is
caught and serialised as an unknown result so the parent process
always receives valid JSON.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wifi_wand/services/captive_portal_probe_helper.rb', line 46 def self.run(argv, output: $stdout, checker: nil) endpoint = parse_argv(argv) checker ||= CaptivePortalChecker.new(verbose: false, output: $stderr) result = checker.probe_endpoint(endpoint) output.print(JSON.generate(result)) output.flush rescue ArgumentError => e output.print(JSON.generate( { login_required: 'unknown', error_class: e.class.to_s, error_message: e. })) output.flush end |