Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 94d27e3a742f0c486ac096db5536429b5fe477a0 @matthiasplappert committed Mar 27, 2011
@@ -0,0 +1,14 @@
+.DS_Store
+*.swp
+*~.nib
+
+build/*
+
+*.pbxuser
+*.mode1v3
+*.mode2v3
+*.perspective
+*.perspectivev3
+
+*.xcodeproj/xcuserdata/*
+*.xcodeproj/project.xcworkspace/xcuserdata/*
@@ -0,0 +1,3 @@
+[submodule "InstapaperKit/Third Party/JSONKit"]
+ path = InstapaperKit/Third Party/JSONKit
+ url = git://github.com/johnezang/JSONKit.git

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,39 @@
+//
+// IKBookmark.h
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface IKBookmark : NSObject {
+ NSInteger _bookmarkID;
+ NSURL *_URL;
+ NSString *_title;
+ NSString *_descr;
+ NSDate *_date;
+ BOOL _starred;
+ NSString *_privateSource;
+ NSString *_hashString;
+ CGFloat _progress;
+ NSDate *_progressDate;
+}
+
+@property (nonatomic, assign) NSInteger bookmarkID;
+@property (nonatomic, retain) NSURL *URL;
+@property (nonatomic, copy) NSString *title;
+@property (nonatomic, copy) NSString *descr;
+@property (nonatomic, retain) NSDate *date;
+@property (nonatomic, assign, getter=isStarred) BOOL starred;
+@property (nonatomic, copy) NSString *privateSource;
+@property (nonatomic, copy) NSString *hashString;
+@property (nonatomic, assign) CGFloat progress;
+@property (nonatomic, retain) NSDate *progressDate;
+
++ (IKBookmark *)bookmarkWithBookmarkID:(NSInteger)bookmarkID;
+- (id)initWithBookmarkID:(NSInteger)bookmarkID;
+
+@end
@@ -0,0 +1,70 @@
+//
+// IKBookmark.m
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "IKBookmark.h"
+
+
+@implementation IKBookmark
+
+@synthesize bookmarkID = _bookmarkID, URL = _URL, title = _title, descr = _descr, date = _date,
+ starred = _starred, privateSource = _privateSource, hashString = _hashString,
+ progress = _progress, progressDate = _progressDate;
+
++ (IKBookmark *)bookmarkWithBookmarkID:(NSInteger)bookmarkID
+{
+ IKBookmark *bookmark = [[[IKBookmark alloc] initWithBookmarkID:bookmarkID] autorelease];
+ return bookmark;
+}
+
+- (id)init
+{
+ return [self initWithBookmarkID:NSNotFound];
+}
+
+- (id)initWithBookmarkID:(NSInteger)bookmarkID
+{
+ if ((self = [super init])) {
+ _bookmarkID = bookmarkID;
+ _URL = nil;
+ _title = nil;
+ _descr = nil;
+ _date = nil;
+ _starred = NO;
+ _privateSource = nil;
+ _hashString = nil;
+ _progress = -1.0f;
+ _progressDate = nil;
+ }
+ return self;
+}
+
+- (NSString *)description
+{
+ return [NSString stringWithFormat:@"<%@: %@ (%d), URL:(%@)>", NSStringFromClass([self class]),
+ self.title,
+ self.bookmarkID,
+ self.URL];
+}
+
+#pragma mark -
+#pragma mark Memory management
+
+- (void)dealloc
+{
+ [_URL release];
+ [_title release];
+ [_descr release];
+ [_date release];
+ [_privateSource release];
+ [_hashString release];
+ [_progressDate release];
+
+ [super dealloc];
+}
+
+@end
@@ -0,0 +1,44 @@
+//
+// IKConstants.h
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+// Error domain
+extern NSString *const IKErrorDomain;
+
+// Error keys
+extern NSString *const IKErrorMessageKey;
+
+// Error keys
+enum {
+ // General errors
+ IKErrorCodeRateLimitExceeded = 1040,
+ IKErrorCodeSubscriptionAccountRequired = 1041,
+ IKErrorCodeApplicationSuspended = 1042,
+
+ // Bookmark errors
+ IKErrorCodeDomainRequiresContent = 1220,
+ IKErrorCodeDomainOptedOut = 1221,
+ IKErrorCodeDomainInvalidURL = 1240,
+ IKErrorCodeInvalidBookmarkID = 1241,
+ IKErrorCodeInvalidFolderID = 1242,
+ IKErrorCodeInvalidProgress = 1243,
+ IKErrorCodeInvalidProgressTimestamp = 1244,
+ IKErrorCodePrivateBookmarkRequiresContent = 1245,
+ IKErrorCodeUnexpectedBookmarkError = 1250,
+
+ // Folder errors
+ IKErrorCodeInvalidTitle = 1250,
+ IKErrorCodeFolderAlreadyExists = 1251,
+ IKErrorCodeCannotAddBookmark = 1252,
+
+ // Operational errors
+ IKErrorCodeUnexpectedServiceError = 1500,
+ IKErrorCodeErrorGeneratingTextOfURL = 1550
+};
@@ -0,0 +1,13 @@
+//
+// IKConstants.m
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "IKConstants.h"
+
+
+NSString *const IKErrorDomain = @"com.matthiasplappert.InstapaperKit";
+NSString *const IKErrorMessageKey = @"message";
@@ -0,0 +1,17 @@
+//
+// IKDeserializer.h
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface IKDeserializer : NSObject
+
++ (BOOL)token:(NSString **)token andTokenSecret:(NSString **)secret fromQlineString:(NSString *)qlineString;
++ (id)objectFromJSONString:(NSString *)JSONString;
+
+@end
@@ -0,0 +1,168 @@
+//
+// IKDeserializer.m
+// InstapaperKit
+//
+// Created by Matthias Plappert on 3/13/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "IKDeserializer.h"
+#import "IKUser.h"
+#import "IKBookmark.h"
+#import "IKFolder.h"
+#import "IKConstants.h"
+
+#import "JSONKit.h"
+
+
+@interface IKDeserializer ()
+
++ (id)_normalizedObjectForKey:(NSString *)key inDictionary:(NSDictionary *)dict;
++ (NSURL *)_URLForKey:(NSString *)key inDictionary:(NSDictionary *)dict;
++ (NSDate *)_dateForKey:(NSString *)key inDictionary:(NSDictionary *)dict;
+
+@end
+
+
+@implementation IKDeserializer
+
++ (BOOL)token:(NSString **)token andTokenSecret:(NSString **)secret fromQlineString:(NSString *)qlineString
+{
+ // Extract token & secret
+ NSArray *parts = [qlineString componentsSeparatedByString:@"&"];
+ for (NSString *part in parts) {
+ NSArray *subparts = [part componentsSeparatedByString:@"="];
+ if ([subparts count] != 2) {
+ // Invalid, skip
+ continue;
+ }
+
+ NSString *key = [subparts objectAtIndex:0];
+ NSString *value = [subparts objectAtIndex:1];
+ if ([key isEqualToString:@"oauth_token"]) {
+ *token = value;
+ } else if ([key isEqualToString:@"oauth_token_secret"]) {
+ *secret = value;
+ }
+ }
+
+ if (!*token || !*secret) {
+ *token = nil;
+ *secret = nil;
+ return NO;
+ }
+
+ return YES;
+}
+
++ (id)objectFromJSONString:(NSString *)JSONString
+{
+ if (!JSONString) {
+ return nil;
+ }
+
+ // Parse JSON
+ NSArray *JSONArray = [JSONString objectFromJSONString];
+ if (![JSONArray isKindOfClass:[NSArray class]]) {
+ // Invalid JSON
+ return nil;
+ }
+
+ NSMutableArray *result = [NSMutableArray array];
+
+ for (NSDictionary *dict in JSONArray) {
+ if (![dict isKindOfClass:[NSDictionary class]]) {
+ // Skip
+ continue;
+ }
+
+ // Get type
+ NSString *type = [dict objectForKey:@"type"];
+ if ([type isEqualToString:@"error"]) {
+ // Return error object, not array
+ NSInteger code = [[dict objectForKey:@"error_code"] integerValue];
+ NSString *message = [dict objectForKey:@"message"];
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:message
+ forKey:IKErrorMessageKey];
+
+ return [NSError errorWithDomain:IKErrorDomain code:code userInfo:userInfo];
+
+ } else if ([type isEqualToString:@"user"]) {
+ // Create user object
+ IKUser *user = [[IKUser alloc] init];
+
+ user.userID = [[self _normalizedObjectForKey:@"user_id" inDictionary:dict] unsignedIntegerValue];
+ user.username = [self _normalizedObjectForKey:@"username" inDictionary:dict];
+ user.subscribed = [[self _normalizedObjectForKey:@"subscription_is_active" inDictionary:dict] boolValue];
+
+ [result addObject:user];
+
+ } else if ([type isEqualToString:@"bookmark"]) {
+ // Create bookmark object
+ IKBookmark *bookmark = [[IKBookmark alloc] init];
+
+ bookmark.bookmarkID = [[self _normalizedObjectForKey:@"bookmark_id" inDictionary:dict] integerValue];
+ bookmark.URL = [self _URLForKey:@"url" inDictionary:dict];
+ bookmark.title = [self _normalizedObjectForKey:@"title" inDictionary:dict];
+ bookmark.descr = [self _normalizedObjectForKey:@"descr" inDictionary:dict];
+ bookmark.date = [self _dateForKey:@"time" inDictionary:dict];
+ bookmark.starred = [[self _normalizedObjectForKey:@"starred" inDictionary:dict] boolValue];
+ bookmark.privateSource = [self _normalizedObjectForKey:@"private_source" inDictionary:dict];
+ bookmark.hashString = [self _normalizedObjectForKey:@"hash" inDictionary:dict];
+ bookmark.progress = [[self _normalizedObjectForKey:@"progress" inDictionary:dict] floatValue];
+ bookmark.progressDate = [self _dateForKey:@"progress_timestamp" inDictionary:dict];
+
+ [result addObject:bookmark];
+
+ } else if ([type isEqualToString:@"folder"]) {
+ // Create folder object
+ IKFolder *folder = [[IKFolder alloc] init];
+
+ folder.folderID = [[self _normalizedObjectForKey:@"folder_id" inDictionary:dict] integerValue];
+ folder.title = [self _normalizedObjectForKey:@"title" inDictionary:dict];
+ folder.syncToMobile = [[self _normalizedObjectForKey:@"sync_to_mobile" inDictionary:dict] boolValue];
+ folder.position = [[self _normalizedObjectForKey:@"position" inDictionary:dict] unsignedIntegerValue];
+
+ [result addObject:folder];
+
+ }
+ }
+
+ return result;
+}
+
+#pragma mark -
+#pragma mark Private methods
+
++ (id)_normalizedObjectForKey:(NSString *)key inDictionary:(NSDictionary *)dict
+{
+ id object = [dict objectForKey:key];
+ if ((NSNull *)object == [NSNull null]) {
+ return nil;
+ }
+
+ return object;
+}
+
++ (NSURL *)_URLForKey:(NSString *)key inDictionary:(NSDictionary *)dict
+{
+ NSString *URLString = [self _normalizedObjectForKey:key inDictionary:dict];
+ if (!URLString || ![URLString isKindOfClass:[NSString class]]) {
+ return nil;
+ }
+
+ return [NSURL URLWithString:URLString];
+}
+
++ (NSDate *)_dateForKey:(NSString *)key inDictionary:(NSDictionary *)dict
+{
+ id object = [self _normalizedObjectForKey:key inDictionary:dict];
+ if (!object || ![object respondsToSelector:@selector(unsignedIntegerValue)]) {
+ return nil;
+ }
+
+ NSUInteger unixTimestamp = [(NSNumber *)object unsignedIntegerValue];
+ return [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)unixTimestamp];
+}
+
+@end
Oops, something went wrong.

0 comments on commit 94d27e3

Please sign in to comment.