Skip to content

Commit 0d85505

Browse files
committed
Updating feature parsing to handle more than one feature
1 parent 23eb959 commit 0d85505

5 files changed

Lines changed: 217 additions & 12 deletions

File tree

IFTTT SDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
DE328DC2243E1DED00603EAC /* LocationMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE328DC1243E1DED00603EAC /* LocationMonitor.swift */; };
8080
DE328DC8243E2FC300603EAC /* SynchronizationTriggerEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE328DC7243E2FC300603EAC /* SynchronizationTriggerEvent.swift */; };
8181
DE37620022DF75CB00F8BD38 /* ConnectionDeeplinkAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE3761FF22DF75CB00F8BD38 /* ConnectionDeeplinkAction.swift */; };
82+
DE3D325929944120001FF335 /* fetch_multiple_feature_fields_connection_response.json in Resources */ = {isa = PBXBuildFile; fileRef = DE3D325829943CA4001FF335 /* fetch_multiple_feature_fields_connection_response.json */; };
8283
DE4A4C5E243632A7004082BF /* SynchronizationSubscriber.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE4A4C5D243632A7004082BF /* SynchronizationSubscriber.swift */; };
8384
DE4A4C602436340C004082BF /* SynchronizationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE4A4C5F2436340C004082BF /* SynchronizationManager.swift */; };
8485
DE4A4C622436409C004082BF /* SynchronizationScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE4A4C612436409C004082BF /* SynchronizationScheduler.swift */; };
@@ -265,6 +266,7 @@
265266
DE328DCB2440CEA500603EAC /* EventPublisherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventPublisherTests.swift; sourceTree = "<group>"; };
266267
DE328DCD2440E89600603EAC /* ConnectionsRegistryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionsRegistryTests.swift; sourceTree = "<group>"; };
267268
DE3761FF22DF75CB00F8BD38 /* ConnectionDeeplinkAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionDeeplinkAction.swift; sourceTree = "<group>"; };
269+
DE3D325829943CA4001FF335 /* fetch_multiple_feature_fields_connection_response.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = fetch_multiple_feature_fields_connection_response.json; sourceTree = "<group>"; };
268270
DE4A4C5D243632A7004082BF /* SynchronizationSubscriber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizationSubscriber.swift; sourceTree = "<group>"; };
269271
DE4A4C5F2436340C004082BF /* SynchronizationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizationManager.swift; sourceTree = "<group>"; };
270272
DE4A4C612436409C004082BF /* SynchronizationScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizationScheduler.swift; sourceTree = "<group>"; };
@@ -475,6 +477,7 @@
475477
FC1F491322710B62008A1B21 /* Connection_ParsingTests.swift */,
476478
1D6E554521CBF4F200FE66F0 /* IFTTT SDKTests-Bridging-Header.h */,
477479
FC1F491522710BE4008A1B21 /* fetch_connection_response.json */,
480+
DE3D325829943CA4001FF335 /* fetch_multiple_feature_fields_connection_response.json */,
478481
DE328DC92440CD7900603EAC /* CLRegion+Parsing_spec.swift */,
479482
DE328DCB2440CEA500603EAC /* EventPublisherTests.swift */,
480483
DE328DCD2440E89600603EAC /* ConnectionsRegistryTests.swift */,
@@ -848,6 +851,7 @@
848851
buildActionMask = 2147483647;
849852
files = (
850853
DEC29F1525841A5200BF56EE /* fetch_connection_response.json in Resources */,
854+
DE3D325929944120001FF335 /* fetch_multiple_feature_fields_connection_response.json in Resources */,
851855
);
852856
runOnlyForDeploymentPostprocessing = 0;
853857
};

IFTTT SDK/Connection+Parsing.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99

1010
extension Connection {
11+
1112
init?(parser: Parser) {
1213
guard
1314
let id = parser["id"].string,
@@ -112,10 +113,14 @@ private extension Connection.Feature {
112113

113114
self.triggers = Set(parser["feature_triggers"].compactMap { innerParser -> Trigger? in
114115
let fieldsParser = innerParser["fields"]
116+
let foundParser = fieldsParser.first(where: { parser -> Bool in
117+
guard let id = parser["id"].string else { return false }
118+
return Trigger.supportedTriggerId(id)
119+
})
115120
guard let id = innerParser["id"].string,
116-
let firstField = fieldsParser.first else { return nil }
121+
let foundParser = foundParser else { return nil }
117122

118-
return Trigger(defaultFieldParser: firstField, triggerId: id)
123+
return Trigger(defaultFieldParser: foundParser, triggerId: id)
119124
})
120125
}
121126
}

IFTTT SDK/NativeServices.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public enum Trigger: Hashable {
9191
]
9292
}
9393
}
94+
95+
static func supportedTriggerId(_ triggerId: String) -> Bool {
96+
switch triggerId {
97+
case Constants.LocationIdentifer: return true
98+
default: return false
99+
}
100+
}
94101

