-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwittRbDelegate.rb
executable file
·97 lines (81 loc) · 3.08 KB
/
TwittRbDelegate.rb
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
require 'base64'
require 'cgi'
class TwittRbDelegate
attr_accessor :credentials_window, :main_window
attr_accessor :username_field, :password_field
attr_accessor :username, :password, :updates
attr_accessor :table_view, :status_label
attr_accessor :reload_button
attr_accessor :tweet_field, :send_button
def applicationDidFinishLaunching(notification)
NSApp.beginSheet(credentials_window,
modalForWindow:main_window,
modalDelegate:nil,
didEndSelector:nil,
contextInfo:nil)
end
def initialize
@updates = []
end
def submitCredentials(sender)
self.username = username_field.stringValue
self.password = password_field.stringValue
NSApp.endSheet(credentials_window)
credentials_window.orderOut(sender)
NSLog "I have #{username} as a username"
NSLog "I have a password length of #{password.length}"
retrieveTweets(sender)
end
def retrieveTweets(sender)
url = NSURL.URLWithString("http://twitter.com/statuses/friends_timeline.xml")
request = NSMutableURLRequest.requestWithURL(url)
auth_token = Base64.encode64("#{username}:#{password}").strip
request.setValue("Basic #{auth_token}",
forHTTPHeaderField:"Authorization")
delegate = TRConnectionDelegate.new(self) do |response|
if Array === response
self.updates = response
self.table_view.reloadData
self.reload_button.enabled = true
self.status_label.stringValue = "Tweets updated"
else
self.send_button.enabled = true
self.status_label.stringValue = response
end
end
NSURLConnection.connectionWithRequest(request, delegate:delegate)
self.reload_button.enabled = false;
self.status_label.stringValue = "Loading..."
end
def hideCredentials(sender)
NSLog "Cancelled twitter credentials"
NSApp.endSheet(credentials_window)
credentials_window.orderOut(sender)
end
def postTweet(sender)
url = NSURL.URLWithString("http://twitter.com/statuses/update.xml")
request = NSMutableURLRequest.requestWithURL(url)
request.HTTPMethod = "POST"
auth_token = Base64.encode64("#{username}:#{password}").strip
request.setValue("Basic #{auth_token}",
forHTTPHeaderField:"Authorization")
body = "status=#{CGI.escape(self.tweet_field.stringValue)}"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
delegate = TRConnectionDelegate.new(self) do |response|
if Array === response
NSLog "Got an update: #{response.inspect}"
self.updates = response.concat(updates)
self.table_view.reloadData
self.send_button.enabled = true
self.tweet_field.stringValue = ""
self.status_label.stringValue = "Status Updated"
else
self.send_button.enabled = true
self.status_label.stringValue = "Invalid response"
end
end
NSURLConnection.connectionWithRequest(request, delegate:delegate)
self.send_button.enabled = false;
self.status_label.stringValue = "Updating Status..."
end
end