forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRKResponseDescriptor.m
More file actions
175 lines (149 loc) · 5.96 KB
/
Copy pathRKResponseDescriptor.m
File metadata and controls
175 lines (149 loc) · 5.96 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// RKResponseDescriptor.m
// RestKit
//
// Created by Blake Watters on 8/16/12.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "RKPathMatcher.h"
#import "RKResponseDescriptor.h"
#import "RKHTTPUtilities.h"
#import "RKMapping.h"
// Cloned from AFStringFromIndexSet -- method should be non-static for reuse
NSString *RKStringFromIndexSet(NSIndexSet *indexSet);
NSString *RKStringFromIndexSet(NSIndexSet *indexSet)
{
NSCParameterAssert(indexSet);
NSMutableString *string = [NSMutableString string];
NSRange range = NSMakeRange([indexSet firstIndex], 1);
while (range.location != NSNotFound) {
NSUInteger nextIndex = [indexSet indexGreaterThanIndex:range.location];
while (nextIndex == range.location + range.length) {
range.length++;
nextIndex = [indexSet indexGreaterThanIndex:nextIndex];
}
if (string.length) {
[string appendString:@","];
}
if (range.length == 1) {
[string appendFormat:@"%lu", (unsigned long) range.location];
} else {
NSUInteger firstIndex = range.location;
NSUInteger lastIndex = firstIndex + range.length - 1;
[string appendFormat:@"%lu-%lu", (unsigned long) firstIndex, (unsigned long) lastIndex];
}
range.location = nextIndex;
range.length = 1;
}
return string;
}
extern NSString *RKStringDescribingRequestMethod(RKRequestMethod method);
@interface RKResponseDescriptor ()
@property (nonatomic, strong, readwrite) RKMapping *mapping;
@property (nonatomic, assign, readwrite) RKRequestMethod method;
@property (nonatomic, copy, readwrite) NSString *pathPattern;
@property (nonatomic, copy, readwrite) NSString *keyPath;
@property (nonatomic, copy, readwrite) NSIndexSet *statusCodes;
@end
@implementation RKResponseDescriptor
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
pathPattern:(NSString *)pathPattern
keyPath:(NSString *)keyPath
statusCodes:(NSIndexSet *)statusCodes
{
return [self responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:pathPattern keyPath:keyPath statusCodes:statusCodes];
}
#pragma clang diagnostic pop
+ (instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
method:(RKRequestMethod)method
pathPattern:(NSString *)pathPattern
keyPath:(NSString *)keyPath
statusCodes:(NSIndexSet *)statusCodes
{
NSParameterAssert(mapping);
RKResponseDescriptor *mappingDescriptor = [self new];
mappingDescriptor.mapping = mapping;
mappingDescriptor.method = method;
mappingDescriptor.pathPattern = pathPattern;
mappingDescriptor.keyPath = keyPath;
mappingDescriptor.statusCodes = statusCodes;
return mappingDescriptor;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p method=%@ pathPattern=%@ keyPath=%@ statusCodes=%@ : %@>",
NSStringFromClass([self class]), self, RKStringDescribingRequestMethod(self.method), self.pathPattern, self.keyPath, self.statusCodes ? RKStringFromIndexSet(self.statusCodes) : self.statusCodes, self.mapping];
}
- (BOOL)matchesPath:(NSString *)path
{
if (!self.pathPattern || !path) return YES;
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:self.pathPattern];
return [pathMatcher matchesPath:path tokenizeQueryStrings:NO parsedArguments:nil];
}
- (BOOL)matchesURL:(NSURL *)URL
{
NSString *pathAndQueryString = RKPathAndQueryStringFromURLRelativeToURL(URL, self.baseURL);
if (self.baseURL) {
if (! RKURLIsRelativeToURL(URL, self.baseURL)) return NO;
return [self matchesPath:pathAndQueryString];
} else {
return [self matchesPath:pathAndQueryString];
}
}
- (BOOL)matchesResponse:(NSHTTPURLResponse *)response
{
if (! [self matchesURL:response.URL]) return NO;
if (self.statusCodes) {
if (! [self.statusCodes containsIndex:response.statusCode]) {
return NO;
}
}
return YES;
}
- (BOOL)matchesMethod:(RKRequestMethod)method
{
return self.method & method;
}
- (BOOL)isEqual:(id)object
{
if (self == object) {
return YES;
}
if ([self class] != [object class]) {
return NO;
}
return [self isEqualToResponseDescriptor:object];
}
#define NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger))
#define NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (NSUINT_BIT - howmuch)))
- (NSUInteger)hash
{
return NSUINTROTATE(NSUINTROTATE(NSUINTROTATE([self.mapping hash], NSUINT_BIT / 4) ^ [self.pathPattern hash], NSUINT_BIT / 4) ^ [self.keyPath hash], NSUINT_BIT / 4) ^ [self.statusCodes hash];
}
- (BOOL)isEqualToResponseDescriptor:(RKResponseDescriptor *)otherDescriptor
{
if (![otherDescriptor isKindOfClass:[RKResponseDescriptor class]]) {
return NO;
}
return
[self.mapping isEqualToMapping:otherDescriptor.mapping] &&
self.method == otherDescriptor.method &&
[self.pathPattern isEqualToString:otherDescriptor.pathPattern] &&
[self.keyPath isEqualToString:otherDescriptor.keyPath] &&
[self.statusCodes isEqualToIndexSet:otherDescriptor.statusCodes];
}
@end