Created
April 2, 2014 22:39
-
-
Save LastZactionHero/9944746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// Download Langauges | |
// | |
// VITLanguage: An individual language, name ("English", "Spanish") and code | |
// ("en", "es") | |
// | |
// VITLanguageManager: Download an array of VITLanguages from API | |
// | |
//------------------------------------------------------------------------------ | |
//------------------------------------------------------------------------------ | |
// VITLanguage | |
//------------------------------------------------------------------------------ | |
@interface VITLanguage : NSObject<NSCoding> | |
@property (nonatomic, copy) NSString *name; | |
@property (nonatomic, copy) NSString *languageCode; | |
- (BOOL)isEnglish; | |
- (NSComparisonResult)compareTo:(VITLanguage*)compare; | |
@end | |
@implementation VITLanguage | |
@synthesize name = _name; | |
@synthesize languageCode = _languageCode; | |
- (BOOL)isEnglish { | |
return [_name isEqualToString:@"English"]; | |
} | |
// Sorting method for lists putting English first | |
- (NSComparisonResult)compareTo:(VITLanguage*)compare { | |
NSComparisonResult result = NSOrderedSame; | |
// English is top of list | |
if([self isEnglish]){ | |
result = NSOrderedAscending; | |
} else if([compare isEnglish]){ | |
result = NSOrderedDescending; | |
} else { | |
result = [[self name] caseInsensitiveCompare:[compare name]]; | |
} | |
return result; | |
} | |
@end | |
//------------------------------------------------------------------------------ | |
// VITLanguageManager | |
//------------------------------------------------------------------------------ | |
#import <Foundation/Foundation.h> | |
#import "VITLanguage.h" | |
@interface VITLanguageManager : NSObject<NSCoding> | |
- (void)addLanguage:(VITLanguage*)language; | |
- (void)refreshLanguages; | |
@end | |
@interface VITLanguageManager() { | |
NSMutableArray *languages; | |
} | |
@end | |
@implementation VITLanguageManager | |
- (id)init { | |
self = [super init]; | |
languages = [[NSMutableArray alloc] init]; | |
return self; | |
} | |
- (void)addLanguage:(VITLanguage *)language { | |
[languages addObject:language]; | |
} | |
- (void)refreshLanguages { | |
// Response Mapping and Descriptor | |
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[VITLanguage class]]; | |
[responseMapping addAttributeMappingsFromDictionary:@{ | |
@"name" : @"name", | |
@"language_code" : @"languageCode" | |
}]; | |
NSIndexSet *responseCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:@"/languages" keyPath:@"" statusCodes:responseCodes]; | |
// Object Manager | |
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:<VERBALIZEIT API HOST>]]; | |
[manager addResponseDescriptor:responseDescriptor]; | |
// Parameters | |
NSDictionary *parameters = @{@"auth_token" : <CUSTOMER AUTH TOKEN>}; | |
// Get Languages | |
[manager getObjectsAtPath:@"/languages" parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
// Add language to list | |
NSArray *responseLanguages = [mappingResult array]; | |
for(VITLanguage* language in responseLanguages) { | |
[[VITLanguageManager shared] addLanguage:language]; | |
} | |
languages = [[languages sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { | |
return [obj1 compareTo:obj2]; | |
}] mutableCopy]; | |
// Handle Success | |
} failure:^(RKObjectRequestOperation *operation, NSError *error) { | |
// Handle Errors | |
}]; | |
} | |
@end | |
//------------------------------------------------------------------------------ | |
// Capability Token | |
// Request a capability token from the API | |
//------------------------------------------------------------------------------ | |
@interface VITCapabilityToken : NSObject | |
@property (nonatomic, copy) NSString* token; | |
@end | |
@implementation VITCapabilityToken | |
@synthesize token = _token; | |
-(void)request { | |
// Response Mapping and Descriptor | |
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[VITCapabilityToken class]]; | |
[responseMapping addAttributeMappingsFromDictionary:@{@"token": @"token"}]; | |
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:@"/capability_tokens" keyPath:@"" statusCodes:statusCodes]; | |
// Manager | |
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:<VERBALIZEIT API HOST>]]; | |
[manager addResponseDescriptor:responseDescriptor]; | |
// Parameters | |
NSDictionary *parameters = @{@"auth_token" : <CUSTOMER AUTH TOKEN>}; | |
// Request Capability Token | |
[manager postObject:nil path:@"/capability_tokens" parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
// Handle Success Case | |
VITCapabilityToken *responseToken = (VITCapabilityToken*)[mappingResult firstObject]; | |
} failure:^(RKObjectRequestOperation *operation, NSError *error) { | |
// Handle Failure Case | |
}]; | |
} | |
@end | |
//------------------------------------------------------------------------------ | |
// Starting a Call | |
// A method for initiating a call via Twilio using a capability token and | |
// language | |
//------------------------------------------------------------------------------ | |
- (void)startCall:(VITCapabilityToken *)capabilityToken (VITLanguage*)language { | |
_device = [[TCDevice alloc] initWithCapabilityToken:[capabilityToken token] delegate:self]; | |
[_device setOutgoingSoundEnabled:YES]; | |
NSDictionary *parameters = @{@"auth_token" : <CUSTOMER AUTH TOKEN>, @"language_code" : [_language languageCode]}; | |
_connection = [_device connect:parameters delegate:self]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment