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
/
BGLastFmWebServiceResponse.m
81 lines (67 loc) · 1.9 KB
/
BGLastFmWebServiceResponse.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
//
// BGLastFmWebServiceResponse.m
// ApiHubTester
//
// Created by Ben Gummer on 18/07/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGLastFmWebServiceResponse.h"
@implementation BGLastFmWebServiceResponse
@synthesize responseDocument;
@synthesize wasOK;
@synthesize lastFmCode;
-(id)initWithData:(NSData *)receivedData {
self = [super init];
if (self != nil) {
self.wasOK = NO;
self.lastFmCode = WS_RESP_GENERICFAILURE;
if (receivedData) {
self.lastFmCode = WS_RESP_NOFAILURE;
self.responseDocument = [[NSXMLDocument alloc] initWithData:receivedData options:NSXMLDocumentTidyXML error:nil];
[self determineStatus];
} else NSLog(@"Init with 0-length data");
}
return self;
}
-(void)dealloc {
self.responseDocument = nil;
[super dealloc];
}
-(NSString *)description {
return [NSString stringWithFormat:@"Call Worked: %d; Error code: %d",self.wasOK,self.lastFmCode];
}
-(void)determineStatus {
if (self.responseDocument != nil) {
self.wasOK = [[self stringValueForXPath:@"/lfm/@status"] isEqualToString:@"ok"];
NSLog(@"Status: %@",(self.wasOK ? @"OK" : @"FAILED"));
if (!self.wasOK) {
[self determineErrorCode];
}
}
}
-(void)determineErrorCode {
if (self.responseDocument != nil) {
self.lastFmCode = [[self stringValueForXPath:@"/lfm/error/@code"] intValue];
if (self.lastFmCode>0) NSLog(@"Error Code: %d",self.lastFmCode);
}
}
-(int)lastFmCode {
if (!self.wasOK) {
return lastFmCode;
}
return WS_RESP_NOFAILURE;
}
-(NSXMLNode *)nodeForXPath:(NSString *)xPath {
NSXMLNode *tempNode = [[self.responseDocument nodesForXPath:xPath error:nil] lastObject];
if (tempNode) return tempNode;
return nil;
}
-(NSString *)stringValueForXPath:(NSString *)xPath {
NSXMLNode *tempNode = [self nodeForXPath:xPath];
if (tempNode) return [tempNode stringValue];
return nil;
}
-(BOOL)failedDueToInvalidKey {
return self.lastFmCode == WS_RESP_INVALIDSESSION;
}
@end