Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
feat/Create Offer class
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseMonteiro committed May 15, 2024
1 parent 1726a29 commit d43ff3b
Show file tree
Hide file tree
Showing 20 changed files with 953 additions and 0 deletions.
33 changes: 33 additions & 0 deletions TikiClient/Classes/License/LicenseUse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation

/// License use
///
/// Define explicit uses for an asset. LicenseUses are extremely helpful in programmatic search
/// and enforcement of your LicenseRecords.
///
/// usecases explicitly define HOW an asset may be used. Use either our list of common enumerations
/// or define your own using LicenseUsecase.
///
/// destinations define WHO can use an asset. destinations narrow down usecases to a set of URLs,
/// categories of companies, or more. Use ECMAScript Regex to specify flexible and easily enforceable
/// rules.
public struct LicenseUse: Codable {
/// Usecases explicitly define HOW an asset may be used.
let usecases: [LicenseUsecase]

/// Destinations explicitly define WHERE an asset may be used.
/// Destinations can be: a wildcard URL (*.your-co.com),
/// a string defining a category of
let destinations: [String]?

/// Create an empty LicenseUse.
///
/// - Parameters:
/// - usecases: Usecases explicitly define HOW an asset may be used.
/// - destinations: Destinations explicitly define WHERE an asset may be used.
public init(usecases: [LicenseUsecase], destinations: [String]? = nil) {
self.usecases = usecases
self.destinations = destinations
}

}
57 changes: 57 additions & 0 deletions TikiClient/Classes/License/LicenseUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Foundation

/**
Use case for license.

- Parameter the license use case enumeration. Default value is nil.
*/
public class LicenseUsecase: Codable {

private var _value: String

/// The String value
public var value: String{
return _value
}

public init(_ value: String){
do {
let licenseUsecaseEnum = try LicenseUsecaseEnum.fromValue(value: value)
_value = licenseUsecaseEnum.rawValue
} catch {
_value = "custom:\(value)"
}
}

public init(_ licenseUsecaseEnum: LicenseUsecaseEnum){
_value = licenseUsecaseEnum.rawValue
}

required public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
var value: String = try container.decode(String.self)
if(value.hasPrefix("custom:")){
value = String(value.dropFirst("custom:".count))
}
do {
let licenseUsecaseEnum = try LicenseUsecaseEnum.fromValue(value: value)
_value = licenseUsecaseEnum.rawValue
} catch {
_value = "custom:\(value)"
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(value)
}

static let attribution = LicenseUsecase(LicenseUsecaseEnum.attribution)
static let retargeting = LicenseUsecase(LicenseUsecaseEnum.retargeting)
static let personalization = LicenseUsecase(LicenseUsecaseEnum.personalization)
static let aiTraining = LicenseUsecase(LicenseUsecaseEnum.aiTraining)
static let distribution = LicenseUsecase(LicenseUsecaseEnum.distribution)
static let analytics = LicenseUsecase(LicenseUsecaseEnum.analytics)
static let support = LicenseUsecase(LicenseUsecaseEnum.support)

}
39 changes: 39 additions & 0 deletions TikiClient/Classes/License/LicenseUseCaseEnum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

/**

Default accepted usecases
*/
public enum LicenseUsecaseEnum: String, Codable, CaseIterable {
case attribution = "attribution"
case retargeting = "retargeting"
case personalization = "personalization"
case aiTraining = "ai_training"
case distribution = "distribution"
case analytics = "analytics"
case support = "support"

/**
Returns the string value for the enum
*/
var value: String {
return self.rawValue
}

///
/// Builds a `LicenseUsecaseEnum` from `value`.
///
/// - Parameter value: string value of enum.
/// - Throws: `NSException` if `value` is not a valid `LicenseUsecaseEnum` value.
///
/// - Returns: `LicenseUsecase

static func fromValue(value: String) throws -> LicenseUsecaseEnum {
for type in LicenseUsecaseEnum.allCases {
if type.rawValue == value {
return type
}
}
throw NSError(domain: "Invalid LicenseUsecaseEnum value \(value)", code: 0, userInfo: nil)
}
}
22 changes: 22 additions & 0 deletions TikiClient/Classes/Rewards.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Rewards.swift
// TikiClient
//
// Created by Jesse Monteiro Ferreira on 14/05/24.
//

