Class: QrForge::Payloads::Wifi

Inherits:
Object
  • Object
show all
Defined in:
lib/qr_forge/payloads/wifi.rb

Overview

Represents a Wi-Fi network payload

Examples:

returns WIFI:T:<encryption>;S:<ssid>;P:<password>;H:<hidden>;;


Constant Summary collapse

VALID_ENCRYPTIONS =
%w[WEP WPA WPA2 WPA3].freeze
NO_PASSWORD_ENCRYPTION =
"NOPASS"

Instance Method Summary collapse

Constructor Details

#initialize(encryption:, ssid:, password: nil, hidden: false) ⇒ Wifi



11
12
13
14
15
16
# File 'lib/qr_forge/payloads/wifi.rb', line 11

def initialize(encryption:, ssid:, password: nil, hidden: false)
  @encryption = encryption.upcase
  @ssid = ssid
  @password = password
  @hidden = hidden
end

Instance Method Details

#encryption_typeString?

Encryption type for the Wi-Fi network.



21
22
23
# File 'lib/qr_forge/payloads/wifi.rb', line 21

def encryption_type
  "T:#{@encryption}" unless @encryption == NO_PASSWORD_ENCRYPTION
end

#hiddenString?

Indicates if the Wi-Fi network is hidden



42
43
44
# File 'lib/qr_forge/payloads/wifi.rb', line 42

def hidden
  "H:true" unless @hidden.nil? || @hidden == false
end

#partsArray<String>

Returns the parts of the Wi-Fi payload as an array.



49
50
51
52
53
54
55
56
57
# File 'lib/qr_forge/payloads/wifi.rb', line 49

def parts
  parts = []
  parts << ssid
  parts << encryption_type
  parts << password
  parts << hidden

  parts.compact
end

#passwordString?

Password for the Wi-Fi network.



35
36
37
# File 'lib/qr_forge/payloads/wifi.rb', line 35

def password
  "P:#{escape(@password)}" if @password && @encryption != NO_PASSWORD_ENCRYPTION
end

#ssidString

SSID of the Wi-Fi network.



28
29
30
# File 'lib/qr_forge/payloads/wifi.rb', line 28

def ssid
  "S:#{escape(@ssid)}"
end

#to_sObject



59
60
61
# File 'lib/qr_forge/payloads/wifi.rb', line 59

def to_s
  "WIFI:#{parts.join(";")};;"
end

#validate!Object

Validates the Wi-Fi payload data.

Raises:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/qr_forge/payloads/wifi.rb', line 67

def validate!
  raise PayloadValidationError, "SSID is required" if blank?(@ssid)

  if @encryption != NO_PASSWORD_ENCRYPTION && blank?(@password)
    raise PayloadValidationError, "Password is required for #{@encryption} networks"
  end

  return if VALID_ENCRYPTIONS.include?(@encryption) || @encryption == NO_PASSWORD_ENCRYPTION

  raise PayloadValidationError, "Invalid encryption: #{@encryption}"
end