-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterService.rb
89 lines (74 loc) · 2.47 KB
/
TwitterService.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
require 'json'
class TwitterService
TWITTER_SEARCH_URL = "http://twitter.com/search.json"
attr_reader :delegate, :refreshURL
def initialize(query="Twitter", options = {delegate: nil, refreshInterval: 30})
@query = query
@cancel = false
@delegate = options[:delegate]
@refreshURL = nil
@timer = NSTimer.scheduledTimerWithTimeInterval(options[:refreshInterval],
target: self,
selector: :refreshSearch,
userInfo: nil,
repeats: true)
end
def cancelled?
@cancel
end
def cancel!
NSLog("cancelling")
cancelConnection
end
def refreshSearch
return if cancelled?
@receivedData = nil
request = NSURLRequest.requestWithURL(searchURL, cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 20.0)
@currentConnection = NSURLConnection.connectionWithRequest(request, delegate: self)
NSLog("Connecting to: #{searchURL.absoluteString}")
end
def searchURL
if refreshURL
NSURL.URLWithString("#{TWITTER_SEARCH_URL}#{refreshURL}")
else
NSURL.URLWithString("#{TWITTER_SEARCH_URL}?q=#{escapeString(@query)}")
end
end
def connectionDidFinishLoading(connection)
unless cancelled?
response = parseJSON(@receivedData)
@refreshURL = response['refresh_url']
self.delegate.newTweetsReceived(createTweets(response))
end
end
def connection(connection, didReceiveResponse: response)
end
def connection(connection, didReceiveData:data)
@receivedData ||= NSMutableData.new
@receivedData.appendData(data)
end
def connection(conn, didFailWithError:error)
NSLog(error.localizedDescription)
end
protected
def createTweets(response)
(response['results'] || []).map do |status|
image = NSImage.alloc.initWithContentsOfURL(NSURL.URLWithString(status['profile_image_url']))
{image: image, tweet: status["text"]}
end
end
def parseJSON(data)
JSON.parse(NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding))
end
def escapeString(string)
string.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
end
def cancelConnection
@cancel = true
@timer.invalidate
unless @currentConnection.nil?
@currentConnection.cancel
@currentConnection = nil
end
end
end