Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow the user to authenticate.

Signed-off-by: Chris Brown <xoebus@xoeb.us>
  • Loading branch information...
commit 58c0fc57903f5c97586511bb8b7d0b8d7071d773 1 parent 6c7f0e1
@xoebus authored
View
7 Woggle/TPCampfireClient.h
@@ -8,10 +8,17 @@
#import <Foundation/Foundation.h>
+#import "TPCampfireUser.h"
+
@interface TPCampfireClient : NSObject
@property (copy, atomic) NSString *domain;
+@property (copy, atomic) NSString *authToken;
- (id)initWithDomain:(NSString *)domain;
+- (id)initWithDomain:(NSString *)domain token:(NSString *)token;
+- (void)getUserTokenWithUsername:(NSString *)username
+ password:(NSString *)password
+ completionHandler:(void (^)(TPCampfireClient *client, TPCampfireUser *user, NSError *error))handler;
@end
View
49 Woggle/TPCampfireClient.m
@@ -8,11 +8,20 @@
#import "TPCampfireClient.h"
+#import "ASIHTTPRequest.h"
+
+@interface TPCampfireClient ()
+
+- (NSString *)campfireUrl;
+
+@end
+
@implementation TPCampfireClient
#pragma mark - Properties
@synthesize domain = _domain;
+@synthesize authToken = _authToken;
#pragma mark - Constructors
@@ -28,4 +37,44 @@ - (id)initWithDomain:(NSString *)domain
return self;
}
+- (id)initWithDomain:(NSString *)domain token:(NSString *)token;
+{
+ self = [self initWithDomain:domain];
+
+ if (self)
+ {
+ _authToken = token;
+ }
+
+ return self;
+}
+
+- (void)getUserTokenWithUsername:(NSString *)username
+ password:(NSString *)password
+ completionHandler:(void (^)(TPCampfireClient *client, TPCampfireUser *user, NSError *error))handler;
+{
+ NSString *url = [NSString stringWithFormat:@"%@/users/me.json", self.campfireUrl];
+ __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
+ [request setUsername:username];
+ [request setPassword:password];
+
+ [request setCompletionBlock:^{
+ NSDictionary *userDict = [NSJSONSerialization JSONObjectWithData:[request responseData]
+ options:NSJSONReadingMutableContainers
+ error:nil];
+ _authToken = [[userDict valueForKey:@"user"] valueForKey:@"api_auth_token"];
+
+ TPCampfireUser *user = [[TPCampfireUser alloc] initWithDictionary:userDict];
+
+ handler(self, user, [request error]);
+ }];
+
+ [request startAsynchronous];
+}
+
+- (NSString *)campfireUrl
+{
+ return [NSString stringWithFormat:@"https://%@.campfirenow.com", self.domain];
+}
+
@end
View
6 WoggleTests/TPCampfireClientSpec.m
@@ -1,11 +1,11 @@
-#include "Kiwi.h"
+#import "Kiwi.h"
-#include "TPCampfireClient.h"
+#import "TPCampfireClient.h"
SPEC_BEGIN(ClientSpec)
describe(@"a campfire client", ^{
- describe(@"has a constructor", ^{
+ it(@"should have a constructor", ^{
TPCampfireClient *client = [[TPCampfireClient alloc] initWithDomain:@"teamproa"];
[[[client domain] should] equal:@"teamproa"];
});
View
4 WoggleTests/TPCampfireMessageSpec.m
@@ -1,6 +1,6 @@
-#include "Kiwi.h"
+#import "Kiwi.h"
-#include "TPCampfireMessage.h"
+#import "TPCampfireMessage.h"
SPEC_BEGIN(MessageSpec)
View
4 WoggleTests/TPCampfireUserSpec.m
@@ -1,6 +1,6 @@
-#include "Kiwi.h"
+#import "Kiwi.h"
-#include "TPCampfireUser.h"
+#import "TPCampfireUser.h"
SPEC_BEGIN(UserSpec)
Please sign in to comment.
Something went wrong with that request. Please try again.