Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate address should allow addresslabel in place of street + number, fix timezone json conversion issues. #371

32 changes: 31 additions & 1 deletion Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,37 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN

demoButton(text: "ipGeocode") {
Radar.ipGeocode { (status, address, proxy) in
print("IP geocode: status = \(Radar.stringForStatus(status)); country = \(String(describing: address?.countryCode)); city = \(String(describing: address?.city)); proxy = \(proxy)")
print("IP geocode: status = \(Radar.stringForStatus(status)); country = \(String(describing: address?.countryCode)); city = \(String(describing: address?.city)); proxy = \(proxy); full address: \(String(describing: address?.dictionaryValue()))")
}
}

demoButton(text: "validateAddress") {
let address: RadarAddress = RadarAddress(from: [
"latitude": 0,
"longitude": 0,
"city": "New York",
"stateCode": "NY",
"postalCode": "10003",
"countryCode": "US",
"street": "Broadway",
"number": "841",
])!

Radar.validateAddress(address: address) { (status, address, verificationStatus) in
print("Validate address with street + number: status = \(Radar.stringForStatus(status)); country = \(String(describing: address?.countryCode)); city = \(String(describing: address?.city)); verificationStatus = \(verificationStatus)")
}

let addressLabel: RadarAddress = RadarAddress(from: [
"latitude": 0,
"longitude": 0,
"city": "New York",
"stateCode": "NY",
"postalCode": "10003",
"countryCode": "US",
"addressLabel": "Broadway 841",
])!
Radar.validateAddress(address: addressLabel) { (status, address, verificationStatus) in
print("Validate address with address label: status = \(Radar.stringForStatus(status)); country = \(String(describing: address?.countryCode)); city = \(String(describing: address?.city)); verificationStatus = \(verificationStatus)")
}
}

Expand Down
15 changes: 12 additions & 3 deletions RadarSDK/RadarAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,8 @@ - (void)validateAddress:(RadarAddress *)address completionHandler:(RadarValidate
}

NSMutableString *queryString = [NSMutableString new];
if (!address.countryCode || !address.stateCode || !address.city || !address.number || !address.postalCode || !address.street) {
if (!address.countryCode || !address.stateCode || !address.city || !address.postalCode ||
!((address.street && address.number) || address.addressLabel)) {
if (completionHandler) {
[RadarUtils runOnMainThread:^{
completionHandler(RadarStatusErrorBadRequest, nil, nil, RadarAddressVerificationStatusNone);
Expand All @@ -1089,9 +1090,16 @@ - (void)validateAddress:(RadarAddress *)address completionHandler:(RadarValidate
[queryString appendFormat:@"countryCode=%@", address.countryCode];
[queryString appendFormat:@"&stateCode=%@", address.stateCode];
[queryString appendFormat:@"&city=%@", address.city];
[queryString appendFormat:@"&number=%@", address.number];
[queryString appendFormat:@"&postalCode=%@", address.postalCode];
[queryString appendFormat:@"&street=%@", address.street];
if (address.street) {
[queryString appendFormat:@"&street=%@", address.street];
}
if (address.number) {
[queryString appendFormat:@"&number=%@", address.number];
}
if (address.addressLabel) {
[queryString appendFormat:@"&addressLabel=%@", address.addressLabel];
}
}

if (address.unit) {
Expand Down Expand Up @@ -1132,6 +1140,7 @@ - (void)validateAddress:(RadarAddress *)address completionHandler:(RadarValidate
[RadarUtils runOnMainThread:^{
completionHandler(RadarStatusSuccess, res, address, verificationStatus);
}];
return;
}
}

Expand Down
8 changes: 4 additions & 4 deletions RadarSDK/RadarAddress.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,13 @@ + (NSString *)stringForConfidence:(RadarAddressConfidence)confidence {
}

+ (RadarAddressVerificationStatus)addressVerificationStatusForString:(NSString *)string {
if ([string isEqualToString:@"v"]) {
if ([string isEqualToString:@"verified"]) {
return RadarAddressVerificationStatusVerified;
} else if ([string isEqualToString:@"p"]) {
} else if ([string isEqualToString:@"partially verified"]) {
return RadarAddressVerificationStatusPartiallyVerified;
} else if ([string isEqualToString:@"a"]) {
} else if ([string isEqualToString:@"ambiguous"]) {
return RadarAddressVerificationStatusAmbiguous;
} else if ([string isEqualToString:@"u"]) {
} else if ([string isEqualToString:@"unverified"]) {
return RadarAddressVerificationStatusUnverified;
} else {
return RadarAddressVerificationStatusNone;
Expand Down
15 changes: 13 additions & 2 deletions RadarSDK/RadarTimeZone.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
#import "RadarTimeZone.h"
#import "RadarUtils.h"

NSDateFormatter *_timezoneDateFormatter = nil;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably there no action items for with respect to this PR, but we really should consider having a more "central" place where we define our time parsers. There are quite a few definitions littered around the codebase. Just a call out to keep in mind for the future.

NSDateFormatter * timezoneDateFormatter(void) {
if (_timezoneDateFormatter == nil) {
_timezoneDateFormatter = [[NSDateFormatter alloc] init];
_timezoneDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[_timezoneDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
}
return _timezoneDateFormatter;
}

@implementation RadarTimeZone

- (instancetype _Nullable)initWithObject:(id)object {
Expand Down Expand Up @@ -35,7 +45,8 @@ - (instancetype _Nullable)initWithObject:(id)object {

id currentTimeObj = dict[@"currentTime"];
if (currentTimeObj && [currentTimeObj isKindOfClass:[NSString class]]) {
_currentTime = [RadarUtils.isoDateFormatter dateFromString:(NSString *)currentTimeObj];

_currentTime = [timezoneDateFormatter() dateFromString:(NSString *)currentTimeObj];
}

id utcOffsetObj = dict[@"utcOffset"];
Expand All @@ -56,7 +67,7 @@ - (NSDictionary *)dictionaryValue {
dict[@"_id"] = self._id;
dict[@"name"] = self.name;
dict[@"code"] = self.code;
dict[@"currentTime"] = self.currentTime;
dict[@"currentTime"] = [timezoneDateFormatter() stringFromDate:self.currentTime];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current time string from server is a different format (no millisecond) so we need a different parser than RadarUtils.isoDateFormatter

dict[@"utcOffset"] = @(self.utcOffset);
dict[@"dstOffset"] = @(self.dstOffset);
return dict;
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarTripOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ - (NSDictionary *)dictionaryValue {
dict[kDestinationGeofenceTag] = self.destinationGeofenceTag;
dict[kDestinationGeofenceExternalId] = self.destinationGeofenceExternalId;
dict[kMode] = [Radar stringForMode:self.mode];
dict[kScheduledArrivalAt] = self.scheduledArrivalAt;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSDate object does not get converted, this field was null, format it to string since all our other date formats are converted to string with JSON

dict[kScheduledArrivalAt] = [RadarUtils.isoDateFormatter stringFromDate:self.scheduledArrivalAt];
if (self.approachingThreshold && self.approachingThreshold > 0) {
dict[kApproachingThreshold] = @(self.approachingThreshold);
}
Expand Down
Loading