Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#import "FirebaseABTesting/Sources/Private/ABTExperimentPayload.h"
16
 
17
static NSString *const kExperimentPayloadKeyExperimentID = @"experimentId";
18
static NSString *const kExperimentPayloadKeyVariantID = @"variantId";
19
 
20
// Start time can either be a date string or integer (milliseconds since 1970).
21
static NSString *const kExperimentPayloadKeyExperimentStartTime = @"experimentStartTime";
22
static NSString *const kExperimentPayloadKeyExperimentStartTimeMillis =
23
    @"experimentStartTimeMillis";
24
static NSString *const kExperimentPayloadKeyTriggerEvent = @"triggerEvent";
25
static NSString *const kExperimentPayloadKeyTriggerTimeoutMillis = @"triggerTimeoutMillis";
26
static NSString *const kExperimentPayloadKeyTimeToLiveMillis = @"timeToLiveMillis";
27
static NSString *const kExperimentPayloadKeySetEventToLog = @"setEventToLog";
28
static NSString *const kExperimentPayloadKeyActivateEventToLog = @"activateEventToLog";
29
static NSString *const kExperimentPayloadKeyClearEventToLog = @"clearEventToLog";
30
static NSString *const kExperimentPayloadKeyTimeoutEventToLog = @"timeoutEventToLog";
31
static NSString *const kExperimentPayloadKeyTTLExpiryEventToLog = @"ttlExpiryEventToLog";
32
 
33
static NSString *const kExperimentPayloadKeyOverflowPolicy = @"overflowPolicy";
34
static NSString *const kExperimentPayloadValueDiscardOldestOverflowPolicy = @"DISCARD_OLDEST";
35
static NSString *const kExperimentPayloadValueIgnoreNewestOverflowPolicy = @"IGNORE_NEWEST";
36
 
37
static NSString *const kExperimentPayloadKeyOngoingExperiments = @"ongoingExperiments";
38
 
39
@implementation ABTExperimentLite
40
 
41
- (instancetype)initWithExperimentId:(NSString *)experimentId {
42
  if (self = [super init]) {
43
    _experimentId = experimentId;
44
  }
45
  return self;
46
}
47
 
48
@end
49
 
50
@implementation ABTExperimentPayload
51
 
52
+ (NSDateFormatter *)experimentStartTimeFormatter {
53
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
54
  [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
55
  [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
56
  // Locale needs to be hardcoded. See
57
  // https://developer.apple.com/library/ios/#qa/qa1480/_index.html for more details.
58
  [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
59
  [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
60
  return dateFormatter;
61
}
62
 
63
+ (nullable instancetype)parseFromData:(NSData *)data {
64
  NSError *error;
65
  NSDictionary *experimentDictionary =
66
      [NSJSONSerialization JSONObjectWithData:data
67
                                      options:NSJSONReadingAllowFragments
68
                                        error:&error];
69
  if (error != nil) {
70
    return nil;
71
  } else {
72
    return [[ABTExperimentPayload alloc] initWithDictionary:experimentDictionary];
73
  }
74
}
75
 
76
- (instancetype)initWithDictionary:(NSDictionary<NSString *, id> *)dictionary {
77
  if (self = [super init]) {
78
    _experimentId = dictionary[kExperimentPayloadKeyExperimentID];
79
    _variantId = dictionary[kExperimentPayloadKeyVariantID];
80
    _triggerEvent = dictionary[kExperimentPayloadKeyTriggerEvent];
81
    _setEventToLog = dictionary[kExperimentPayloadKeySetEventToLog];
82
    _activateEventToLog = dictionary[kExperimentPayloadKeyActivateEventToLog];
83
    _clearEventToLog = dictionary[kExperimentPayloadKeyClearEventToLog];
84
    _timeoutEventToLog = dictionary[kExperimentPayloadKeyTimeoutEventToLog];
85
    _ttlExpiryEventToLog = dictionary[kExperimentPayloadKeyTTLExpiryEventToLog];
86
 
87
    // Experiment start time can either be in the form of a date string or milliseconds since 1970.
88
    if (dictionary[kExperimentPayloadKeyExperimentStartTime]) {
89
      // Convert from date string.
90
      NSDate *experimentStartTime = [[[self class] experimentStartTimeFormatter]
91
          dateFromString:dictionary[kExperimentPayloadKeyExperimentStartTime]];
92
      _experimentStartTimeMillis =
93
          [@([experimentStartTime timeIntervalSince1970] * 1000) longLongValue];
94
    } else if (dictionary[kExperimentPayloadKeyExperimentStartTimeMillis]) {
95
      // Simply store milliseconds.
96
      _experimentStartTimeMillis =
97
          [dictionary[kExperimentPayloadKeyExperimentStartTimeMillis] longLongValue];
98
      ;
99
    }
100
 
101
    _triggerTimeoutMillis = [dictionary[kExperimentPayloadKeyTriggerTimeoutMillis] longLongValue];
102
    _timeToLiveMillis = [dictionary[kExperimentPayloadKeyTimeToLiveMillis] longLongValue];
103
 
104
    // Overflow policy can be an integer, or string e.g. "DISCARD_OLDEST" or "IGNORE_NEWEST".
105
    if ([dictionary[kExperimentPayloadKeyOverflowPolicy] isKindOfClass:[NSString class]]) {
106
      // If it's a string, pick against the preset string values.
107
      NSString *policy = dictionary[kExperimentPayloadKeyOverflowPolicy];
108
      if ([policy isEqualToString:kExperimentPayloadValueDiscardOldestOverflowPolicy]) {
109
        _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
110
      } else if ([policy isEqualToString:kExperimentPayloadValueIgnoreNewestOverflowPolicy]) {
111
        _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest;
112
      } else {
113
        _overflowPolicy = ABTExperimentPayloadExperimentOverflowPolicyUnrecognizedValue;
114
      }
115
    } else {
116
      _overflowPolicy = [dictionary[kExperimentPayloadKeyOverflowPolicy] intValue];
117
    }
118
 
119
    NSMutableArray<ABTExperimentLite *> *ongoingExperiments = [[NSMutableArray alloc] init];
120
 
121
    NSArray<NSDictionary<NSString *, NSString *> *> *ongoingExperimentsArray =
122
        dictionary[kExperimentPayloadKeyOngoingExperiments];
123
 
124
    for (NSDictionary<NSString *, NSString *> *experimentDictionary in ongoingExperimentsArray) {
125
      NSString *experimentId = experimentDictionary[kExperimentPayloadKeyExperimentID];
126
      if (experimentId) {
127
        ABTExperimentLite *liteExperiment =
128
            [[ABTExperimentLite alloc] initWithExperimentId:experimentId];
129
        [ongoingExperiments addObject:liteExperiment];
130
      }
131
    }
132
 
133
    _ongoingExperiments = [ongoingExperiments copy];
134
  }
135
  return self;
136
}
137
 
138
- (void)clearTriggerEvent {
139
  _triggerEvent = nil;
140
}
141
 
142
- (BOOL)overflowPolicyIsValid {
143
  return self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyIgnoreNewest ||
144
         self.overflowPolicy == ABTExperimentPayloadExperimentOverflowPolicyDiscardOldest;
145
}
146
 
147
- (void)setOverflowPolicy:(ABTExperimentPayloadExperimentOverflowPolicy)overflowPolicy {
148
  _overflowPolicy = overflowPolicy;
149
}
150
 
151
@end