Skip to content

Commit

Permalink
Add ability to set default settings is segment.com can't be reached. (#…
Browse files Browse the repository at this point in the history
…888)

* Added integration middleware support.

* Fixed warnings; Updated project to recommended settings.

* only signal the runner if there’s actually middleware to be processed.

* Added & Updated tests.

* Added experimental raw filter block.

* Removed unnecessary logs.

* Added logic to allow tests to function.

* Allow for user-supplied settings.

* Make sure segment.io is still present in the integrations list.

* Added ability to set default settings if segment.com can’t be reached.

* Removed test code.

Co-authored-by: Brandon Sneed <brandon.sneed@segment.com>
  • Loading branch information
bsneed and Brandon Sneed authored May 20, 2020
1 parent 946147c commit 86fe29f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Analytics/Classes/Integrations/SEGIntegrationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ - (void)refreshSettings
NSDictionary *previouslyCachedSettings = [self cachedSettings];
if (previouslyCachedSettings) {
[self setCachedSettings:previouslyCachedSettings];
} else if (self.configuration.defaultSettings != nil) {
// If settings request fail, load a user-supplied version if present.
// but make sure segment.io is in the integrations
NSMutableDictionary *newSettings = [self.configuration.defaultSettings serializableMutableDeepCopy];
newSettings[@"integrations"][@"Segment.io"][@"apiKey"] = self.configuration.writeKey;
[self setCachedSettings:newSettings];
} else {
// If settings request fail, fall back to using just Segment integration.
// Doesn't address situations where this callback never gets called (though we don't expect that to ever happen).
Expand Down
1 change: 1 addition & 0 deletions Analytics/Classes/Internal/SEGAnalyticsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NSString *SEGEventNameForScreenTitle(NSString *title);

// Deep copy and check NSCoding conformance
@protocol SEGSerializableDeepCopy <NSObject>
-(id _Nullable) serializableMutableDeepCopy;
-(id _Nullable) serializableDeepCopy;
@end

Expand Down
48 changes: 37 additions & 11 deletions Analytics/Classes/Internal/SEGAnalyticsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ static id SEGCoerceJSONObject(id obj)

@implementation NSDictionary(SerializableDeepCopy)

- (NSDictionary *)serializableDeepCopy
- (id)serializableDeepCopy:(BOOL)mutable
{
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSArray *keys = [self allKeys];
for (id key in keys) {
id aValue = [self objectForKey:key];
Expand All @@ -263,27 +263,39 @@ - (NSDictionary *)serializableDeepCopy
}

if ([aValue conformsToProtocol:@protocol(SEGSerializableDeepCopy)]) {
theCopy = [aValue serializableDeepCopy];
theCopy = [aValue serializableDeepCopy:mutable];
} else if ([aValue conformsToProtocol:@protocol(NSCopying)]) {
theCopy = [aValue copy];
} else {
theCopy = aValue;
}

[returnDict setValue:theCopy forKey:key];
}
[result setValue:theCopy forKey:key];
}

return [returnDict copy];
if (mutable) {
return result;
} else {
return [result copy];
}
}

- (NSDictionary *)serializableDeepCopy {
return [self serializableDeepCopy:NO];
}

- (NSMutableDictionary *)serializableMutableDeepCopy {
return [self serializableDeepCopy:YES];
}

@end


@implementation NSArray(SerializableDeepCopy)

-(NSArray *)serializableDeepCopy
-(id)serializableDeepCopy:(BOOL)mutable
{
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:self.count];
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:self.count];

for (id aValue in self) {
id theCopy = nil;
Expand All @@ -299,16 +311,30 @@ -(NSArray *)serializableDeepCopy
}

if ([aValue conformsToProtocol:@protocol(SEGSerializableDeepCopy)]) {
theCopy = [aValue serializableDeepCopy];
theCopy = [aValue serializableDeepCopy:mutable];
} else if ([aValue conformsToProtocol:@protocol(NSCopying)]) {
theCopy = [aValue copy];
} else {
theCopy = aValue;
}
[returnArray addObject:theCopy];
[result addObject:theCopy];
}

return [returnArray copy];
if (mutable) {
return result;
} else {
return [result copy];
}
}


- (NSArray *)serializableDeepCopy {
return [self serializableDeepCopy:NO];
}

- (NSMutableArray *)serializableMutableDeepCopy {
return [self serializableDeepCopy:YES];
}


@end
7 changes: 7 additions & 0 deletions Analytics/Classes/SEGAnalyticsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull);
*/
@property (nonatomic, strong, nullable) id<SEGCrypto> crypto;


/**
* Set the default settings to use if Segment.com cannot be reached. JSON->NSDictionary conversion is handled for you.
* An example configuration can be found here, using your write key: https://cdn-settings.segment.com/v1/projects/YOUR_WRITE_KEY/settings
*/
@property (nonatomic, strong, nullable) NSDictionary *defaultSettings;

/**
* Set custom middlewares. Will be run before all integrations.
* This property is deprecated in favor of the `sourceMiddleware` property.
Expand Down

0 comments on commit 86fe29f

Please sign in to comment.