This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BGLastFmScrobbleResponse.m
104 lines (85 loc) · 3.22 KB
/
BGLastFmScrobbleResponse.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// BGLastFmScrobbleResponse.m
// LastFmProtocolTester
//
// Created by Ben Gummer on 8/07/07.
// Copyright 2007 Ben Gummer. All rights reserved.
//
#import "BGLastFmScrobbleResponse.h"
#import "BGLastFmDefines.h"
@implementation BGLastFmScrobbleResponse
- (id) init {
self = [super init];
if (self != nil) {
responseComponents = [NSMutableDictionary new];
[responseComponents setValue:[NSNumber numberWithBool:YES] forKey:Scrobble_WasSuccessful];
}
return self;
}
-(id)initWithScrobbleResponseString:(NSString *)aString {
self = [super init];
if (self != nil) {
NSLog(@"%@",aString);
responseLines = [[aString componentsSeparatedByString:@"\n"] retain];
NSString *statusLine = [responseLines objectAtIndex:0];
responseType = 0;
if ([statusLine rangeOfString:@"OK"].length > 0) {
responseType = 1;
} else if ([statusLine rangeOfString:@"BADSESSION"].length > 0) {
responseType = 2;
} else if ([statusLine rangeOfString:@"FAILED"].length > 0) {
responseType = 3;
}
responseComponents = [[self parseResponse] retain];
}
return self;
}
- (void) dealloc {
[responseLines release];
[responseComponents release];
[self setLastScrobbleDate:nil];
[super dealloc];
}
-(NSDictionary *)parseResponse {
NSMutableDictionary *responseDetailsDictionary = [[NSMutableDictionary new] autorelease];
if (responseType==1) {
[responseDetailsDictionary setObject:@"OK" forKey:Scrobble_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:YES] forKey:Scrobble_WasSuccessful];
} else if (responseType==2) {
[responseDetailsDictionary setObject:@"BADSESSION" forKey:Scrobble_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:NO] forKey:Scrobble_WasSuccessful];
[responseDetailsDictionary setObject:@"The supplied session key was invalid" forKey:Scrobble_FailureReason];
} else if (responseType==3) {
[responseDetailsDictionary setObject:@"FAILED" forKey:Scrobble_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:NO] forKey:Scrobble_WasSuccessful];
[responseDetailsDictionary setObject:[[responseLines objectAtIndex:0] substringFromIndex:7] forKey:Scrobble_FailureReason];
} else if (responseType==0) {
[responseDetailsDictionary setObject:@"UNKNOWNRESPONSE" forKey:Scrobble_ResponseTypeString];
[responseDetailsDictionary setObject:[NSNumber numberWithBool:NO] forKey:Scrobble_WasSuccessful];
[responseDetailsDictionary setObject:@"Scrobble response could not be parsed" forKey:Scrobble_FailureReason];
}
return responseDetailsDictionary;
}
-(BOOL)wasSuccessful {
return [[responseComponents objectForKey:Scrobble_WasSuccessful] boolValue];
}
-(void)setWasSuccessful:(BOOL)aBool {
[responseComponents setValue:[NSNumber numberWithBool:aBool] forKey:Scrobble_WasSuccessful];
}
-(int)responseType {
return responseType;
}
-(NSString *)failureReason {
return [responseComponents objectForKey:Scrobble_FailureReason];
}
-(NSString *)description {
return [NSString stringWithFormat:@"%@",responseComponents];
}
-(NSCalendarDate *)lastScrobbleDate {
return lastScrobbleDate;
}
-(void)setLastScrobbleDate:(NSCalendarDate *)aDate {
if (lastScrobbleDate!=nil) [lastScrobbleDate release];
lastScrobbleDate = [aDate retain];
}
@end