Skip to content

Commit

Permalink
use Thread instead of Fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
seki committed Feb 10, 2014
1 parent 5e4ac88 commit 752ebb7
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions sample/drip_tw.rb
@@ -1,3 +1,4 @@
# -*- encoding: utf-8 -*-
require 'simple-oauth'
require 'drb'
require 'pp'
Expand Down Expand Up @@ -36,6 +37,40 @@ def push(event)
end
end

class DripThread
def initialize(app)
@app = app
@queue = Queue.new
Thread.new do
story
end
end

def story
event = @queue.pop
pending = []
while event['id_str'].nil?
pending << event
event = @queue.pop
end

@app.fill_timeline(event['id_str'])

while event = pending.shift
@app.write(event)
end

while true
event = @queue.pop
@app.write(event)
end
end

def push(event)
@queue.push(event)
end
end

class JSONStream
def initialize(drip)
@buf = ''
Expand All @@ -49,8 +84,10 @@ def push(str)
@buf.sub!(line,"")
line.strip!
event = JSON.parse(line)
rescue
break
rescue JSON::ParserError
pp $line if $DEBUG
pp $! if $DEBUG
next
end
pp event if $DEBUG
@drip.push(event)
Expand Down Expand Up @@ -138,7 +175,7 @@ def set_pin(pin)
end

def drip_stream
json = JSONStream.new(DripFiber.new(self))
json = JSONStream.new(DripThread.new(self))
oauth.request(:GET, 'https://userstream.twitter.com/2/user.json') do |r|
r.read_body do |chunk|
json.push(chunk)
Expand All @@ -147,11 +184,18 @@ def drip_stream
end

def home_timeline(since_id, max_id)
url = "http://api.twitter.com/1.1/statuses/home_timeline.json?count=200&include_entities=true"
url = "https://api.twitter.com/1.1/statuses/home_timeline.json?count=200&include_entities=true"
url += "&since_id=#{since_id}" if since_id
url += "&max_id=#{max_id}" if max_id
r = oauth.request(:GET, url)
JSON.parse(r.body)
body = r.body
begin
JSON.parse(body)
rescue
pp body
pp url
exit
end
end

def user_timeline(since_id, max_id)
Expand Down

0 comments on commit 752ebb7

Please sign in to comment.