95102
public func hash(into hasher: inout Hasher) {
96103
hasher.combine(identifier)

SDKHostAppTests/Connection_ParsingTests.swift

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@
88
import XCTest
99
@testable import IFTTTConnectSDK
1010

11+
extension Bundle {
12+
func fetchConnection(fromJSON atPath: String) -> Connection? {
13+
guard let path = url(forResource: atPath, withExtension: "json"),
14+
let json = try? Data(contentsOf: path) else { return nil }
15+
16+
let parser = Parser(content: json)
17+
return Connection(parser: parser)
18+
}
19+
}
20+
1121
class Connection_ParsingTests: XCTestCase {
1222

1323
var connection: Connection!
14-
24+
var multipleFeatureFieldsConnection: Connection!
25+
1526
override func setUp() {
1627
#if SWIFT_PACKAGE
1728
let bundle = Bundle.module
1829
#else
1930
let bundle = Bundle(for: Connection_ParsingTests.self)
2031
#endif
21-
if let path = bundle.url(forResource: "fetch_connection_response",
22-
withExtension: "json"),
23-
let json = try? Data(contentsOf: path) {
24-
let parser = Parser(content: json)
25-
connection = Connection(parser: parser)
26-
}
32+
connection = bundle.fetchConnection(fromJSON: "fetch_connection_response")
33+
multipleFeatureFieldsConnection = bundle.fetchConnection(fromJSON: "fetch_multiple_feature_fields_connection_response")
2734
}
2835

2936
func test_fetchConnectionResponse() {
@@ -87,8 +94,33 @@ class Connection_ParsingTests: XCTestCase {
8794
assert(testMisspelledIFTTTIdentifier.stripIFTTTPrefix() == testMisspelledIFTTTIdentifier)
8895
}
8996

90-
func test_addIFTTTPrefix() {
91-
let testIdentifier = "1234"
92-
assert(testIdentifier.addIFTTTPrefix() == "ifttt_1234")
97+
func test_fetchMultipleFeatureFieldsConnectionResponse() {
98+
XCTAssertNotNil(multipleFeatureFieldsConnection)
99+
if multipleFeatureFieldsConnection == nil {
100+
return
101+
}
102+
103+
let firstTrigger = multipleFeatureFieldsConnection.activeUserTriggers.first
104+
switch firstTrigger {
105+
case .location(let region):
106+
XCTAssertEqual(region.radius, 349.2418372528667)
107+
XCTAssertEqual(region.center.latitude, 37.78338859999999)
108+
XCTAssertEqual(region.center.longitude, -122.408433)
109+
XCTAssertEqual(region.identifier, "ifttt_b0fe2ba5-ff2e-4c4b-b63a-8f65c7f90c48")
110+
default:
111+
XCTFail("Expecting a location trigger")
112+
}
113+
114+
let storage = Connection.ConnectionStorage(connection: multipleFeatureFieldsConnection)
115+
XCTAssertEqual(storage.hasLocationTriggers, true)
116+
117+
if let firstRegion = storage.locationRegions.first {
118+
XCTAssertEqual(firstRegion.radius, 349.2418372528667)
119+
XCTAssertEqual(firstRegion.center.latitude, 37.78338859999999)
120+
XCTAssertEqual(firstRegion.center.longitude, -122.408433)
121+
XCTAssertEqual(firstRegion.identifier, "ifttt_b0fe2ba5-ff2e-4c4b-b63a-8f65c7f90c48")
122+
} else {
123+
XCTFail("Expecting a region to be returned")
124+
}
93125
}
94126
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"type": "connection",
3+
"id": "aMozvjdb",
4+
"name": "Deliver grocery when any member enters an area",
5+
"description": "Set a location and good stuff will be delivered there",
6+
"url": "https://ifttt.com/applets/aMozvjdb",
7+
"enabled_count": 2,
8+
"user_status": "enabled",
9+
"services": [{
10+
"service_id": "grocery_express",
11+
"service_name": "Grocery Express",
12+
"service_short_name": "Grocery Expr",
13+
"is_primary": true,
14+
"monochrome_icon_url": "https://assets.ifttt.com/images/channels/1059449428/icons/monochrome_regular.png",
15+
"color_icon_url": "https://assets.ifttt.com/images/channels/1059449428/icons/on_color_regular.png",
16+
"brand_color": "#c8c8c8",
17+
"url": "https://ifttt.com/grocery_express",
18+
"triggers": [],
19+
"queries": [],
20+
"actions": []
21+
}, {
22+
"service_id": "g_location",
23+
"service_name": "G Location",
24+
"service_short_name": "G Loc.",
25+
"is_primary": false,
26+
"monochrome_icon_url": "https://assets.ifttt.com/images/channels/default/icons/monochrome_regular.png",
27+
"color_icon_url": "https://assets.ifttt.com/images/channels/default/icons/on_color_regular.png",
28+
"brand_color": "#ff9f1c",
29+
"url": "https://ifttt.com/g_location",
30+
"triggers": [{
31+
"id": "any_member_enters_an_area",
32+
"label": "Any member enters an area",
33+
"field_options_url": "https://connect.ifttt.com/v2/connections/aMozvjdb/triggers/g_location.any_member_enters_an_area/field_options",
34+
"run_url": null,
35+
"fields": [{
36+
"id": "group_guid",
37+
"label": "Which Group?",
38+
"type": "COLLECTION_SELECT",
39+
"required": true,
40+
"hidden": false,
41+
"default_value": null
42+
}, {
43+
"id": "location",
44+
"label": "Locate an area",
45+
"type": "LOCATION_ENTER",
46+
"required": true,
47+
"hidden": false,
48+
"default_value": {
49+
"latitude": "37.78338859999999",
50+
"longitude": "-122.408433",
51+
"radius": "366.4021942042208",
52+
"address": "923 Market St, San Francisco, CA 94103, USA",
53+
"description": "923 Market St, San Francisco, CA 94103, USA",
54+
"zoom": "16"
55+
}
56+
}],
57+
"user_triggers": [{
58+
"run_url": null,
59+
"fields": [{
60+
"id": "group_guid",
61+
"label": "My_first_group",
62+
"group": null,
63+
"value": "gAiXzykdSNyPEAAOAAAAwmRhYTYxMmEzMjUwZjkzZGJNeV9maXJzdF9ncm91cA"
64+
}, {
65+
"id": "location",
66+
"value": {
67+
"lat": 37.78338859999999,
68+
"lng": -122.408433,
69+
"radius": 349.2418372528667,
70+
"address": "923 Market St, San Francisco, CA 94103, USA",
71+
"description": "923 Market St, San Francisco, CA 94103, USA",
72+
"zoom": 16
73+
}
74+
}]
75+
}]
76+
}],
77+
"queries": [],
78+
"actions": []
79+
}],
80+
"cover_image": null,
81+
"value_propositions": [],
82+
"features": [{
83+
"id": "a9wzuevc23",
84+
"title": "Start when any member enters an area",
85+
"description": "Set an area and the grocery will be delivered when any member enters that area.",
86+
"icon_url": "https://ifttt.com/value-prop-icons/gps.png",
87+
"field_options_url": "https://connect.ifttt.com/v2/connections/aMozvjdb/features/a9wzuevc23/field_options",
88+
"fields": [],
89+
"feature_triggers": [{
90+
"id": "pc7a2p4lbn",
91+
"trigger_id": "any_member_enters_an_area",
92+
"service_id": "g_location",
93+
"label": "Any member enters an area",
94+
"field_options_url": "https://connect.ifttt.com/v2/connections/aMozvjdb/features/a9wzuevc23/triggers/g_location.any_member_enters_an_area/field_options",
95+
"run_url": null,
96+
"fields": [{
97+
"id": "group_guid",
98+
"label": "Which Group?",
99+
"type": "COLLECTION_SELECT",
100+
"required": true,
101+
"hidden": false,
102+
"default_value": null
103+
}, {
104+
"id": "location",
105+
"label": "Locate an area",
106+
"type": "LOCATION_ENTER",
107+
"required": true,
108+
"hidden": false,
109+
"default_value": {
110+
"lat": 37.78338859999999,
111+
"lng": -122.408433,
112+
"radius": 366.4021942042208,
113+
"address": "923 Market St, San Francisco, CA 94103, USA",
114+
"description": "923 Market St, San Francisco, CA 94103, USA",
115+
"zoom": 16
116+
}
117+
}]
118+
}],
119+
"feature_queries": [],
120+
"feature_actions": []
121+
}],
122+
"user_connection": {
123+
"user_features": [{
124+
"id": "b3a585f5-1427-40f5-9295-957cde1856dd",
125+
"feature_id": "a9wzuevc23",
126+
"enabled": true,
127+
"user_fields": [],
128+
"user_feature_triggers": [{
129+
"id": "b0fe2ba5-ff2e-4c4b-b63a-8f65c7f90c48",
130+
"feature_trigger_id": "pc7a2p4lbn",
131+
"user_fields": [{
132+
"field_id": "group_guid",
133+
"field_type": "COLLECTION_SELECT",
134+
"value": {
135+
"label": "My_first_group",
136+
"group": null,
137+
"value": "gAiXzykdSNyPEAAOAAAAwmRhYTYxMmEzMjUwZjkzZGJNeV9maXJzdF9ncm91cA"
138+
}
139+
}, {
140+
"field_id": "location",
141+
"field_type": "LOCATION_ENTER",
142+
"value": {
143+
"lat": 37.78338859999999,
144+
"lng": -122.408433,
145+
"radius": 349.2418372528667,
146+
"address": "923 Market St, San Francisco, CA 94103, USA",
147+
"description": "923 Market St, San Francisco, CA 94103, USA",
148+
"zoom": 16
149+
}
150+
}]
151+
}],
152+
"user_feature_queries": [],
153+
"user_feature_actions": []
154+
}]
155+
},
156+
"embedded_url": "https://ifttt.com/access/api/aMozvjdb"
157+
}

0 commit comments

Comments
 (0)