-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKMSResponseCollection.m
99 lines (79 loc) · 2.29 KB
/
KMSResponseCollection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by Sam Deane on 06/11/2012.
// Copyright 2012 Karelia Software. All rights reserved.
//
#import "KMSResponseCollection.h"
#import "KMSRegExResponder.h"
#import "KMSServer.h"
@interface KMSResponseCollection()
@property (strong, nonatomic) NSDictionary* sets;
@property (strong, nonatomic) NSDictionary* responses;
@end
@implementation KMSResponseCollection
#pragma mark - Object Lifecycle
+ (KMSResponseCollection*)collectionWithURL:(NSURL *)url
{
KMSResponseCollection* collection = [[KMSResponseCollection alloc] initWithURL:url];
return [collection autorelease];
}
- (id)initWithURL:(NSURL*)url
{
KMSAssert(url != nil);
if ((self = [super init]) != nil)
{
NSError* error = nil;
NSData* data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
NSDictionary* info = nil;
if (data)
{
info = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (info)
{
self.sets = info[@"sets"];
self.responses = info[@"responses"];
self.scheme = info[@"scheme"];
}
else
{
KMSLog(@"failed to load response collection with error: %@", error);
[self release];
self = nil;
}
}
return self;
}
- (void)dealloc
{
[_sets release];
[_responses release];
[super dealloc];
}
- (NSArray*)responsesWithName:(NSString*)name
{
NSMutableArray* responses = [NSMutableArray array];
NSDictionary* set = self.sets[name];
NSArray* responseNames = set[@"responses"];
for (NSString* responseName in responseNames)
{
NSDictionary* response = self.responses[responseName];
if (response)
{
NSMutableArray* array = [NSMutableArray array];
[array addObject:response[@"pattern"]];
[array addObjectsFromArray:response[@"commands"]];
[responses addObject:array];
}
else
{
KMSLog(@"unknown response %@ in set %@", responseName, name);
}
}
return responses;
}
- (KMSRegExResponder*)responderWithName:(NSString *)name
{
NSArray* responses = [self responsesWithName:name];
return [KMSRegExResponder responderWithResponses:responses];
}
@end