/
REAddress.m
138 lines (114 loc) · 4.39 KB
/
REAddress.m
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* The MIT License
* Copyright (c) 2015 Recurly, Inc.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import "REAddress.h"
#import "REValidation.h"
#import "REMacros.h"
#import "REError.h"
#import "Foundation+Recurly.h"
static NSString *readProperty(ABRecordRef record, ABPropertyID propertyId)
{
return CFBridgingRelease(ABRecordCopyValue(record, propertyId));
}
static NSString *readKey(NSDictionary *dict, CFStringRef key)
{
if(dict) {
return dict[(__bridge __strong id)(key)];
}else{
return nil;
}
}
static NSDictionary *readAddress(ABRecordRef record)
{
CFTypeRef addressInfo = ABRecordCopyValue(record, kABPersonAddressProperty);
if(addressInfo) {
NSDictionary *dict = CFBridgingRelease(ABMultiValueCopyValueAtIndex(addressInfo, 0));
CFRelease(addressInfo);
return dict;
}else {
return NULL;
}
}
@implementation REAddress
- (instancetype)initWithABRecord:(ABRecordRef)record
{
self = [super init];
if (self) {
[self parseABRecord:record];
}
return self;
}
- (void)parseABRecord:(ABRecordRef)record
{
_firstName = readProperty(record, kABPersonFirstNameProperty);
_lastName = readProperty(record, kABPersonLastNameProperty);
_companyName = readProperty(record, kABPersonOrganizationProperty);
NSDictionary *firstAddress = readAddress(record);
_address1 = readKey(firstAddress, kABPersonAddressStreetKey);
_city = readKey(firstAddress, kABPersonAddressCityKey);
_postalCode = readKey(firstAddress, kABPersonAddressZIPKey);
_state = readKey(firstAddress, kABPersonAddressStateKey);
_countryCode= readKey(firstAddress, kABPersonAddressCountryCodeKey);
}
- (NSDictionary *)JSONDictionary
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:4];
[dict setOptionalObject:_firstName forKey:@"first_name"];
[dict setOptionalObject:_lastName forKey:@"last_name"];
[dict setOptionalObject:_companyName forKey:@"company"];
[dict setOptionalObject:_vatCode forKey:@"vat_number"];
[dict setOptionalObject:_phone forKey:@"phone"];
[dict setOptionalObject:_address1 forKey:@"address1"];
[dict setOptionalObject:_address2 forKey:@"address2"];
[dict setOptionalObject:_city forKey:@"city"];
[dict setOptionalObject:_postalCode forKey:@"postal_code"];
[dict setOptionalObject:_state forKey:@"state"];
[dict setOptionalObject:_countryCode forKey:@"country"];
return dict;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"REAddress{\n"
@"\t-First name: %@\n"
@"\t-Last name: %@\n"
@"\t-Company name: %@\n"
@"\t-VAT code: %@\n"
@"\t-Address1: %@\n"
@"\t-Address2: %@\n"
@"\t-Postal code: %@\n"
@"\t-City: %@\n"
@"\t-State: %@\n"
@"\t-Country code: %@\n}",
_firstName, _lastName, _companyName, _vatCode,
_address1, _address2, _postalCode, _city, _state, _countryCode];
}
- (NSError *)validate
{
if(![REValidation validateCountryCode:_countryCode]) {
return [REError invalidFieldError:@"country code" message:nil];
}
return nil;
}
@end