import Foundation

public class Rewards {
let virtualCurrency: String
let exclusiveAccess: String
let upgrades: String
let custom: String

public init(virtualCurrency: String, exclusiveAccess: String, upgrades: String, custom: String) {
self.virtualCurrency = virtualCurrency
self.exclusiveAccess = exclusiveAccess
self.upgrades = upgrades
self.custom = custom
}
}
81 changes: 81 additions & 0 deletions TikiClient/Classes/TitleTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Foundation


/// Title tag
///
/// Tags are included in the [TitleRecord] and describe the represented data asset.
/// Tags improve record searchability and come in handy when bulk searching and filtering licenses.
/// Use either our list of common enumerations or define your own using [customValue] as constructor
/// parameter.
public class TitleTag: Codable {

private var _value: String

/// The TitleTag String value
public var value: String{
return _value
}

public init(_ value: String){
do {
let titleTagEnum = try TitleTagEnum.fromValue(value: value)
_value = titleTagEnum.rawValue
} catch {
_value = "custom:\(value)"
}
}

public init(_ titleTagEnum: TitleTagEnum){
_value = titleTagEnum.rawValue
}

required public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
var value: String = try container.decode(String.self)
if(value.hasPrefix("custom:")){
value = String(value.dropFirst("custom:".count))
}
do {
let titleTagEnum = try TitleTagEnum.fromValue(value: value)
_value = titleTagEnum.rawValue
} catch {
_value = "custom:\(value)"
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(value)
}

public static let emailAddress = TitleTag(TitleTagEnum.emailAddress)
public static let phoneNumber = TitleTag(TitleTagEnum.phoneNumber)
public static let physicalAddress = TitleTag(TitleTagEnum.physicalAddress)
public static let contactInfo = TitleTag(TitleTagEnum.contactInfo)
public static let health = TitleTag(TitleTagEnum.health)
public static let fitness = TitleTag(TitleTagEnum.fitness)
public static let paymentInfo = TitleTag(TitleTagEnum.paymentInfo)
public static let creditInfo = TitleTag(TitleTagEnum.creditInfo)
public static let financialInfo = TitleTag(TitleTagEnum.financialInfo)
public static let preciseLocation = TitleTag(TitleTagEnum.preciseLocation)
public static let coarseLocation = TitleTag(TitleTagEnum.coarseLocation)
public static let sensitiveInfo = TitleTag(TitleTagEnum.sensitiveInfo)
public static let contacts = TitleTag(TitleTagEnum.contacts)
public static let messages = TitleTag(TitleTagEnum.messages)
public static let photoVideo = TitleTag(TitleTagEnum.photoVideo)
public static let audio = TitleTag(TitleTagEnum.audio)
public static let gameplayContent = TitleTag(TitleTagEnum.gameplayContent)
public static let customerSupport = TitleTag(TitleTagEnum.customerSupport)
public static let userContent = TitleTag(TitleTagEnum.userContent)
public static let browsingHistory = TitleTag(TitleTagEnum.browsingHistory)
public static let searchHistory = TitleTag(TitleTagEnum.searchHistory)
public static let userId = TitleTag(TitleTagEnum.userId)
public static let deviceId = TitleTag(TitleTagEnum.deviceId)
public static let purchaseHistory = TitleTag(TitleTagEnum.purchaseHistory)
public static let productInteraction = TitleTag(TitleTagEnum.productInteraction)
public static let advertisingData = TitleTag(TitleTagEnum.advertisingData)
public static let usageData = TitleTag(TitleTagEnum.usageData)
public static let crashData = TitleTag(TitleTagEnum.crashData)
public static let performanceData = TitleTag(TitleTagEnum.performanceData)
public static let diagnosticData = TitleTag(TitleTagEnum.diagnosticData)
}
52 changes: 52 additions & 0 deletions TikiClient/Classes/TitleTagEnum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Foundation

