-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRegionEvent.swift
86 lines (76 loc) · 2.98 KB
/
RegionEvent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
81
82
83
84
85
86
//
// RegionEvent.swift
// IFTTTConnectSDK
//
// Copyright © 2021 IFTTT. All rights reserved.
//
import Foundation
/// A record of a region entry/exit event that occurs in the SDK.
public struct RegionEvent: Hashable {
private struct Key {
static let RecordId = "record_id"
static let RegionType = "region_type"
static let OccurredAt = "occurred_at"
static let EventType = "event_type"
static let ChannelId = "channel_id"
static let TriggerSubscriptionId = "trigger_subscription_id"
static let InstallationId = "installation_id"
}
private struct Constants {
static let Geo = "geo"
static let LocationServiceId = 941030000.description
}
/// The kind of event this is.
public let kind: RegionEventKind
/// The id of the event.
let recordId: UUID
/// The timestamp this event occurred at.
let occurredAt: Date
/// The trigger subscription id that this event corresponds to.
let triggerSubscriptionId: String
/// Creates an instance of `RegionEvent`.
///
/// - Parameters:
/// - recordId: The id of the recorded event.
/// - kind: The kind of region event.
/// - ocurredAt: The timestamp this event occurred at.
/// - triggerSubscriptionId: The trigger subscription id that this event corresponds to.
init(recordId: UUID = UUID(),
kind: RegionEventKind,
occurredAt: Date = Date(),
triggerSubscriptionId: String) {
self.recordId = recordId
self.kind = kind
self.occurredAt = occurredAt
self.triggerSubscriptionId = triggerSubscriptionId
}
/// Creates an optional instance of `RegionEvent`.
///
/// - Parameters:
/// - json: The `JSON` that should be used in creating the `RegionEvent`
init?(json: JSON) {
let parser = Parser(content: json)
guard let recordId = parser[Key.RecordId].uuid,
let ocurredAtString = parser[Key.OccurredAt].string,
let ocurredAtDate = APIDateFormatter.satellite.date(from: ocurredAtString),
let eventType = parser[Key.EventType].representation(of: RegionEventKind.self),
let triggerSubscriptionId = parser[Key.TriggerSubscriptionId].string else {
return nil
}
self.recordId = recordId
self.occurredAt = ocurredAtDate
self.kind = eventType
self.triggerSubscriptionId = triggerSubscriptionId
}
func toJSON(stripPrefix: Bool) -> JSON {
return [
Key.RecordId: recordId.uuidString,
Key.RegionType: Constants.Geo,
Key.OccurredAt: APIDateFormatter.satellite.string(from: occurredAt),
Key.EventType: kind.rawValue,
Key.ChannelId: Constants.LocationServiceId,
Key.TriggerSubscriptionId: stripPrefix ? triggerSubscriptionId.stripIFTTTPrefix(): triggerSubscriptionId,
Key.InstallationId: API.anonymousId
]
}
}