Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
baztokyo committed Nov 19, 2011
0 parents commit 93706c0
Show file tree
Hide file tree
Showing 27 changed files with 2,062 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
*~

.DS_Store

FSQDemo/FSQDemo.xcodeproj/project.xcworkspace/xcuserdata/*.xcuserdatad
FSQDemo/FSQDemo.xcodeproj/xcuserdata/*.xcuserdatad
61 changes: 61 additions & 0 deletions BZFoursquare/BZFoursquare.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2011 Ba-Z Communication Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#import <Foundation/Foundation.h>
#import "BZFoursquareRequest.h"

@protocol BZFoursquareSessionDelegate;

@interface BZFoursquare : NSObject {
NSString *clientID_;
NSString *callbackURL_;
NSString *version_;
NSString *locale_;
id<BZFoursquareSessionDelegate> sessionDelegate_;
NSString *accessToken_;
}
@property(nonatomic,copy,readonly) NSString *clientID;
@property(nonatomic,copy,readonly) NSString *callbackURL;
@property(nonatomic,copy) NSString *version; // YYYYMMDD
@property(nonatomic,copy) NSString *locale; // en (default), fr, de, it, etc.
@property(nonatomic,assign) id<BZFoursquareSessionDelegate> sessionDelegate;
@property(nonatomic,copy) NSString *accessToken;

- (id)initWithClientID:(NSString *)clientID callbackURL:(NSString *)callbackURL;

- (BOOL)startAuthorization;
- (BOOL)handleOpenURL:(NSURL *)url;
- (void)invalidateSession;
- (BOOL)isSessionValid;

- (BZFoursquareRequest *)requestWithPath:(NSString *)path HTTPMethod:(NSString *)HTTPMethod parameters:(NSDictionary *)parameters delegate:(id<BZFoursquareRequestDelegate>)delegate;

@end

@protocol BZFoursquareSessionDelegate <NSObject>
@optional
- (void)foursquareDidAuthorize:(BZFoursquare *)foursquare;
- (void)foursquareDidNotAuthorize:(BZFoursquare *)foursquare error:(NSDictionary *)errorInfo;
@end
145 changes: 145 additions & 0 deletions BZFoursquare/BZFoursquare.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright (C) 2011 Ba-Z Communication Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#import <UIKit/UIKit.h>
#import "BZFoursquare.h"

#ifndef __has_feature
#define __has_feature(x) 0
#endif

#if __has_feature(objc_arc)
#error This file does not support Objective-C Automatic Reference Counting (ARC)
#endif

#define kAuthorizeBaseURL @"https://foursquare.com/oauth2/authorize"

@interface BZFoursquare ()
@property(nonatomic,copy,readwrite) NSString *clientID;
@property(nonatomic,copy,readwrite) NSString *callbackURL;
@end

@implementation BZFoursquare

@synthesize clientID = clientID_;
@synthesize callbackURL = callbackURL_;
@synthesize version = version_;
@synthesize locale = locale_;
@synthesize sessionDelegate = sessionDelegate_;
@synthesize accessToken = accessToken_;

- (id)initWithClientID:(NSString *)clientID callbackURL:(NSString *)callbackURL {
NSParameterAssert(clientID != nil && callbackURL != nil);
self = [super init];
if (self) {
self.clientID = clientID;
self.callbackURL = callbackURL;
}
return self;
}

- (void)dealloc {
self.clientID = nil;
self.callbackURL = nil;
self.version = nil;
self.locale = nil;
self.sessionDelegate = nil;
self.accessToken = nil;
[super dealloc];
}

- (BOOL)startAuthorization {
NSMutableArray *pairs = [NSMutableArray array];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:clientID_, @"client_id", @"token", @"response_type", callbackURL_, @"redirect_uri", nil];
for (NSString *key in parameters) {
NSString *value = [parameters objectForKey:key];
CFStringRef escapedValue = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)value, NULL, CFSTR("%:/?#[]@!$&'()*+,;="), kCFStringEncodingUTF8);
NSMutableString *pair = [[key mutableCopy] autorelease];
[pair appendString:@"="];
[pair appendString:(NSString *)escapedValue];
[pairs addObject:pair];
CFRelease(escapedValue);
}
NSString *URLString = kAuthorizeBaseURL;
NSMutableString *mURLString = [[URLString mutableCopy] autorelease];
[mURLString appendString:@"?"];
[mURLString appendString:[pairs componentsJoinedByString:@"&"]];
NSURL *URL = [NSURL URLWithString:mURLString];
BOOL result = [[UIApplication sharedApplication] openURL:URL];
if (!result) {
NSLog(@"*** %s: cannot open url \"%@\"", __PRETTY_FUNCTION__, URL);
}
return result;
}

- (BOOL)handleOpenURL:(NSURL *)url {
if (![[url absoluteString] hasPrefix:callbackURL_]) {
return NO;
}
NSString *fragment = [url fragment];
NSArray *pairs = [fragment componentsSeparatedByString:@"&"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *key = [kv objectAtIndex:0];
NSString *val = [[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[parameters setObject:val forKey:key];
}
self.accessToken = [parameters objectForKey:@"access_token"];
if (accessToken_) {
if ([sessionDelegate_ respondsToSelector:@selector(foursquareDidAuthorize:)]) {
[sessionDelegate_ foursquareDidAuthorize:self];
}
} else {
if ([sessionDelegate_ respondsToSelector:@selector(foursquareDidNotAuthorize:error:)]) {
[sessionDelegate_ foursquareDidNotAuthorize:self error:parameters];
}
}
return YES;
}

- (void)invalidateSession {
self.accessToken = nil;
}

- (BOOL)isSessionValid {
return (accessToken_ != nil);
}

- (BZFoursquareRequest *)requestWithPath:(NSString *)path HTTPMethod:(NSString *)HTTPMethod parameters:(NSDictionary *)parameters delegate:(id<BZFoursquareRequestDelegate>)delegate {
NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:parameters];
if ([self isSessionValid]) {
[mDict setObject:accessToken_ forKey:@"oauth_token"];
}
if (version_) {
[mDict setObject:version_ forKey:@"v"];
}
if (locale_) {
[mDict setObject:locale_ forKey:@"locale"];
}
return [[[BZFoursquareRequest alloc] initWithPath:path HTTPMethod:HTTPMethod parameters:mDict delegate:delegate] autorelease];
}

@end
66 changes: 66 additions & 0 deletions BZFoursquare/BZFoursquareRequest.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2011 Ba-Z Communication Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#import <Foundation/Foundation.h>

@protocol BZFoursquareRequestDelegate;

@interface BZFoursquareRequest : NSObject {
NSString *path_;
NSString *HTTPMethod_;
NSDictionary *parameters_;
id<BZFoursquareRequestDelegate> delegate_;
NSURLConnection *connection_;
NSMutableData *responseData_;
NSDictionary *meta_;
NSArray *notifications_;
NSDictionary *response_;
}
@property(nonatomic,copy,readonly) NSString *path;
@property(nonatomic,copy,readonly) NSString *HTTPMethod;
@property(nonatomic,copy,readonly) NSDictionary *parameters;
@property(nonatomic,assign) id<BZFoursquareRequestDelegate> delegate;
// responses
@property(nonatomic,copy,readonly) NSDictionary *meta;
@property(nonatomic,copy,readonly) NSArray *notifications;
@property(nonatomic,copy,readonly) NSDictionary *response;

+ (NSURL *)baseURL;

- (id)initWithPath:(NSString *)path HTTPMethod:(NSString *)HTTPMethod parameters:(NSDictionary *)parameters delegate:(id<BZFoursquareRequestDelegate>)delegate;

- (void)start;
- (void)cancel;

@end

@protocol BZFoursquareRequestDelegate <NSObject>
@optional
- (void)requestDidStartLoading:(BZFoursquareRequest *)request;
- (void)requestDidFinishLoading:(BZFoursquareRequest *)request;
- (void)request:(BZFoursquareRequest *)request didFailWithError:(NSError *)error;
@end

FOUNDATION_EXPORT NSString * const BZFoursquareErrorDomain;
Loading

0 comments on commit 93706c0

Please sign in to comment.