Skip to content

Commit 2fc7f3f

Browse files
authored
Merge pull request #37 from Yelp/ssheldon_search_query
Add YLPQuery to allow easier search customization
2 parents e906d9c + 7904aa1 commit 2fc7f3f

File tree

9 files changed

+320
-121
lines changed

9 files changed

+320
-121
lines changed

Classes/Client/YLPClient+Search.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@class YLPCoordinate;
1313
@class YLPGeoBoundingBox;
1414
@class YLPGeoCoordinate;
15+
@class YLPQuery;
1516
@class YLPSearch;
1617

1718
NS_ASSUME_NONNULL_BEGIN
@@ -20,6 +21,9 @@ typedef void(^YLPSearchCompletionHandler)(YLPSearch *_Nullable search, NSError *
2021

2122
@interface YLPClient (Search)
2223

24+
- (void)searchWithQuery:(YLPQuery *)query
25+
completionHandler:(YLPSearchCompletionHandler)completionHandler;
26+
2327
- (void)searchWithLocation:(NSString *)location
2428
currentLatLong:(nullable YLPCoordinate *)cll
2529
term:(nullable NSString *)term
@@ -50,7 +54,7 @@ typedef void(^YLPSearchCompletionHandler)(YLPSearch *_Nullable search, NSError *
5054
sort:(YLPSortType)sort
5155
completionHandler:(YLPSearchCompletionHandler)completionHandler;
5256

53-
- (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordiante
57+
- (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate
5458
completionHandler:(YLPSearchCompletionHandler)completionHandler;
5559

5660
@end

Classes/Client/YLPClient+Search.m

Lines changed: 32 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
#import "YLPCoordinate.h"
1212
#import "YLPGeoBoundingBox.h"
1313
#import "YLPGeoCoordinate.h"
14+
#import "YLPQuery.h"
15+
#import "YLPQueryPrivate.h"
1416
#import "YLPResponsePrivate.h"
1517
#import "YLPClientPrivate.h"
1618

1719
@implementation YLPClient (Search)
1820

1921
- (void)searchWithLocation:(NSString *)location
2022
completionHandler:(YLPSearchCompletionHandler)completionHandler {
21-
22-
NSDictionary *params = @{@"location": location};
23-
[self searchWithParams:params completionHandler:completionHandler];
23+
YLPQuery *query = [[YLPQuery alloc] initWithLocation:location currentLatLong:nil];
24+
[self searchWithQuery:query completionHandler:completionHandler];
2425
}
2526

2627
- (void)searchWithLocation:(NSString *)location
@@ -30,9 +31,12 @@ - (void)searchWithLocation:(NSString *)location
3031
offset:(NSUInteger)offset
3132
sort:(YLPSortType)sort
3233
completionHandler:(YLPSearchCompletionHandler)completionHandler {
33-
34-
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:@{@"location": location}];
35-
[self buildParamsAndCallSearch:params currentLatLong:cll term:term limit:limit offset:offset sort:sort completionHandler:completionHandler];
34+
YLPQuery *query = [[YLPQuery alloc] initWithLocation:location currentLatLong:cll];
35+
query.term = term;
36+
query.limit = limit;
37+
query.offset = offset;
38+
query.sort = sort;
39+
[self searchWithQuery:query completionHandler:completionHandler];
3640
}
3741

3842
- (void)searchWithBounds:(YLPGeoBoundingBox *)bounds
@@ -41,15 +45,19 @@ - (void)searchWithBounds:(YLPGeoBoundingBox *)bounds
4145
offset:(NSUInteger)offset
4246
sort:(YLPSortType)sort
4347
completionHandler:(YLPSearchCompletionHandler)completionHandler {
44-
45-
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:@{@"bounds": bounds.description}];
46-
[self buildParamsAndCallSearch:params currentLatLong:cll term:term limit:limit offset:offset sort:sort completionHandler:completionHandler];
48+
YLPQuery *query = [[YLPQuery alloc] initWithBounds:bounds];
49+
query.currentLatLong = cll;
50+
query.term = term;
51+
query.limit = limit;
52+
query.offset = offset;
53+
query.sort = sort;
54+
[self searchWithQuery:query completionHandler:completionHandler];
4755
}
4856

4957
- (void)searchWithBounds:(YLPGeoBoundingBox *)bounds
5058
completionHandler:(YLPSearchCompletionHandler)completionHandler {
51-
52-
[self searchWithBounds:bounds currentLatLong:nil term:nil limit:0 offset:0 sort:0 completionHandler:completionHandler];
59+
YLPQuery *query = [[YLPQuery alloc] initWithBounds:bounds];
60+
[self searchWithQuery:query completionHandler:completionHandler];
5361
}
5462

5563
- (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate
@@ -58,63 +66,28 @@ - (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate
5866
offset:(NSUInteger)offset
5967
sort:(YLPSortType)sort
6068
completionHandler:(YLPSearchCompletionHandler)completionHandler {
61-
62-
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:@{@"ll": geoCoordinate.description}];
63-
[self buildParamsAndCallSearch:params currentLatLong:cll term:term limit:limit offset:offset sort:sort completionHandler:completionHandler];
69+
YLPQuery *query = [[YLPQuery alloc] initWithGeoCoordinate:geoCoordinate];
70+
query.currentLatLong = cll;
71+
query.term = term;
72+
query.limit = limit;
73+
query.offset = offset;
74+
query.sort = sort;
75+
[self searchWithQuery:query completionHandler:completionHandler];
6476
}
6577

66-
- (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordiante
78+
- (void)searchWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate
6779
completionHandler:(YLPSearchCompletionHandler)completionHandler {
68-
69-
[self searchWithGeoCoordinate:geoCoordiante currentLatLong:nil term:nil limit:0 offset:0 sort:0 completionHandler:completionHandler];
70-
}
71-
72-
- (void)buildParamsAndCallSearch:(NSMutableDictionary *)params
73-
currentLatLong:(YLPCoordinate *)cll
74-
term:(NSString *)term
75-
limit:(NSUInteger)limit
76-
offset:(NSUInteger)offset
77-
sort:(YLPSortType)sort
78-
completionHandler:(YLPSearchCompletionHandler)completionHandler {
79-
80-
[params addEntriesFromDictionary:[self paramsWithTerm:term currentLatLong:cll limit:limit offset:offset sort:sort]];
81-
[self searchWithParams:params completionHandler:completionHandler];
82-
}
83-
84-
- (NSDictionary *)paramsWithTerm:(NSString *)term
85-
currentLatLong:(YLPCoordinate *)cll
86-
limit:(NSUInteger)limit
87-
offset:(NSUInteger)offset
88-
sort:(YLPSortType)sort {
89-
90-
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
91-
92-
if (cll) {
93-
params[@"cll"] = cll.description;
94-
}
95-
if (term) {
96-
params[@"term"] = term;
97-
}
98-
if (limit) {
99-
params[@"limit"] = [NSNumber numberWithInteger:limit];
100-
}
101-
if (offset) {
102-
params[@"offset"] = [NSNumber numberWithInteger:offset];
103-
}
104-
if (sort) {
105-
params[@"sort"] = [NSNumber numberWithInteger:sort];
106-
}
107-
108-
return params;
80+
YLPQuery *query = [[YLPQuery alloc] initWithGeoCoordinate:geoCoordinate];
81+
[self searchWithQuery:query completionHandler:completionHandler];
10982
}
11083

11184
- (NSURLRequest *)searchRequestWithParams:(NSDictionary *)params {
11285
return [self requestWithPath:@"/v2/search/" params:params];
11386
}
11487

115-
- (void)searchWithParams:(NSDictionary *)params
116-
completionHandler:(YLPSearchCompletionHandler)completionHandler {
117-
88+
- (void)searchWithQuery:(YLPQuery *)query
89+
completionHandler:(YLPSearchCompletionHandler)completionHandler {
90+
NSDictionary *params = [query parameters];
11891
NSURLRequest *req = [self searchRequestWithParams:params];
11992

12093
[self queryWithRequest:req completionHandler:^(NSDictionary *responseDict, NSError *error) {

Classes/Request/YLPQuery.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// YLPQuery.h
3+
// YelpAPI
4+
//
5+
// Created by Steven Sheldon on 6/26/16.
6+
//
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "YLPSortType.h"
11+
12+
@class YLPCoordinate;
13+
@class YLPGeoBoundingBox;
14+
@class YLPGeoCoordinate;
15+
16+
NS_ASSUME_NONNULL_BEGIN
17+
18+
@interface YLPQuery : NSObject
19+
20+
/**
21+
Initializes a query with a location specified by text.
22+
@param location the particular neighborhood, address or city to search in
23+
@param cll location as a hint to the geocoder to disambiguate the location text
24+
*/
25+
- (instancetype)initWithLocation:(NSString *)location
26+
currentLatLong:(nullable YLPCoordinate *)cll;
27+
28+
/**
29+
Initializes a query with a location specified by a geographical bounding box.
30+
@param bounds bounds within which to search
31+
*/
32+
- (instancetype)initWithBounds:(YLPGeoBoundingBox *)bounds;
33+
34+
/**
35+
Initializes a query with a location specified by a geographic coordinate.
36+
@param geoCoordinate coordinate around which to search
37+
*/
38+
- (instancetype)initWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate;
39+
40+
/**
41+
Search term (e.g. "food", "restaurants"). If term is nil, everything will be searched.
42+
*/
43+
@property (copy, nonatomic, nullable) NSString *term;
44+
45+
/**
46+
Number of business results to return. If 0, the API maximum of 20 results will be returned.
47+
*/
48+
@property (assign, nonatomic) NSUInteger limit;
49+
50+
/**
51+
Amount by which to offset the list of returned business. The default is 0.
52+
*/
53+
@property (assign, nonatomic) NSUInteger offset;
54+
55+
/**
56+
Sort mode for the list of returned businesses. The default is BestMatched.
57+
*/
58+
@property (assign, nonatomic) YLPSortType sort;
59+
60+
/**
61+
Aliases of categories to filter search results with. If none, all results will be returned.
62+
*/
63+
@property (copy, nonatomic, null_resettable) NSArray<NSString *> *categoryFilter;
64+
65+
/**
66+
Search radius in meters. If the value is too large, an AREA_TOO_LARGE error may be returned.
67+
*/
68+
@property (assign, nonatomic) double radiusFilter;
69+
70+
/**
71+
Whether to exclusively search for businesses with deals.
72+
*/
73+
@property (assign, nonatomic) BOOL dealsFilter;
74+
75+
@end
76+
77+
NS_ASSUME_NONNULL_END

Classes/Request/YLPQuery.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// YLPQuery.m
3+
// YelpAPI
4+
//
5+
// Created by Steven Sheldon on 6/26/16.
6+
//
7+
//
8+
9+
#import "YLPQuery.h"
10+
#import "YLPQueryPrivate.h"
11+
#import "YLPCoordinate.h"
12+
#import "YLPGeoBoundingBox.h"
13+
#import "YLPGeoCoordinate.h"
14+
15+
@implementation YLPQuery
16+
17+
- (instancetype)initWithLocation:(NSString *)location currentLatLong:(nullable YLPCoordinate *)cll {
18+
if (self = [super init]) {
19+
_mode = YLPSearchModeLocation;
20+
_location = [location copy];
21+
_currentLatLong = cll;
22+
}
23+
return self;
24+
}
25+
26+
- (instancetype)initWithBounds:(YLPGeoBoundingBox *)bounds {
27+
if (self = [super init]) {
28+
_mode = YLPSearchModeBounds;
29+
_bounds = bounds;
30+
}
31+
return self;
32+
}
33+
34+
- (instancetype)initWithGeoCoordinate:(YLPGeoCoordinate *)geoCoordinate {
35+
if (self = [super init]) {
36+
_mode = YLPSearchModeCoordinate;
37+
_geoCoordinate = geoCoordinate;
38+
}
39+
return self;
40+
}
41+
42+
- (NSArray<NSString *> *)categoryFilter {
43+
return _categoryFilter ?: @[];
44+
}
45+
46+
- (NSDictionary *)parameters {
47+
NSMutableDictionary *params = [NSMutableDictionary dictionary];
48+
switch (self.mode) {
49+
case YLPSearchModeLocation:
50+
params[@"location"] = self.location;
51+
break;
52+
case YLPSearchModeBounds:
53+
params[@"bounds"] = self.bounds.description;
54+
break;
55+
case YLPSearchModeCoordinate:
56+
params[@"ll"] = self.geoCoordinate.description;
57+
break;
58+
}
59+
60+
if (self.currentLatLong) {
61+
params[@"cll"] = self.currentLatLong.description;
62+
}
63+
if (self.term) {
64+
params[@"term"] = self.term;
65+
}
66+
if (self.limit) {
67+
params[@"limit"] = @(self.limit);
68+
}
69+
if (self.offset) {
70+
params[@"offset"] = @(self.offset);
71+
}
72+
if (self.sort) {
73+
params[@"sort"] = @(self.sort);
74+
}
75+
if (self.categoryFilter.count > 0) {
76+
params[@"category_filter"] = [self.categoryFilter componentsJoinedByString:@","];
77+
}
78+
if (self.radiusFilter > 0) {
79+
params[@"radius_filter"] = @(self.radiusFilter);
80+
}
81+
if (self.dealsFilter) {
82+
params[@"deals_filter"] = @(YES);
83+
}
84+
85+
return params;
86+
}
87+
88+
@end

Classes/Request/YLPQueryPrivate.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// YLPQueryPrivate.h
3+
// YelpAPI
4+
//
5+
// Created by Steven Sheldon on 6/26/16.
6+
//
7+
//
8+
9+
#import "YLPQuery.h"
10+
11+
typedef NS_ENUM(NSUInteger, YLPSearchMode) {
12+
YLPSearchModeLocation,
13+
YLPSearchModeBounds,
14+
YLPSearchModeCoordinate,
15+
};
16+
17+
NS_ASSUME_NONNULL_BEGIN
18+
19+
@interface YLPQuery ()
20+
21+
@property (assign, nonatomic) YLPSearchMode mode;
22+
@property (copy, nonatomic, nullable) NSString *location;
23+
@property (strong, nonatomic, nullable) YLPCoordinate *currentLatLong;
24+
@property (strong, nonatomic, nullable) YLPGeoBoundingBox *bounds;
25+
@property (strong, nonatomic, nullable) YLPGeoCoordinate *geoCoordinate;
26+
27+
- (NSDictionary *)parameters;
28+
29+
@end
30+
31+
NS_ASSUME_NONNULL_END

Classes/YelpAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#import "YLPGeoBoundingBox.h"
1717
#import "YLPGeoCoordinate.h"
18+
#import "YLPQuery.h"
1819
#import "YLPSortType.h"
1920

2021
#import "YLPBusiness.h"

0 commit comments

Comments
 (0)