Newer
Older
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
//
// HCPApplicationConfig.m
//
// Created by Nikolay Demyankov on 10.08.15.
//
#import "HCPApplicationConfig.h"
#import "NSBundle+HCPExtension.h"
#import "NSError+HCPExtension.h"
@interface HCPApplicationConfig() {
NSString *_storeUrl;
}
@property (nonatomic, strong) NSString *storeIdentifier;
@property (nonatomic, strong, readwrite) HCPContentConfig *contentConfig;
@end
#pragma mark JSON keys declaration
static NSString *const STORE_PACKAGE_IDENTIFIER_JSON_KEY = @"ios_identifier";
#pragma mark Local constants
static NSString *const STORE_URL_TEMPLATE = @"https://itunes.apple.com/app/%@";
@implementation HCPApplicationConfig
#pragma mark Public API
- (NSString *)storeUrl {
if (self.storeIdentifier.length == 0) {
return nil;
}
if (_storeUrl == nil) {
if ([self.storeIdentifier hasPrefix:@"http://"] || [self.storeIdentifier hasPrefix:@"https://"]) {
_storeUrl = self.storeIdentifier;
} else {
_storeUrl = [NSString stringWithFormat:STORE_URL_TEMPLATE, self.storeIdentifier];
}
}
return _storeUrl;
}
+ (instancetype)configFromBundle:(NSString *)configFileName {
NSURL *wwwFolderURL = [NSURL fileURLWithPath:[NSBundle pathToWwwFolder] isDirectory:YES];
NSURL *chcpJsonFileURLFromBundle = [wwwFolderURL URLByAppendingPathComponent:configFileName];
NSData *jsonData = [NSData dataWithContentsOfURL:chcpJsonFileURLFromBundle];
if (jsonData == nil) {
return nil;
}
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if (error) {
NSLog(@"Can't read application config from bundle. %@", [error underlyingErrorLocalizedDesription]);
return nil;
}
return [HCPApplicationConfig instanceFromJsonObject:json];
}
#pragma mark HCPJsonConvertable implementation
- (id)toJson {
NSMutableDictionary *jsonObject;
NSDictionary *contentConfigJsonObject = [self.contentConfig toJson];
if (contentConfigJsonObject) {
jsonObject = [[NSMutableDictionary alloc] initWithDictionary:contentConfigJsonObject];
} else {
jsonObject = [[NSMutableDictionary alloc] init];
}
if (self.storeIdentifier) {
jsonObject[STORE_PACKAGE_IDENTIFIER_JSON_KEY] = self.storeIdentifier;
}
return jsonObject;
}
+ (instancetype)instanceFromJsonObject:(id)json {
if (!json || ![json isKindOfClass:[NSDictionary class]]) {
return nil;
}
NSDictionary *jsonObject = json;
HCPApplicationConfig *appConfig = [[HCPApplicationConfig alloc] init];
appConfig.storeIdentifier = jsonObject[STORE_PACKAGE_IDENTIFIER_JSON_KEY];
appConfig.contentConfig = [HCPContentConfig instanceFromJsonObject:jsonObject];
return appConfig;
}
@end