Skip to content

Commit

Permalink
Merge pull request #85 from rickharrison/search-sort
Browse files Browse the repository at this point in the history
Adds sorting methods for search queries.
  • Loading branch information
rickharrison committed Jan 29, 2016
2 parents ac7f624 + 1517cd0 commit 0852104
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
17 changes: 15 additions & 2 deletions Classes/Model/RKPagination.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ typedef NS_ENUM(NSUInteger, RKTimeSortingMethod) {
RKTimeSortingMethodAllTime
};

typedef NS_ENUM(NSUInteger, RKSearchSortingMethod) {
RKSearchSortingMethodRelevance = 101,
RKSearchSortingMethodHot,
RKSearchSortingMethodTop,
RKSearchSortingMethodNew,
RKSearchSortingMethodComments
};

extern NSString * RKStringFromCommentSortingMethod(RKCommentSortingMethod sortingMethod);
extern NSString * RKStringFromTimeSortingMethod(RKTimeSortingMethod sortingMethod);
extern NSString * RKStringFromUserContentSortingMethod(RKUserContentSortingMethod sortingMethod);
Expand All @@ -58,7 +66,7 @@ extern NSString * RKStringFromUserContentSortingMethod(RKUserContentSortingMetho

/**
The full name of the thing for which other objects will be returned before.
This property takes precedence over the `after` property.
*/
@property (nonatomic, copy) NSString *before;
Expand All @@ -75,7 +83,7 @@ extern NSString * RKStringFromUserContentSortingMethod(RKUserContentSortingMetho

/**
The sorting method for user content. This affects the order in which user content is returned.
@note Only the RKUserContentSortingMethodTop and RKUserContentSortingMethodControversial sorting methods are affected by the timeMethod property.
*/
@property (nonatomic, assign) RKUserContentSortingMethod userContentSortingMethod;
Expand All @@ -85,6 +93,11 @@ extern NSString * RKStringFromUserContentSortingMethod(RKUserContentSortingMetho
*/
@property (nonatomic, assign) RKTimeSortingMethod timeMethod;

/**
The sorting method for search queries. This affects the order in which search results are returned.
*/
@property (nonatomic, assign) RKSearchSortingMethod searchSortingMethod;

/**
Extracts a pagination object from a listing response.
*/
Expand Down
56 changes: 40 additions & 16 deletions Classes/Model/RKPagination.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,58 @@
}
}

NSString * RKStringFromSearchSortingMethod(RKSearchSortingMethod sortingMethod)
{
switch (sortingMethod)
{
case RKSearchSortingMethodRelevance:
return @"relevance";
case RKSearchSortingMethodHot:
return @"hot";
case RKSearchSortingMethodTop:
return @"top";
case RKSearchSortingMethodNew:
return @"new";
case RKSearchSortingMethodComments:
return @"comments";
default:
return nil;
}
}

@implementation RKPagination

+ (RKPagination *)paginationFromListingResponse:(NSDictionary *)listingResponse
{
RKPagination *pagination = [[RKPagination alloc] init];

id before = [listingResponse valueForKeyPath:@"data.before"];
id after = [listingResponse valueForKeyPath:@"data.after"];

if (before == [NSNull null] && after == [NSNull null])
{
return nil;
}

if (before != [NSNull null])
{
pagination.before = before;
}

if (after != [NSNull null])
{
pagination.after = after;
}

return pagination;
}

+ (RKPagination *)paginationWithLimit:(NSUInteger)limit
{
RKPagination *pagination = [[RKPagination alloc] init];

pagination.limit = limit;

return pagination;
}

Expand All @@ -123,7 +142,7 @@ - (instancetype)init
{
_limit = 25;
}

return self;
}

Expand All @@ -141,37 +160,42 @@ - (NSString *)description
- (NSDictionary *)dictionaryValue
{
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:4];

if (self.limit)
{
[parameters setObject:[NSString stringWithFormat:@"%lu", (unsigned long)self.limit] forKey:@"limit"];
}

if (self.before)
{
[parameters setObject:self.before forKey:@"before"];
}

if (self.after)
{
[parameters setObject:self.after forKey:@"after"];
}

if (self.timeMethod)
{
[parameters setObject:RKStringFromTimeSortingMethod(self.timeMethod) forKey:@"t"];
}

if (self.userContentSortingMethod)
{
[parameters setObject:RKStringFromUserContentSortingMethod(self.userContentSortingMethod) forKey:@"sort"];
}


if(self.searchSortingMethod)
{
[parameters setObject:RKStringFromSearchSortingMethod(self.searchSortingMethod) forKey:@"sort"];
}

if ([parameters count] == 0)
{
return nil;
}

return [parameters copy];
}

Expand All @@ -192,7 +216,7 @@ - (id)initWithCoder:(NSCoder *)decoder
_userContentSortingMethod = [[decoder decodeObjectOfClass:[NSNumber class] forKey:@"userContentSortingMethod"] unsignedIntegerValue];
_timeMethod = [[decoder decodeObjectOfClass:[NSNumber class] forKey:@"timeMethod"] unsignedIntegerValue];
}

return self;
}

Expand Down

0 comments on commit 0852104

Please sign in to comment.