/**
* Default accepted tags.
*
* - value: The string value of the tag.
*/
public enum TitleTagEnum: String, Codable, CaseIterable {
case emailAddress = "email_address"
case phoneNumber = "phone_number"
case physicalAddress = "physical_address"
case contactInfo = "contact_info"
case health = "health"
case fitness = "fitness"
case paymentInfo = "payment_info"
case creditInfo = "credit_info"
case financialInfo = "financial_info"
case preciseLocation = "precise_location"
case coarseLocation = "coarse_location"
case sensitiveInfo = "sensitive_info"
case contacts = "contacts"
case messages = "messages"
case photoVideo = "photo_video"
case audio = "audio"
case gameplayContent = "gameplay_content"
case customerSupport = "customer_support"
case userContent = "user_content"
case browsingHistory = "browsing_history"
case searchHistory = "search_history"
case userId = "user_id"
case deviceId = "device_id"
case purchaseHistory = "purchase_history"
case productInteraction = "product_interaction"
case advertisingData = "advertising_data"
case usageData = "usage_data"
case crashData = "crash_data"
case performanceData = "performance_data"
case diagnosticData = "diagnostic_data"

/**
* Builds a [TitleTagEnum] from [value]
* @throws [IllegalArgumentException] if value is not a valid [TitleTagEnum] value
*/
public static func fromValue(value: String) throws -> TitleTagEnum {
for type in TitleTagEnum.allCases {
if type.rawValue == value {
return type
}
}
throw NSError(domain: "Invalid TitleTagEnum value \(value)", code: 0, userInfo: nil)
}
}
43 changes: 43 additions & 0 deletions TikiClient/Classes/Trail/LicenseRecord.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import Foundation

/// A `LicenseRecord` describes the terms under which a data asset may be used and MUST contain a reference to the
/// corresponding `TitleRecord`.
///
/// Learn more about `LicenseRecords` at https://docs.mytiki.com/docs/offer-customization.
public struct LicenseRecord {

/// This record's unique identifier.
public var id: String?

/// The `TitleRecord` associated with this license.
public var title: TitleRecord

/// A list of `Use` instances describing how an asset can be used.
public var uses: [Use]

/// The legal terms for the license.
public var terms: String

/// A human-readable description of the license.
public var description: String?

/// The date when the license expires.
public var expiry: Date?

public init?(from: RspLicense){
guard let titleRecord = TitleRecord(from: from.title!), from.id != nil, from.title != nil, from.uses != nil, from.terms != nil, from.title != nil else{
return nil
}
self.id = from.id
self.title = titleRecord
self.uses = from.uses!
self.terms = from.terms!
self.description = from.description
self.expiry = from.expiry
}
}
29 changes: 29 additions & 0 deletions TikiClient/Classes/Trail/PayableRecord.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import Foundation

public struct PayableRecord{
public let id: String
public let license: LicenseRecord
public let amount: String
public let type: String?
public let description: String?
public let expiry: Date?
public let reference: String?

public init?(from: RspPayable){
guard let licenseRecord = LicenseRecord(from: from.license!), from.id != nil, from.license != nil, from.amount != nil else{
return nil
}
id = from.id!
license = licenseRecord
amount = from.amount!
type = from.type
description = from.description
expiry = from.expiry
reference = from.reference
}
}
29 changes: 29 additions & 0 deletions TikiClient/Classes/Trail/Rsp/RspLicense.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import Foundation

public struct RspLicense: Rsp {
public let id: String?
public let title: RspTitle?
public let uses: [Use]?
public let terms: String?
public let description: String?
public let expiry: Date?
public let requestId: String

public init(from: [String : Any?]) {
self.requestId = RspLicense.nullToNil(value: from["requestId"] as? String ?? nil) != nil ? from["requestId"] as! String : ""
self.id = from["id"] as? String
self.title = from["title"] != nil ? RspTitle(from: from["title"] as! [String: Any?]) : nil
self.uses = (from["uses"] as! [[String: Any]]).map{ use in
Use(from: use)
} as [Use]
self.terms = from["terms"] as? String
self.description = from["description"] as? String
// self.expiry = RspLicense.nullToNil(value: from["expiry"] as? Int64) != nil ? Date(milliseconds: from["expiry"] as! Int64) : nil
self.expiry = nil
}
}
Loading

0 comments on commit d43ff3b

Please sign in to comment.