-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHackfoldrClient.m
More file actions
124 lines (102 loc) · 3.97 KB
/
Copy pathHackfoldrClient.m
File metadata and controls
124 lines (102 loc) · 3.97 KB
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
//
// HackfoldrClient.m
// hackfoldr-iOS
//
// Created by Superbil on 2014/6/22.
// Copyright (c) 2014年 org.superbil. All rights reserved.
//
#import "HackfoldrClient.h"
#import "AFCSVParserResponseSerializer.h"
#import "HackfoldrPage.h"
@implementation HackfoldrTaskCompletionSource
+ (HackfoldrTaskCompletionSource *)taskCompletionSource
{
return [[HackfoldrTaskCompletionSource alloc] init];
}
- (void)dealloc
{
[self.connectionTask cancel];
self.connectionTask = nil;
}
- (void)cancel
{
[self.connectionTask cancel];
[super cancel];
}
@end
#pragma mark -
@interface HackfoldrClient ()
@property (nonatomic, strong, readwrite) HackfoldrPage *lastPage;
@end
@implementation HackfoldrClient
+ (instancetype)sharedClient
{
static dispatch_once_t onceToken;
static HackfoldrClient *shareClient;
dispatch_once(&onceToken, ^{
shareClient = [[HackfoldrClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://ethercalc.org/"]];
});
return shareClient;
}
+ (AFCSVParserResponseSerializer *)CSVSerializer
{
AFCSVParserResponseSerializer *serializer = [AFCSVParserResponseSerializer serializer];
serializer.usedEncoding = NSUTF8StringEncoding;
return serializer;
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
self.responseSerializer = [AFJSONResponseSerializer serializer];
}
return self;
}
- (HackfoldrTaskCompletionSource *)_taskCompletionWithPath:(NSString *)inPath
{
HackfoldrTaskCompletionSource *source = [HackfoldrTaskCompletionSource taskCompletionSource];
NSString *requestPath = [NSString stringWithFormat:@"%@.csv.json", inPath];
source.connectionTask = [self GET:requestPath parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id fieldArray) {
HackfoldrPage *page = [[HackfoldrPage alloc] initWithKey:inPath fieldArray:fieldArray];
self->_lastPage = [page copy];
[source setResult:self->_lastPage];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[source setError:error];
}];
return source;
}
- (HackfoldrTaskCompletionSource *)taskCompletionFromEthercalcWithKey:(NSString *)key
{
return [self _taskCompletionWithPath:key];
}
- (HackfoldrTaskCompletionSource *)taskCompletionFromGoogleSheetWithSheetKey:(NSString *)keyID
{
// example at https://docs.google.com/spreadsheets/d/176W720jq1zpjsOcsZTkSmwqhmm_hK4VFINK_aubF8sc/export?format=csv&gid=0
HackfoldrTaskCompletionSource *source = [HackfoldrTaskCompletionSource taskCompletionSource];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://docs.google.com/"]];
manager.responseSerializer = [[self class] CSVSerializer];
NSString *requestKeyID = [keyID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
NSString *requestPath = [NSString stringWithFormat:@"spreadsheets/d/%@/export?format=csv&gid=0", requestKeyID];
source.connectionTask = [manager GET:requestPath parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id csvFieldArray) {
HackfoldrPage *page = [[HackfoldrPage alloc] initWithKey:keyID fieldArray:csvFieldArray];
self->_lastPage = [page copy];
[source setResult:self->_lastPage];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error:%@ %@", error, task.response);
[source setError:error];
}];
return source;
}
- (HackfoldrTaskCompletionSource *)taskCompletionWithKey:(NSString *)hackfoldrKey
{
HackfoldrTaskCompletionSource *completionSource = nil;
// check where the data come from, ethercalc or gsheet
if (hackfoldrKey.length < 40) {
completionSource = [self taskCompletionFromEthercalcWithKey:hackfoldrKey];
} else {
completionSource = [self taskCompletionFromGoogleSheetWithSheetKey:hackfoldrKey];
}
return completionSource;
}
@end