Class: SecureKeys::Swift::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/core/utils/swift/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(mapped_keys:, secure_key_bytes:) ⇒ Writer

Initialize the writer with the mapped keys and the secure key bytes

Parameters:

  • mapped_keys (Array<Hash>)

    The mapped keys

  • secure_key_bytes (Array<UInt8>)

    The secure key bytes



18
19
20
21
22
23
# File 'lib/core/utils/swift/writer.rb', line 18

def initialize(mapped_keys:, secure_key_bytes:)
  self.mapped_keys = mapped_keys
  self.secure_key_bytes = secure_key_bytes
  self.key_file = "#{SWIFT_PACKAGE_NAME}.swift"
  self.key_directory = "#{SWIFT_PACKAGE_DIRECTORY}/Sources/#{SWIFT_PACKAGE_NAME}"
end

Instance Method Details

#formatted_keysString

Generate the formatted keys content using Swift code format

Returns:

  • (String)

    The formatted keys content



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/core/utils/swift/writer.rb', line 33

def formatted_keys
  <<~SWIFT
  #{mapped_keys.map { |key| case_key_declaration_template(name: key[:name]) }.join("\t")}
      case unknown

      // MARK: - Properties

      /// The decrypted value of the key
      public var decryptedValue: String {
          switch self {
              #{mapped_keys.map { |key| switch_case_key_declaration_template(name: key[:name], value: key[:value], iv: key[:iv], tag: key[:tag]) }.join("\t\t")}
              case .unknown: fatalError("Unknown key \\(rawValue)")
          }
      }
  SWIFT
end

#key_swift_file_template(content:) ⇒ String

Generate the Swift file template

Parameters:

  • content (String)

    The content of the file

Returns:

  • (String)

    The Swift file template



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/core/utils/swift/writer.rb', line 53

def key_swift_file_template(content:)
  <<~SWIFT
  // swiftlint:disable all

  import Foundation
  import CryptoKit

  #{key_swift_global_helper_functions}

  // MARK: - SecureKey enum

  /// Keys is a class that contains all the keys that are used in the application.
  @available(iOS 13.0, *)
  public enum SecureKey: String {

      // MARK: - Cases

      #{content}
  }

  #{key_array_decrypt_swift_extension}

  #{key_string_swift_extension}

  // swiftlint:enable all
  SWIFT
end

#writeObject

Write the keys to the file



26
27
28
29
# File 'lib/core/utils/swift/writer.rb', line 26

def write
  # Write the file
  File.write("#{key_directory}/#{key_file}", key_swift_file_template(content: formatted_keys))
end