Source

ios_ezcomet_api / ezcomet_api / ezcomet / EZComet.m

Full commit
//
//  EZComet.m
//  ezcomet_api
//
//  Created by Chung-Che Hsieh on 12/7/30.
//  Copyright (c) 2012年 a761007@gmail.com. All rights reserved.
//

#import "EZComet.h"

#import "ASIFormDataRequest.h"
#import "JSON.h"

#define API_HOSTNAME @"https://api.ezcomet.com"
#define WRITE_ENDPOINT @"/write"
#define READ_ENDPOINT @"/read"

#define API_KEY @"your api key"

@implementation EZComet

static EZComet *instance=nil;

@synthesize delegate;

#pragma mark -
#pragma mark initialize
+ (id) shareInstance
{
    if (instance==nil) {
        instance = [[EZComet alloc] init];
    }
    return instance;
}

#pragma mark -
#pragma push message request delegate
- (void) pushMessageRequestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    
    if (![[data objectForKey:@"code"] isEqualToString:@"ok"]) {
        [self pushMessageRequestFailed:request];
    } else {
        NSLog(@"%@", [request responseString]);
        if (self.delegate != nil && 
            [self.delegate respondsToSelector:@selector(ezcomet:pushMessageDidSucessWithTick:)]) {
            [self.delegate ezcomet:self pushMessageDidSucessWithTick:[data objectForKey:@"tick"]];
        }
    }
}

- (void) pushMessageRequestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    NSString *err_msg = [data objectForKey:@"msg"];
    NSLog(@"%@", [request responseString]);
    if (self.delegate != nil && 
        [self.delegate respondsToSelector:@selector(ezcomet:pushMessageDidFailWithErrorMessage:)]) {
        [self.delegate ezcomet:self pushMessageDidFailWithErrorMessage:err_msg];
    }
}

#pragma mark -
#pragma push message
- (void) pushMessageWithUserName:(NSString *)user_name 
                         Channel:(NSString *)channel 
                         Message:(NSString *)message
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSString *url = [NSString stringWithFormat:@"%@%@", 
                     API_HOSTNAME, WRITE_ENDPOINT];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
    [request setValidatesSecureCertificate:NO];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(pushMessageRequestFinished:)];
    [request setDidFailSelector:@selector(pushMessageRequestFailed:)];
    [request addPostValue:API_KEY forKey:@"api_key"];
    [request addPostValue:[NSString stringWithFormat:@"%@-%@", user_name, channel] forKey:@"qname"];
    [request addPostValue:message forKey:@"msg"];
    [request startAsynchronous];    
}

#pragma mark -
#pragma mark get messages request delegate
- (void) getMessagesRequestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    
    if (![[data objectForKey:@"code"] isEqualToString:@"ok"]) {
        [self pushMessageRequestFailed:request];
    } else {
        NSLog(@"%@", [request responseString]);
        if (self.delegate != nil && 
            [self.delegate respondsToSelector:@selector(ezcomet:getMessagesDidSucessWithMessages:)]) {
            [self.delegate ezcomet:self getMessagesDidSucessWithMessages:[data objectForKey:@"msgs"]];
        }
    }
}

- (void) getMessagesRequestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    NSString *err_msg = [data objectForKey:@"msg"];
    NSLog(@"%@", [request responseString]);
    if (self.delegate != nil && 
        [self.delegate respondsToSelector:@selector(ezcomet:gethMessagesDidFailWithErrorMessage:)]) {
        [self.delegate ezcomet:self getMessagesDidFailWithErrorMessage:err_msg];
    }
}

#pragma mark -
#pragma mark get messages
- (void) getMessagesWithUserName:(NSString *)user_name 
                         Channel:(NSString *)channel 
                            Tick:(NSString *)tick
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSString *qname = [NSString stringWithFormat:@"%@-%@", user_name, channel];
    NSString *url = [NSString stringWithFormat:@"%@%@?api_key=%@&qname=%@&tick=%@", 
                     API_HOSTNAME, READ_ENDPOINT, API_KEY, qname, (tick==nil)?@"0":tick];
    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
    [request setValidatesSecureCertificate:NO];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(getMessagesRequestFinished:)];
    [request setDidFailSelector:@selector(getMessagesRequestFailed:)];
    // TODO: set cookies 
    [request startAsynchronous];
}

#pragma mark -
#pragma flush request messages delegate
- (void) flushMessagesRequestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    
    if (![[data objectForKey:@"code"] isEqualToString:@"ok"]) {
        [self flushMessagesRequestFailed:request];
    } else {
        NSLog(@"%@", [request responseString]);
        if (self.delegate != nil && 
            [self.delegate respondsToSelector:@selector(ezcomet:flushMessagesDidSucessWithTick:)]) {
            [self.delegate ezcomet:self flushMessagesDidSucessWithTick:[data objectForKey:@"tick"]];
        }
    }
}

- (void) flushMessagesRequestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSDictionary *data = [[request responseString] JSONValue];
    NSString *err_msg = [data objectForKey:@"msg"];
    NSLog(@"%@", [request responseString]);
    if (self.delegate != nil && 
        [self.delegate respondsToSelector:@selector(ezcomet:flushMessagesDidFailWithErrorMessage:)]) {
        [self.delegate ezcomet:self flushMessagesDidFailWithErrorMessage:err_msg];
    }
}
#pragma mark -
#pragma mark flush messages
- (void) flushMessagesWithUserName:(NSString *)user_name Channel:(NSString *)channel
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSString *url = [NSString stringWithFormat:@"%@%@", 
                     API_HOSTNAME, WRITE_ENDPOINT];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
    [request setValidatesSecureCertificate:NO];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(flushMessagesRequestFinished:)];
    [request setDidFailSelector:@selector(flushMessagesRequestFailed:)];
    [request addPostValue:API_KEY forKey:@"api_key"];
    [request addPostValue:[NSString stringWithFormat:@"%@-%@", user_name, channel] forKey:@"qname"];
    [request addPostValue:@"1" forKey:@"flush"];
    [request startAsynchronous];
}

@end