lib/pubnub.rb in pubnub-0.1.11 vs lib/pubnub.rb in pubnub-0.1.12

- old
+ new

@@ -13,19 +13,17 @@ require 'openssl' require 'base64' require 'open-uri' require 'uri' require 'net/http' +require 'net/https' require 'json' require 'pp' require 'rubygems' require 'securerandom' require 'digest' require 'pubnub_crypto' -require 'eventmachine' -require 'em-http' -require 'fiber' class Pubnub MAX_RETRIES = 3 retries=0 #** @@ -53,58 +51,10 @@ @origin = 'http://' + @origin end end #** - #* Pubnub 3.0 Compatibility - #* - #* Init the Pubnub Client API - #* - #* @param string publish_key required key to send messages. - #* @param string subscribe_key required key to receive messages. - #* @param string secret_key required key to sign messages. - #* @param boolean ssl required for 2048 bit encrypted messages. - #* - def initialize( publish_key, subscribe_key, secret_key, ssl_on ) - @publish_key = publish_key - @subscribe_key = subscribe_key - @secret_key = secret_key - @cipher_key = '' - @ssl = ssl_on - @origin = 'pubsub.pubnub.com' - - if @ssl - @origin = 'https://' + @origin - else - @origin = 'http://' + @origin - end - end - - #** - #* Pubnub 2.0 Compatibility - #* - #* Init the Pubnub Client API - #* - #* @param string publish_key required key to send messages. - #* @param string subscribe_key required key to receive messages. - #* - def initialize( publish_key, subscribe_key ) - @publish_key = publish_key - @subscribe_key = subscribe_key - @secret_key = '' - @cipher_key = '' - @ssl = false - @origin = 'pubsub.pubnub.com' - - if @ssl - @origin = 'https://' + @origin - else - @origin = 'http://' + @origin - end - end - - #** #* Publish #* #* Send a message to a channel. #* #* @param array args with channel and message. @@ -176,16 +126,16 @@ puts "Missing Callback." return false end ## EventMachine loop - EventMachine.run do + #EventMachine.run do timetoken = 0 request = [ 'subscribe', @subscribe_key, channel, '0', timetoken.to_s ] args['request'] = request _subscribe(args) - end + #end end #** #* History #* @@ -256,13 +206,12 @@ # Construct Request url = encode_URL(request); url = @origin + url # Execute Request - http = EventMachine::HttpRequest.new(url, :connect_timeout => 310, :inactivity_timeout => 0).get - http.callback { - http_response = JSON.parse(http.response) + open(url) do |f| + http_response = JSON.parse(f.read) messages = http_response[0] timetoken = http_response[1] next if !messages.length @@ -290,34 +239,27 @@ request = [ 'subscribe', @subscribe_key, channel, '0', timetoken.to_s ] args['request'] = request # Recusive call to _subscribe _subscribe(args) - } - http.errback { - http.error - # TODO: check for time function and reconnect - } + end end #** #* Request URL #* #* @param array request of url directories. #* @return array from JSON response. #* def _request(args) - request = args['request'] + request = args['request'] callback = args['callback'] url = encode_URL(request) url = @origin + url - EventMachine.run { - # Execute Request - http = EventMachine::HttpRequest.new(url, :connect_timeout => 310, :inactivity_timeout => 0).get - http.callback { + open(url) do |f| + response = JSON.parse(f.read) if request[0] == 'history' - response = JSON.parse(http.response) if @cipher_key.length > 0 myarr=Array.new() response.each do |message| pc=PubnubCrypto.new(@cipher_key) if message.is_a? Array @@ -327,23 +269,17 @@ end myarr.push(message) end callback.call(myarr) else - callback.call(reponse) + callback.call(response) end elsif request[0] == 'publish' - callback.call(JSON.parse(http.response)) + callback.call(response) else - callback.call(JSON.parse(http.response)) + callback.call(response) end - EventMachine.stop - } - http.errback { - puts http.error - EventMachine.stop - } - } + end end def encode_URL(request) ## Construct Request URL url = '/' + request.map{ |bit| bit.split('').map{ |ch|