forked from bps/scrobblepod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BGLastFmWebServiceParameterList.m
82 lines (63 loc) · 2.15 KB
/
BGLastFmWebServiceParameterList.m
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
//
// BGLastFmWebServiceParameterList.m
// ApiHubTester
//
// Created by Ben Gummer on 18/07/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFmWebServiceParameterList.h"
#import "HubStrings.h"
#import "CocoaCryptoHashing.h"
@implementation BGLastFmWebServiceParameterList
-(id)initWithMethod:(NSString *)aMethod andSessionKey:(NSString *)aSk {
self = [super init];
if (self != nil) {
parameters = [[NSMutableDictionary dictionary] retain];
[self setParameter:API_KEY forKey:@"api_key"];
[self setParameter:aMethod forKey:@"method"];
if (aSk!=nil) [self setParameter:aSk forKey:@"sk"];
}
return self;
}
-(void)dealloc {
[parameters release];
[super dealloc];
}
-(NSString *)description {
return [NSString stringWithFormat:@"Parameters for %@:\n%@",[self parameterForKey:@"method"],parameters];
}
-(void)setParameter:(NSString *)theParameter forKey:(NSString *)theKey {
[parameters setValue:theParameter forKey:theKey];
}
-(NSString *)parameterForKey:(NSString *)theKey {
return [parameters objectForKey:theKey];
}
-(NSString *)methodSignature {
[self resetSessionKeyValue];
NSMutableString *stringSoup = [[NSMutableString alloc] init];
NSArray *sortedParameterKeys = [[parameters allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key;
for (key in sortedParameterKeys) {
[stringSoup appendString:key];
[stringSoup appendString:[parameters valueForKey:key]];
}
[stringSoup appendString:API_SECRET];
NSString *methodSignatureHash = [stringSoup md5HexHash];
[stringSoup release];
return methodSignatureHash;
}
-(NSString *)concatenatedParametersString {
[self resetSessionKeyValue];
NSMutableArray *parameterPairs = [[NSMutableArray alloc] initWithCapacity:parameters.count];
NSString *key;
for (key in parameters) {
[parameterPairs addObject:[NSString stringWithFormat:@"%@=%@",key,[parameters valueForKey:key]]];
}
NSString *finalJoinedString = [parameterPairs componentsJoinedByString:@"&"];
[parameterPairs release];
return finalJoinedString;
}
-(void)resetSessionKeyValue {
[self setParameter:[[NSUserDefaults standardUserDefaults] stringForKey:BGWebServiceSessionKey] forKey:@"sk"];
}
@end