Created
September 14, 2009 00:24
-
-
Save sorah/186390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
=begin | |
lingr.rb | |
writed by Sora Harakami <sora134@gmail.com> | |
http://soralabo.net/ | |
http://codnote.net/ | |
=end | |
require "net/http" | |
require "simple-json.rb" | |
require "thread" | |
require "timeout" | |
class Lingr | |
def initialize(params = {}) | |
@debug = false | |
@cookie_key = "__lingr" | |
@params = params | |
@session = Lingr::Session.new(self) | |
@http = Net::HTTP.new("lingr.com",8080) | |
@http2 = Net::HTTP.new("lingr.com") | |
@c_error = 0 | |
@session_id = nil | |
@nickname = nil | |
@public_session_id = nil | |
@counter = nil | |
@event = Lingr::Event.new(self) | |
@room = Lingr::Room.new(self) | |
end | |
def request(path, params = {}, options = {}) | |
Thread.new do | |
begin | |
options["success"] ||= lambda{|json,lingr|} | |
options["failure"] ||= lambda{|json,lingr|} | |
options["complete"] ||= lambda{|json,lingr|} | |
options["observe"] ||= false | |
timeout(60) do | |
params["callback"] = "foo" | |
req = Net::HTTP::Get.new(path+"?"+params.map{|k,v| "#{k}=#{v}" }.join("&")) | |
unless @session_id.nil? | |
req['Cookie'] = @cookie_key+"="+@session_id | |
end | |
if options["observe"] | |
n_http = @http | |
else | |
n_http = @http2 | |
end | |
n_http.request(req) do |h| | |
raw_json = h.body.gsub(/foo\(/,"").gsub(/\);?/,"") | |
json = WebAPI::JsonParser.new.parse(raw_json) | |
if json["status"] == 'ok' | |
options["success"].call(json,self) | |
else | |
options["failure"].call(json,self) | |
end | |
options["complete"].call(json,self) | |
end | |
end | |
rescue Timeout::Error | |
@c_error += 1 | |
options["failure"].call(nil,self) | |
end | |
end | |
end | |
class Event | |
def initialize(lingr) | |
@lingr = lingr | |
@event_ary = [] | |
end | |
def add(name,data = nil) | |
@event_ary << [name,data] | |
end | |
def recieve() | |
@event_ary.shift | |
end | |
end | |
class Room | |
def initialize(lingr) | |
@lingr = lingr | |
end | |
def show(room_id) | |
@lingr.request('/room/show',{ | |
"session" => @lingr.session_id, | |
"room" => room_id | |
},{ | |
"success" => lambda{|json,lingr| | |
lingr.events.add('room_show_complete',json) | |
}, | |
"failure" => lambda{|json,lingr| | |
lingr.events.add('api_failure',json) | |
} | |
}) | |
end | |
def subscribe(room_id) | |
@lingr.request('/room/subscribe',{ | |
"session" => @lingr.session_id, | |
"room" => room_id | |
},{ | |
"success" => lambda{|json,lingr| | |
@lingr.counter = json["counter"] | |
@lingr.events.add('subscribe_complete',json) | |
}, | |
"failure" => lambda{|json,lingr| | |
@lingr.events.add('api_failure',json) | |
} | |
}) | |
end | |
def unsubscribe(room_id,n=nil) | |
params = { | |
"session" => @lingr.session_id, | |
"room" => room_id | |
} | |
params["not"] = n unless n.nil? | |
@lingr.request('/room/unsubscribe', params, { | |
"success" => lambda{|json.lingr| | |
@lingr.counter = json["counter"] | |
@lingr.events.add('unsubscribe_complete',json) | |
}, | |
"failure" => lambda{|json,lingr| | |
@lingr.events.add('api_failure',json) | |
} | |
}) | |
end | |
def say(room_id,text,nickname=nil,local_echo_count) | |
nickname = @lingr.nickname if nickname.nil? | |
@lingr.request('/room/say',{ | |
"session" => @lingr.session_id, | |
"room" => room_id, | |
"nickname" => nickname, | |
"text" => text, | |
"local_id" => local_echo_count | |
}, { | |
"success" => lambda{|json,lingr| | |
@lingr.events.add('say_complete',json) | |
} | |
"failure" => lambda{|json,lingr| | |
@lingr.events.add('api_failure',json) | |
} | |
}) | |
end | |
def observe | |
@lingr.request('/event/observe', { | |
"session" => @lingr.session_id, | |
"counter" => @lingr.counter | |
}, { | |
"observe" => true, | |
"success" => lambda{|json,lingr| | |
@lingr.counter = [json["counter"],@lingr.counter].max unless json["counter"].nil? | |
@lingr.events.add('observe_complete', json) | |
@lingr.c_error = 0 | |
Thread.new{ @lingr.observe } | |
}, | |
"failure" => lambda{|json,lingr| | |
@lingr.events.add('observe_failure', json) | |
@lingr.c_error += 1 | |
Thread.new{ sleep 2**@lingr.c_error; @lingr.observe } | |
} | |
}) | |
end | |
class Session | |
def initialize(lingr) | |
@lingr = lingr | |
@presence = nil | |
@username = nil | |
if @lingr.params[:user].nil? || @lingr.params[:password].nil? | |
raise ArgumentError,"params required user/pass" | |
end | |
end | |
def start | |
if @lingr.session_id.nil? | |
params = {} | |
params["user"] = @lingr.params[:user] | |
params["password"] = @lingr.params[:password] | |
@lingr.request('/session/create', params, { | |
"success" => lambda{|json,lingr| | |
lingr.session_id = json["session"] | |
lingr.public_session_id = json["public_id"] | |
lingr.nickname = json["nickname"] | |
lingr.session.presence = json["presence"] | |
lingr.session.username = json["user"]["username"] unless json["user"].nil? | |
lingr.events.add('session_created',json) | |
} | |
}) | |
else | |
@lingr.request('/session/verify', {"session"=>@lingr.session_id}, { | |
"success" => lambda{|json,lingr| | |
lingr.public_session_id = json["public_id"] | |
lingr.nickname = json["nickname"] | |
lingr.session.presence = json["presence"] | |
lingr.session.username = json["user"]["username"] unless json["user"].nil? | |
lingr.events.add('session_verified',json) | |
}, | |
"failture" => lambda{|json,lingr| | |
lingr.session_id = nil | |
lingr.public_session_id = nil | |
lingr.nickname = nil | |
lingr.session.presence = nil | |
lingr.session.username = nil | |
lingr.session.start | |
} | |
}) | |
end | |
end | |
def end | |
unless @lingr.session_id.nil? | |
@lingr.request('/session/destroy', {"session"=>@lingr.session_id},{ | |
"success" => lambda{|json,lingr| | |
lingr.session_id = nil | |
lingr.public_session_id = nil | |
lingr.session.username = nil | |
lingr.session.presence = nil | |
lingr.nickname = nil | |
lingr.events.add('session_destroyed',json) | |
}, | |
"failure" => lambda{|json,lingr| | |
lingr.session_id = nil | |
lingr.public_session_id = nil | |
lingr.session.username = nil | |
lingr.session.presence = nil | |
lingr.nickname = nil | |
lingr.events.add('api_failure',json); | |
} | |
}) | |
end | |
end | |
def set_presence(presence,nickname=nil) | |
nickname = @lingr.nickname if nickname.nil? | |
@lingr.request('/session/set_presence', { | |
"session" => @lingr.session_id, | |
"nickname" => nickname, | |
"presence" => presence | |
},{ | |
"success" => lambda{|json,lingr| | |
if json["presence"] == 'online' | |
lingr.nickname = json["nickname"] | |
lingr.session.presence = 'online' | |
elsif json["presence"] == 'offline' | |
lingr.nickname = nil | |
lingr.session.presence = 'offline' | |
end | |
lingr.events.add('set_presence_complete',json) | |
}, | |
"failure" => lambda{|json,lingr| lingr.events.add('api_failure',json) } | |
}) | |
end | |
attr_accessor :presence,:username | |
end | |
attr_reader :params,:session,:room | |
attr_accessor :session_id,:debug,:nickname,:public_session_id,:events,:counter | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment