-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRConnectionDelegate.rb
More file actions
executable file
·50 lines (44 loc) · 1.55 KB
/
TRConnectionDelegate.rb
File metadata and controls
executable file
·50 lines (44 loc) · 1.55 KB
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
class TRConnectionDelegate
def initialize(parent, &block)
@parent = parent
@block = block
end
def connectionDidFinishLoading(connection)
doc = NSXMLDocument.alloc.initWithData(@receivedData,
options:NSXMLDocumentValidate,
error:nil)
if doc
statuses = doc.nodesForXPath("*/status|status", error:nil)
if statuses and !statuses.empty?
tweets = statuses.map do |s|
{
:user => s.nodesForXPath('user/name', error:nil).first.stringValue,
:tweet => s.nodesForXPath('text', error:nil).first.stringValue,
:profile_image_url => s.nodesForXPath('user/profile_image_url', error:nil).first.stringValue,
:url => s.nodesForXPath('user/url', error:nil).first.stringValue,
:created_at => s.nodesForXPath('created_at', error:nil).first.stringValue,
}
end
@block.call(tweets)
end
else
@block.call("Invalid response")
end
end
def connection(connection, didReceiveResponse:response)
case response.statusCode
when 401
@block.call("Invalid username and password")
when (400..500)
@block.call("Unable to complete your request")
end
end
def connection(connection, didReceiveData:data)
@receivedData ||= NSMutableData.new
@receivedData.appendData(data)
end
def connection(conn, didFailWithError:error)
@parent.send_button.enabled = true
@parent.status_label.stringValue = "Error sending update"
end
end