Class: QrForge::Payload

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

Overview

Payload is a factory class that builds different types of payloads based on the provided type and data. It will validate the payload data (based on the type) and return a string representation of the payload.

Constant Summary collapse

PAYLOAD_TYPES =

TODO: Add passkey and sms support and vcard

{
  wifi: ::QrForge::Payloads::Wifi,
  plain: ::QrForge::Payloads::PlainText,
  url: ::QrForge::Payloads::Url,
  geo: ::QrForge::Payloads::Geo,
  phone: ::QrForge::Payloads::Phone
}.freeze

Class Method Summary collapse

Class Method Details

.build(type:, data:) ⇒ String

Builds a payload based on the type and data provided.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/qr_forge/payload.rb', line 22

def self.build(type:, data:)
  klass = PAYLOAD_TYPES[type.to_sym]

  raise ArgumentError "Invalid payload type: #{type}" unless klass

  payload = case data
            when Hash
              klass.new(**data)
            when String
              klass.new(data)
            else
              raise ArgumentError, "Invalid data type: #{data.class}. Expected Hash or String."
            end

  payload.validate!
  payload
end