forked from michaelvillar/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MVCloudAppLinkService.m
90 lines (78 loc) · 2.33 KB
/
MVCloudAppLinkService.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
#import "MVCloudAppLinkService.h"
#import "MVJSONGetRequest.h"
@interface MVCloudAppLinkService ()
@property (strong, readwrite) NSURL *url;
@property (strong, readwrite) NSString *token;
@property (strong, readwrite) NSString *name;
@property (strong, readwrite) NSString *itemType;
@property (strong, readwrite) NSURL *downloadUrl;
@property (readwrite) BOOL informationFetched;
@property (readwrite) BOOL error;
@end
@implementation MVCloudAppLinkService
@synthesize url = url_,
token = token_,
name = name_,
itemType = itemType_,
downloadUrl = downloadUrl_,
informationFetched = informationFetched_,
error = error_;
- (id)initWithURL:(NSURL*)url
token:(NSString*)token
{
self = [super init];
if(self)
{
url_ = url;
token_ = token;
name_ = nil;
itemType_ = nil;
downloadUrl_ = nil;
informationFetched_ = NO;
error_ = NO;
}
return self;
}
- (BOOL)isImage
{
return [self.itemType isEqualToString:@"image"];
}
#pragma mark -
#pragma mark MVService Methods
- (void)fetchInformation
{
NSString *urlString = [NSString stringWithFormat:@"http://cl.ly/%@",self.token];
NSURL *url = [NSURL URLWithString:urlString];
MVJSONGetRequest *getRequest = [[MVJSONGetRequest alloc] initWithURL:url];
[getRequest get:^(NSObject *object) {
@try {
NSDictionary *dictionary = (NSDictionary*)object;
if(dictionary && [dictionary valueForKey:@"name"] && [dictionary valueForKey:@"download_url"])
{
self.name = [dictionary valueForKey:@"name"];
self.itemType = [dictionary valueForKey:@"item_type"];
NSString *downloadUrlString = [dictionary valueForKey:@"download_url"];
self.downloadUrl = [NSURL URLWithString:downloadUrlString];
}
else
{
self.error = YES;
}
}
@catch (NSException *exception) {
self.error = YES;
}
self.informationFetched = YES;
}];
}
+ (MVCloudAppLinkService*)serviceForURL:(NSURL*)url;
{
if([url.host isEqualToString:@"cl.ly"])
{
MVCloudAppLinkService *service = [[MVCloudAppLinkService alloc] initWithURL:url
token:url.path];
return service;
}
return nil;
}
@end