This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
/
SKSite.m
172 lines (141 loc) · 5 KB
/
SKSite.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// SKSite.m
// StackKit
//
// Created by Dave DeLong on 12/27/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import <StackKit/SKSite_Internal.h>
#import <StackKit/SKConstants.h>
#import <StackKit/SKObject_Internal.h>
#import <StackKit/SKCache.h>
#import <StackKit/SKSiteCache.h>
#import <StackKit/SKRequestManager.h>
@interface SKSite()
@end
@implementation SKSite {
SKRequestManager *_requestManager;
}
@dynamic name;
@dynamic audience;
@dynamic launchDate;
@dynamic logoURL;
@dynamic siteURL;
@dynamic iconURL;
@dynamic faviconURL;
@dynamic APISiteParameter;
+ (void)requestSitesWithCompletionHandler:(SKRequestHandler)handler {
[[SKSiteCache sharedSiteCache] requestAllSitesWithCompletionHandler:handler];
}
+ (void)requestSiteWithNameLike:(NSString *)name completionHandler:(SKSiteHandler)handler {
handler = [handler copy];
NSArray *words = [name componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
SKRequestHandler block = ^(NSArray *sites, NSError *error) {
// this will be called on the main thread
if (sites) {
NSPredicate *template = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] $word OR audience CONTAINS[cd] $word or siteURL.absoluteString CONTAINS[cd] $word"];
for (NSString *word in words) {
if ([word length] == 0) { continue; }
NSPredicate *p = [template predicateWithSubstitutionVariables:[NSDictionary dictionaryWithObject:word forKey:@"word"]];
sites = [sites filteredArrayUsingPredicate:p];
}
SKSite *match = nil;
if ([sites count] > 0) {
match = [sites objectAtIndex:0];
}
handler(match, nil);
} else {
handler(nil, error);
}
};
[self requestSitesWithCompletionHandler:block];
[handler release];
}
+ (void)requestSitesWithNames:(NSArray *)names completionHandler:(SKRequestHandler)handler {
handler = [handler copy];
[self requestSitesWithCompletionHandler:^(NSArray *sites, NSError *error) {
NSArray *filtered = nil;
if (sites) {
NSArray *siteNames = [sites valueForKey:@"name"];
NSDictionary *map = [NSDictionary dictionaryWithObjects:sites forKeys:siteNames];
filtered = [map objectsForKeys:names notFoundMarker:[NSNull null]];
}
handler(filtered,error);
}];
[handler release];
}
#pragma mark -
#pragma mark SKSite Instance Stuff
+ (NSArray *)APIKeysBackingProperties {
static dispatch_once_t onceToken;
static NSArray *array = nil;
dispatch_once(&onceToken, ^{
array = [[NSArray alloc] initWithObjects:
SKAPIKeys.site.name,
SKAPIKeys.site.audience,
SKAPIKeys.site.launchDate,
SKAPIKeys.site.logoURL,
SKAPIKeys.site.siteURL,
SKAPIKeys.site.iconURL,
SKAPIKeys.site.faviconURL,
SKAPIKeys.site.siteState,
SKAPIKeys.site.apiSiteParameter,
nil];
});
return array;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
NSDictionary *info = [aDecoder decodeObjectForKey:@"info"];
self = [self _initWithInfo:info site:nil];
if (self) {
[self setCachesDataLocally:[aDecoder decodeBoolForKey:@"cachesLocally"]];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:[self _info] forKey:@"info"];
[aCoder encodeBool:[self cachesDataLocally] forKey:@"cachesLocally"];
}
- (id)_initWithInfo:(id)info site:(SKSite *)site {
self = [super _initWithInfo:info site:nil];
if (self) {
_requestManager = [[SKRequestManager requestManagerWithSite:self] retain];
}
return self;
}
- (void)dealloc {
[_requestManager release];
[super dealloc];
}
- (void)setCachesDataLocally:(BOOL)cachesDataLocally {
[_requestManager setShouldCacheDataLocally:cachesDataLocally];
}
- (BOOL)cachesDataLocally {
return [_requestManager shouldCacheDataLocally];
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@ {name = %@, url = %@}",
[super description],
[self name],
[self siteURL]];
}
// this has a non-object return type, so we'll override the getter manually
- (SKSiteState)siteState {
NSString *value = [self _valueForInfoKey:SKAPIKeys.site.siteState];
SKSiteState s = SKSiteStateNormal;
if ([value isEqualToString:@"linked_meta"]) {
s = SKSiteStateLinkedMeta;
} else if ([value isEqualToString:@"open_beta"]) {
s = SKSiteStateOpenBeta;
} else if ([value isEqualToString:@"closed_beta"]) {
s = SKSiteStateClosedBeta;
}
return s;
}
- (SKSite *)site {
return self;
}
- (void)executeFetchRequest:(SKFetchRequest *)request completionHandler:(SKRequestHandler)handler {
[_requestManager executeRequest:request asynchronously:YES completionHandler:handler];
}
@end