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

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.

Parameters:

  • argv (Array<String>)

    positional args: url, expected_code, optional expected_body

Returns:

  • (Hash)

    with :url (String), :expected_code (Integer), :expected_body (String|nil)

Raises:

  • (ArgumentError)

    when url or expected_code are absent or expected_code is non-numeric



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.

Parameters:

  • argv (Array<String>)
  • output (IO) (defaults to: $stdout)

    destination for the JSON result (default: $stdout)

  • checker (CaptivePortalChecker, nil) (defaults to: nil)

    optional pre-built checker; a fresh one is created when nil (useful for injecting test doubles)



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.message }))
  output.flush
end