Skip to content

Commit 76f2724

Browse files
author
Joey Lorich
committed
Convert apikit from submodule
1 parent e07ad8f commit 76f2724

5 files changed

Lines changed: 391 additions & 1 deletion

File tree

EasyReader/Library/apikit

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// AKRoute.h
3+
// APIKit Router
4+
//
5+
// Created by Joseph Lorich on 3/19/14.
6+
// Copyright (c) 2014 Joseph Lorich.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
26+
#import <Foundation/Foundation.h>
27+
28+
typedef enum {
29+
kAKRequestMethodGET,
30+
kAKRequestMethodPOST,
31+
kAKRequestMethodPUT,
32+
kAKRequestMethodDELETE
33+
} AKRequestMethod;
34+
35+
36+
/**
37+
* An API Route
38+
*/
39+
@interface AKRoute : NSObject
40+
41+
42+
#pragma mark - Properties
43+
44+
/// The string representation for the url for this route
45+
@property (nonatomic, readonly) NSString *path;
46+
47+
/// The request method for this route
48+
@property (nonatomic, readonly) AKRequestMethod requestMethod;
49+
50+
51+
#pragma mark - Initializers
52+
53+
/**
54+
* Initializes a new CSApiRoute
55+
*
56+
* @param path the path for this URL
57+
* @param requestMethod the request method for this route
58+
*/
59+
- (id)initWithPath:(NSString *)path requestMethod:(AKRequestMethod)requestMethod;
60+
61+
62+
/**
63+
* Builds a URL String for a given set of params
64+
*
65+
* @param params The URL Parameters
66+
*/
67+
- (NSString *)pathStringForParams:(NSDictionary *)params;
68+
69+
@end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// AKRoute.m
3+
// APIKit Router
4+
//
5+
// Created by Joseph Lorich on 3/19/14.
6+
// Copyright (c) 2014 Joseph Lorich.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
26+
#import "AKRoute.h"
27+
28+
@implementation AKRoute
29+
{
30+
NSString *_path;
31+
AKRequestMethod _requestMethod;
32+
}
33+
34+
- (id)initWithPath:(NSString *)path requestMethod:(AKRequestMethod)requestMethod
35+
{
36+
self = [super init];
37+
38+
if (self)
39+
{
40+
_path = path;
41+
_requestMethod = requestMethod;
42+
}
43+
44+
return self;
45+
}
46+
47+
- (NSString *)pathStringForParams:(NSDictionary *)params
48+
{
49+
return [self replaceTokensInPath:_path params:params];
50+
}
51+
52+
/**
53+
* Replaces all slug tokens (e.g. ":first_name") in a path with their appropriate param
54+
*
55+
* @param urlString the base path to parse
56+
* @param params the parameters for the URL
57+
*/
58+
- (NSString *)replaceTokensInPath:(NSString*)urlString params:(NSDictionary *)params
59+
{
60+
NSMutableString *parsedString = [urlString mutableCopy];
61+
62+
for (NSString *param in [params allKeys])
63+
{
64+
id value = [params valueForKey:param];
65+
66+
NSString *paramType = NSStringFromClass([value class]);
67+
68+
if ([paramType isEqualToString:@"NSString"])
69+
{
70+
NSString *token = [NSString stringWithFormat:@":%@", param];
71+
[parsedString stringByReplacingOccurrencesOfString:token withString:value];
72+
}
73+
}
74+
75+
return parsedString;
76+
}
77+
78+
79+
@end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// AKRouter.h
3+
// APIKit Router
4+
//
5+
// Created by Joseph Lorich on 3/19/14.
6+
// Copyright (c) 2014 Joseph Lorich.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
26+
#import <Foundation/Foundation.h>
27+
#import "AKRoute.h"
28+
29+
/**
30+
* An interface to create and process named routes for URLs
31+
*/
32+
@interface AKRouter : NSObject
33+
34+
35+
/**
36+
* Returns a shared singleton router instance
37+
*/
38+
+ (AKRouter *) shared;
39+
40+
/**
41+
* Registers a new route with the router
42+
*
43+
* @param routeName The route name string
44+
* @param path The path for the route
45+
* @param requestMethod the Request Method
46+
*/
47+
- (void)registerRoute:(NSString *)routeName path:(NSString *)path requestMethod:(AKRequestMethod)requestMethod;
48+
49+
50+
/**
51+
* Builds an NSURL Based on the route name and parameters
52+
*
53+
* @param routeName the API Route Name
54+
* @param params paramaters to be used in building the API route
55+
*/
56+
- (NSURL *)urlFor:(NSString *)routeName params:(NSDictionary *)params;
57+
58+
/**
59+
* Builds an NSString path based on the route name and parameters
60+
*
61+
* @param routeName the API Route Name
62+
* @param params paramaters to be used in building the API route
63+
*/
64+
- (NSString *)pathFor:(NSString *)routeName params:(NSDictionary *)params;
65+
66+
/**
67+
* Returns the request method for the given route
68+
*
69+
* @param routeName the API Route Name
70+
*/
71+
- (AKRequestMethod)methodFor:(NSString *)routeName;
72+
73+
/**
74+
* Gets the route object for the given route name
75+
*
76+
* @param routeName The routes name
77+
*/
78+
- (AKRoute *)routeForName:(NSString*)routeName;
79+
80+
@end
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//
2+
// AKRouter.m
3+
// APIKit Router
4+
//
5+
// Created by Joseph Lorich on 3/19/14.
6+
// Copyright (c) 2014 Joseph Lorich.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
26+
#import "AKRouter.h"
27+
28+
static dispatch_once_t pred;
29+
static AKRouter *shared = nil;
30+
31+
@implementation AKRouter
32+
{
33+
/// Internal route storage dictionary
34+
NSMutableDictionary *_routes;
35+
36+
/// The base API URL
37+
NSURL *_baseURL;
38+
39+
/// The base API Path
40+
NSString *_basePath;
41+
}
42+
43+
+ (AKRouter *)shared
44+
{
45+
dispatch_once(&pred, ^{
46+
// Ensure we don't overwrite a manually set client
47+
if (shared != nil) return;
48+
49+
// Load API configuration data from the info plist
50+
NSBundle *bundle = [NSBundle mainBundle];
51+
52+
NSString *apiHost = [bundle objectForInfoDictionaryKey:@"ApiHost"];
53+
NSString *basePath = [bundle objectForInfoDictionaryKey:@"ApiBasePath"];
54+
55+
shared = [[AKRouter alloc] initWithAPIHost:apiHost basePath:basePath];
56+
});
57+
58+
return shared;
59+
}
60+
61+
- (id)initWithAPIHost:(NSString *)apiHost basePath:(NSString*)basePath
62+
{
63+
self = [super init];
64+
65+
if (self)
66+
{
67+
_routes = [[NSMutableDictionary alloc] init];
68+
_baseURL = [NSURL URLWithString:apiHost];
69+
_basePath = basePath;
70+
}
71+
72+
return self;
73+
}
74+
75+
- (void)registerRoute:(NSString *)routeName path:(NSString *)path requestMethod:(AKRequestMethod)requestMethod
76+
{
77+
if ([[_routes allKeys] containsObject:routeName])
78+
{
79+
[NSException raise:@"Route already exists" format:@"The route named %@ already is registered", routeName];
80+
return;
81+
}
82+
83+
AKRoute *route = [[AKRoute alloc] initWithPath:path requestMethod:requestMethod];
84+
85+
[_routes setValue:route forKey:routeName];
86+
}
87+
88+
- (NSURL *)urlFor:(NSString *)routeName params:(NSDictionary *)params
89+
{
90+
NSString *path = [self pathFor:routeName params:params];
91+
92+
if (!path)
93+
{
94+
return nil;
95+
}
96+
97+
return [NSURL URLWithString:path relativeToURL:_baseURL];
98+
}
99+
100+
- (NSString *)pathFor:(NSString *)routeName params:(NSDictionary *)params
101+
{
102+
AKRoute *route = [self routeForName:routeName];
103+
NSString *pathString = [route pathStringForParams:params];
104+
105+
return [NSString stringWithFormat:@"%@/%@",
106+
[self stripTrainingSlashes:_basePath],
107+
[self stripLeadingSlashes:pathString]];
108+
}
109+
110+
- (AKRequestMethod)methodFor:(NSString *)routeName
111+
{
112+
AKRoute *route = [self routeForName:routeName];
113+
114+
return [route requestMethod];
115+
}
116+
117+
- (AKRoute *)routeForName:(NSString *)routeName
118+
{
119+
return [_routes valueForKey:routeName];
120+
}
121+
122+
123+
#pragma mark - Private Methods
124+
125+
/**
126+
* Strips any trailing '/' from the end of a string
127+
*
128+
* This is used to help parse out paths
129+
*/
130+
- (NSString *)stripTrainingSlashes:(NSString *)string
131+
{
132+
NSInteger stringLength = [string length];
133+
134+
if ([string characterAtIndex:stringLength - 1] == '/')
135+
{
136+
return [self stripTrainingSlashes:[string substringWithRange:NSMakeRange(0, stringLength - 1)]];
137+
}
138+
else
139+
{
140+
return string;
141+
}
142+
}
143+
144+
/**
145+
* Strips any leading '/' from the beginning of a string
146+
*
147+
* This is used to help parse out paths
148+
*/
149+
- (NSString *)stripLeadingSlashes:(NSString *)string
150+
{
151+
if ([string characterAtIndex:0] == '/')
152+
{
153+
NSInteger stringLength = [string length];
154+
return [self stripLeadingSlashes:[string substringWithRange:NSMakeRange(1, stringLength-2)]];
155+
}
156+
else
157+
{
158+
return string;
159+
}
160+
}
161+
162+
163+
@end

0 commit comments

Comments
 (0)