forked from darrylhthomas/StackMob_iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackMobQuery.m
More file actions
132 lines (101 loc) · 4.35 KB
/
Copy pathStackMobQuery.m
File metadata and controls
132 lines (101 loc) · 4.35 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
//
// StackMobQuery.m
// StackMobiOS
//
// Created by Jordan West on 10/14/11.
// Copyright (c) 2011 StackMob, Inc. All rights reserved.
//
#import "StackMobQuery.h"
const double earthRadianInMi = 3956.6;
const double earthRadiamInKm = 6367.5;
@interface StackMobQuery (Private)
- (NSString *)keyForField:(NSString *)f andOperator:(NSString *)op;
- (void)setGeoParam:(SMGeoPoint *)pt withRadius:(double)r andDiv:(double)d forField:(NSString *)f andOperator:(NSString *)o;
@end
@implementation StackMobQuery
@synthesize params = _params;
@synthesize headers = _headers;
+ (StackMobQuery *)query {
return [[[StackMobQuery alloc] init] autorelease];
}
- (id)init {
self = [super init];
if (self) {
_params = [[NSMutableDictionary alloc] initWithCapacity:4];
_headers = [[NSMutableDictionary alloc] initWithCapacity:4];
}
return self;
}
- (void)field:(NSString *)f mustEqualValue:(id)v {
[self.params setValue:v forKey:f];
}
- (void)field:(NSString *)f mustBeLessThanValue:(id)v {
[self.params setValue:v forKey:[self keyForField:f andOperator:@"lt"]];
}
- (void)field:(NSString *)f mustBeLessThanOrEqualToValue:(id)v {
[self.params setValue:v forKey:[self keyForField:f andOperator:@"lte"]];
}
- (void)field:(NSString *)f mustBeGreaterThanValue:(id)v {
[self.params setValue:v forKey:[self keyForField:f andOperator:@"gt"]];
}
- (void)field:(NSString *)f mustBeGreaterThanOrEqualToValue:(id)v {
[self.params setValue:v forKey:[self keyForField:f andOperator:@"gte"]];
}
- (void)field:(NSString *)f mustBeOneOf:(NSArray *)arr {
[self.params setValue:arr forKey:[self keyForField:f andOperator:@"in"]];
}
- (void)field:(NSString *)f centeredAt:(SMGeoPoint *)point mustBeWithinMi:(double)mi {
[self setGeoParam:point withRadius:mi andDiv:earthRadianInMi forField:f andOperator:@"within"];
}
- (void)field:(NSString *)f centeredAt:(SMGeoPoint *)point mustBeWithinKm:(double)km {
[self setGeoParam:point withRadius:km andDiv:earthRadiamInKm forField:f andOperator:@"within"];
}
- (void)field:(NSString *)f mustBeNear:(SMGeoPoint *)point {
[self.params setValue:[point stringValue] forKey:[self keyForField:f andOperator:@"near"]];
}
- (void)field:(NSString *)f mustBeNear:(SMGeoPoint *)point withinMi:(double)mi {
[self setGeoParam:point withRadius:mi andDiv:earthRadianInMi forField:f andOperator:@"near"];
}
- (void)field:(NSString *)f mustBeNear:(SMGeoPoint *)point withinKm:(double)km {
[self setGeoParam:point withRadius:km andDiv:earthRadiamInKm forField:f andOperator:@"near"];
}
- (void)field:(NSString *)f mustBeWithinBoxWithLowerLeft:(SMGeoPoint *)lowerLeft andUpperRight:(SMGeoPoint *)upperRight {
NSString *boxString = [NSString stringWithFormat:@"%@,%@", [lowerLeft stringValue], [upperRight stringValue]];
[self.params setValue:boxString forKey:[self keyForField:f andOperator:@"within"]];
}
- (void)setExpandDepth:(NSUInteger)depth {
[self.headers setValue:[NSString stringWithFormat:@"%d", depth] forKey:@"X-StackMob-Expand"];
}
- (void)setRangeStart:(NSUInteger)start andEnd:(NSUInteger)end {
[self.headers setValue:[NSString stringWithFormat:@"objects=%d-%d", start, end] forKey:@"Range"];
}
- (void)orderByField:(NSString *)f withDirection:(SMOrderDirection)dir {
NSString *orderStr;
NSString *currentHeader = [self.headers objectForKey:@"X-StackMob-OrderBy"];
NSString *newHeaderValue;
if (dir == SMOrderAscending) {
orderStr = @"asc";
} else {
orderStr = @"desc";
}
if ([currentHeader isKindOfClass:[NSString class]]) {
newHeaderValue = [NSString stringWithFormat:@"%@,%@:%@", currentHeader, f, orderStr];
} else {
newHeaderValue = [NSString stringWithFormat:@"%@:%@", f, orderStr];
}
[self.headers setValue:newHeaderValue forKey:@"X-StackMob-OrderBy"];
}
- (NSString *)keyForField:(NSString *)f andOperator:(NSString *)op {
return [NSString stringWithFormat:@"%@[%@]", f, op];
}
- (void)setGeoParam:(SMGeoPoint *)pt withRadius:(double)r andDiv:(double)d forField:(NSString *)f andOperator:(NSString *)o {
NSNumber *radius = [NSNumber numberWithDouble:r / d];
NSString *arg = [NSString stringWithFormat:@"%@,%@", [pt stringValue], radius];
[self.params setValue:arg forKey:[self keyForField:f andOperator:o]];
}
- (void)dealloc {
self.params = nil;
self.headers = nil;
[super dealloc];
}
@end