Skip to content
Browse files

added RhoMobile PubNub Ruby Library by David Greene

  • Loading branch information...
1 parent 45ef414 commit 318ed8562adbcf54e4e266d354ced6dde863eadd @stephenlb stephenlb committed
Showing with 347 additions and 1 deletion.
  1. +69 −0 ruby-rhomobile/README
  2. +226 −0 ruby-rhomobile/pubnub.rb
  3. +52 −1 ruby/README
View
69 ruby-rhomobile/README
@@ -0,0 +1,69 @@
+PubNub - http://github/pubnub/pubnub-api
+
+David Greene
+https://github.com/trumpetx
+http://davidgreene.me/
+Austin, TX
+
+## ------------------------------------------
+## PubNub 3.0 Real-time Cloud Push API - RUBY
+## ------------------------------------------
+##
+## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
+## http://www.pubnub.com/blog/ruby-push-api
+##
+## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
+## This is a cloud-based service for broadcasting Real-time messages
+## to thousands of web and mobile clients simultaneously.
+
+## -------------
+## Ruby Push API
+## -------------
+pubnub = Pubnub.new(
+ "demo", ## PUBLISH_KEY
+ "demo", ## SUBSCRIBE_KEY
+ "", ## SECRET_KEY
+ false ## SSL_ON?
+)
+
+# -------
+# PUBLISH
+# -------
+# Send Message
+info = pubnub.publish({
+ 'channel' => 'hello_world',
+ 'message' => { 'text' => 'some text data' }
+})
+puts(info)
+
+
+# ---------
+# SUBSCRIBE
+# ---------
+def on_activate_app
+ require 'helpers/pubnub'
+ puts('Creating new Pubnub Client API')
+
+ # sub, pub, secret, ssl on or off
+ pubnub = Pubnub.new( 'demo', 'demo', nil, false )
+
+ t1 = Thread.new do
+ pubnub.subscribe({
+ 'channel' => 'hello_world',
+ 'callback' => lambda do |message|
+ WebView.execute_js("alert('#{message}');")
+ return true
+ end
+ })
+ end
+end
+
+# -------
+# HISTORY
+# -------
+# Load Previously Published Messages
+messages = pubnub.history({
+ 'channel' => 'hello_world',
+ 'limit' => 10
+})
+puts(messages)
View
226 ruby-rhomobile/pubnub.rb
@@ -0,0 +1,226 @@
+## www.pubnub.com - PubNub realtime push service in the cloud.
+## http://www.pubnub.com/blog/ruby-push-api - Ruby Push API Blog
+
+## PubNub Real Time Push APIs and Notifications Framework
+## Copyright (c) 2010 Stephen Blum
+## http://www.pubnub.com/
+
+## -----------------------------------
+## PubNub 3.0 Real-time Push Cloud API
+## -----------------------------------
+
+require 'digest/md5'
+require 'uri'
+require 'net/http'
+require 'timeout'
+require 'json'
+
+class Pubnub
+ #**
+ #* Pubnub
+ #*
+ #* 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
+ @ssl = ssl_on
+ @origin = 'pubsub.pubnub.com'
+ @limit = 1800
+
+ 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.
+ #* @return array success information.
+ #*
+ def publish(args)
+ ## Fail if bad input.
+ if !(args['channel'] && args['message'])
+ puts('Missing Channel or Message')
+ return false
+ end
+
+ ## Capture User Input
+ channel = args['channel']
+ message = args['message'].to_json
+
+ ## Sign Message
+ if !@secret_key
+ @secret_key = ''
+ end
+ signature = @secret_key.length > 0 ? Digest::MD5.hexdigest([
+ @publish_key,
+ @subscribe_key,
+ @secret_key,
+ channel,
+ message
+ ].join('/')) : '0'
+
+ ## Fail if message too long.
+ if message.length > @limit
+ puts('Message TOO LONG (' + @limit.to_s + ' LIMIT)')
+ return [ 0, 'Message Too Long.' ]
+ end
+
+ ## Send Message
+ return self._request([
+ 'publish',
+ @publish_key,
+ @subscribe_key,
+ signature,
+ channel,
+ '0',
+ message
+ ])
+ end
+
+ #**
+ #* Subscribe
+ #*
+ #* This is BLOCKING.
+ #* Listen for a message on a channel.
+ #*
+ #* @param array args with channel and message.
+ #* @return false on fail, array on success.
+ #*
+ def subscribe(args)
+ ## Capture User Input
+ channel = args['channel']
+ callback = args['callback']
+ timetoken = args['timetoken'] ? args['timetoken'] : 0
+
+ ## Fail if missing channel
+ if !channel
+ puts "Missing Channel.\n"
+ return false
+ end
+
+ ## Fail if missing callback
+ if !callback
+ puts "Missing Callback.\n"
+ return false
+ end
+
+ ## Begin Recusive Subscribe
+ begin
+ ## Wait for Message
+ response = self._request([
+ 'subscribe',
+ @subscribe_key,
+ channel,
+ '0',
+ timetoken.to_s
+ ])
+
+ messages = response[0]
+ args['timetoken'] = response[1]
+
+
+ ## If it was a timeout
+ if !messages.length
+ return self.subscribe(args)
+ end
+
+ ## Run user Callback and Reconnect if user permits.
+ messages.each do |message|
+ if !callback.call(message)
+ return
+ end
+ end
+
+ ## Keep Listening.
+ return self.subscribe(args)
+ rescue Timeout::Error
+ return self.subscribe(args)
+ rescue
+ sleep(1)
+ return self.subscribe(args)
+ end
+ end
+
+ #**
+ #* History
+ #*
+ #* Load history from a channel.
+ #*
+ #* @param array args with 'channel' and 'limit'.
+ #* @return mixed false on fail, array on success.
+ #*
+ def history(args)
+ ## Capture User Input
+ limit = +args['limit'] ? +args['limit'] : 10
+ channel = args['channel']
+
+ ## Fail if bad input.
+ if (!channel)
+ puts 'Missing Channel'
+ return false
+ end
+
+ ## Get History
+ return self._request([
+ 'history',
+ @subscribe_key,
+ channel,
+ '0',
+ limit.to_s
+ ]);
+ end
+
+ #**
+ #* Time
+ #*
+ #* Timestamp from PubNub Cloud.
+ #*
+ #* @return int timestamp.
+ #*
+ def time()
+ return self._request([
+ 'time',
+ '0'
+ ])[0]
+ end
+
+ #**
+ #* Request URL
+ #*
+ #* @param array request of url directories.
+ #* @return array from JSON response.
+ #*
+ def _request(request)
+ ## Construct Request URL
+ url = @origin + '/' + request.map{ |bit| bit.split('').map{ |ch|
+ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.index(ch) ?
+ '%' + ch.unpack('H2')[0].to_s.upcase : URI.encode(ch)
+ }.join('') }.join('/')
+
+ response = ''
+
+ uri = URI.parse(url)
+ http = Net::HTTP.new(uri.host, uri.port)
+ #http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
+ http.start {
+ http.request_get(uri.path) {|res|
+ response = res.body
+ }
+ }
+
+ return Rho::JSON.parse(response)
+ end
+end
View
53 ruby/README
@@ -1,4 +1,55 @@
Pubnub - http://github/pubnub/pubnub-api
@poptartinc on Twitter, @poptart on Github
-Will merge the actual, live gem in soon :D
+## ------------------------------------------
+## PubNub 3.0 Real-time Cloud Push API - RUBY
+## ------------------------------------------
+##
+## www.pubnub.com - PubNub Real-time Push Service in the Cloud.
+## http://www.pubnub.com/blog/ruby-push-api
+##
+## PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
+## This is a cloud-based service for broadcasting Real-time messages
+## to thousands of web and mobile clients simultaneously.
+
+## -------------
+## Ruby Push API
+## -------------
+pubnub = Pubnub.new(
+ "demo", ## PUBLISH_KEY
+ "demo", ## SUBSCRIBE_KEY
+ "", ## SECRET_KEY
+ false ## SSL_ON?
+)
+
+# -------
+# PUBLISH
+# -------
+# Send Message
+info = pubnub.publish({
+ 'channel' => 'hello_world',
+ 'message' => { 'text' => 'some text data' }
+})
+puts(info)
+
+# ---------
+# SUBSCRIBE
+# ---------
+# Listen for Messages *BLOCKING*
+pubnub.subscribe({
+ 'channel' => 'hello_world',
+ 'callback' => lambda do |message|
+ puts(message) ## print message
+ return true ## keep listening?
+ end
+})
+
+# -------
+# HISTORY
+# -------
+# Load Previously Published Messages
+messages = pubnub.history({
+ 'channel' => 'hello_world',
+ 'limit' => 10
+})
+puts(messages)

0 comments on commit 318ed85

Please sign in to comment.
Something went wrong with that request. Please try again.