aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMURLBuilder.m
diff options
context:
space:
mode:
authorGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2012-03-01 17:00:20 +0000
committerGravatar gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2012-03-01 17:00:20 +0000
commit32654998f055c85d9a70b6f415abf79dd81c601e (patch)
tree6d5ecf98880ccbd2c0ad55036f730eee4e6fa871 /Foundation/GTMURLBuilder.m
parent4f85e00accda833f7fd0ef1a40018818ae090495 (diff)
[Author: msenesi]
Flexible builder for URLs. R=altse,dmaclach,thomasvl APPROVED=dmaclach
Diffstat (limited to 'Foundation/GTMURLBuilder.m')
-rw-r--r--Foundation/GTMURLBuilder.m138
1 files changed, 138 insertions, 0 deletions
diff --git a/Foundation/GTMURLBuilder.m b/Foundation/GTMURLBuilder.m
new file mode 100644
index 0000000..0cfce45
--- /dev/null
+++ b/Foundation/GTMURLBuilder.m
@@ -0,0 +1,138 @@
+//
+// GTMURLBuilder.m
+//
+// Copyright 2012 Google Inc.
+//
+// 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 "GTMURLBuilder.h"
+
+#import "GTMDefines.h"
+#import "GTMLogger.h"
+#import "GTMNSDictionary+URLArguments.h"
+#import "GTMNSString+URLArguments.h"
+
+@implementation GTMURLBuilder
+
+@synthesize baseURLString = baseURLString_;
+
++ (GTMURLBuilder *)builderWithString:(NSString *)URLString {
+ GTMURLBuilder *URLBuilder =
+ [[[GTMURLBuilder alloc] initWithString:URLString] autorelease];
+ return URLBuilder;
+}
+
++ (GTMURLBuilder *)builderWithURL:(NSURL *)URL {
+ return [self builderWithString:[URL absoluteString]];
+}
+
+- (id)init {
+ self = [super init];
+ [self release];
+ _GTMDevAssert(NO, @"Invalid initialization.");
+ return nil;
+}
+
+- (id)initWithString:(NSString *)URLString {
+ self = [super init];
+ if (self) {
+ _GTMDevAssert(URLString, @"URL must not be nil");
+ NSURL *URL = [NSURL URLWithString:URLString];
+ _GTMDevAssert(URL, @"URL is invalid");
+
+ // NSURL does not work with ports.
+ baseURLString_ = [URL absoluteString];
+ if ([URL path]) {
+ NSRange pathRange =
+ [baseURLString_ rangeOfString:[URL path] options:NSBackwardsSearch];
+ if (pathRange.location != NSNotFound) {
+ baseURLString_ = [baseURLString_ substringToIndex:pathRange.location];
+ }
+
+ baseURLString_ =
+ [NSString stringWithFormat:@"%@%@", baseURLString_, [URL path]];
+ }
+ [baseURLString_ retain];
+ params_ = [[NSDictionary gtm_dictionaryWithHttpArgumentsString:[URL query]]
+ mutableCopy];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [baseURLString_ release];
+ [params_ release];
+ [super dealloc];
+}
+
+- (void)setValue:(NSString *)value forParameter:(NSString *)parameter {
+ [params_ setObject:value forKey:parameter];
+}
+
+- (void)setIntegerValue:(NSInteger)value forParameter:(NSString *)parameter {
+ [params_ setObject:[NSString stringWithFormat:@"%i", value] forKey:parameter];
+}
+
+- (NSString *)valueForParameter:(NSString *)parameter {
+ return [params_ objectForKey:parameter];
+}
+
+- (void)removeParameter:(NSString *)parameter {
+ [params_ removeObjectForKey:parameter];
+}
+
+- (void)setParameters:(NSDictionary *)parameters {
+ [params_ autorelease];
+ params_ = [[NSDictionary dictionaryWithDictionary:parameters] mutableCopy];
+}
+
+- (NSDictionary *)parameters {
+ return params_;
+}
+
+- (NSURL *)URL {
+ if (![params_ count]) {
+ return [NSURL URLWithString:baseURLString_];
+ } else {
+ return [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
+ baseURLString_, [params_ gtm_httpArgumentsString]]];
+ }
+}
+
+- (NSString *)URLString {
+ return [[self URL] absoluteString];
+}
+
+- (BOOL)isEqual:(GTMURLBuilder *)URLBuilder {
+ if (!URLBuilder) {
+ return NO;
+ }
+
+ if (!([[self baseURLString] isEqualToString:[URLBuilder baseURLString]])) {
+ return NO;
+ }
+
+ if (![[self parameters] isEqualToDictionary:[URLBuilder parameters]]) {
+ return NO;
+ }
+
+ return YES;
+}
+
++ (NSURL *)URLWithString:(NSString *)URLString {
+ return [NSURL URLWithString:
+ [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
+}
+
+@end