Skip to content

Commit

Permalink
adding Ruby example
Browse files Browse the repository at this point in the history
removing PHP specific text
  • Loading branch information
leggetter committed Feb 1, 2012
1 parent 46661fd commit 01837e6
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
*.DS_Store
examples/php/config.php
examples/ruby-sinatra/config.rb
24 changes: 22 additions & 2 deletions README.md
Expand Up @@ -4,7 +4,7 @@

The purpose of the PushNotifier.js library is to make is super easy to add simple Push notifications to any web application. It uses the [jQuery Gritter Growl plugin](http://boedesign.com/blog/2009/07/11/growl-for-jquery-gritter/) for the UI and [Pusher](http://pusher.com) for realtime push notifications.

The first version of this sample shows how to perform Realtime Push Notifications from PHP. Later versions will demonstrate how to achieve this using other backend technologies.
The first version of this sample shows examples of server functionality in PHP and Ruby with Sinatra. If you'd like to see the example in other languages please [get in touch](http://pusher.com/support).

## How easy is it?

Expand All @@ -28,13 +28,25 @@ It's as easy as:

3. Trigger events on your server and see them instantly appear in your web app:

**PHP**

$app_key = 'YOUR_APP_KEY';
$app_secret = 'YOUR_APP_SECRET';
$app_id = 'YOUR_APP_ID';

$pusher = new Pusher($app_key, $app_secret, $app_id);
$data = array('message' => 'This is an HTML5 Realtime Push Notification!');
$pusher->trigger('my_notifications', 'notification', $data);
**Ruby**
require 'pusher'

Pusher.app_id = 'YOUR_APP_ID'
Pusher.key = 'YOUR_APP_KEY'
Pusher.secret = 'YOUR_APP_SECRET'

data = {'message' => 'This is an HTML5 Realtime Push Notification!'}
Pusher['my_notifications'].trigger('notification', data)

## Tutorial

Expand All @@ -50,4 +62,12 @@ http://html5-realtime-push-notifications.phpfogapp.com/
### PHP

1. Rename `examples/php/config.example.php` to `config.php` and add your Pusher app credentials.
2. Running on a web server navigate to `examples/index.html` to see a side-by-side page example and click the 'Notify' button to trigger a notification.
2. Running on a web server navigate to `examples/index.html` to see a side-by-side page example and click the 'Notify' button to trigger a notification.

### Ruby - sinatra

1. Rename `examples/ruby-sinatra/config_example.rb` to `config.rb` and add your Pusher app credentials
2. Update `examples/notify.html` so that the `NOTIFY_ENDPOINT` has the value `/notify`
3. In `examples/ruby-sinatra` run `bundle install` to install the dependencies defined in `examples/ruby-sinatra/Gemfile`
4. Start the Sinatra server by running `bundle exec ruby -rubygems notify.rb`
5. Navigate to http://localhost:4567 (default for sinatra) to see a side-by-side page example and click the 'Notify' button to trigger a notification.
2 changes: 1 addition & 1 deletion examples/index.html
Expand Up @@ -2,7 +2,7 @@
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>PHP HTML5 Push Notifications using Pusher - Frameset</title>
<title>HTML5 Push Notifications using Pusher - Frameset</title>
</head>

<frameset cols="50%,50%">
Expand Down
15 changes: 9 additions & 6 deletions examples/notify.html
Expand Up @@ -2,7 +2,7 @@
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>PHP HTML5 Push Notifications using Pusher</title>
<title>HTML5 Push Notifications using Pusher</title>

<link rel="stylesheet/less" type="text/css" href="lib/twitter-bootstrap/lib/bootstrap.less">
<script src="lib/less/less-1.1.5.min.js"></script>
Expand All @@ -19,10 +19,13 @@
<script src="../src/PusherNotifier.js"></script>

<script>
var NOTIFY_ENDPOINT = "php/notify.php";
//var NOTIFY_ENDPOINT = '/notify'; // ruby-sinatra

$(function() {
$("a[href='#notify']").click(function() {
$.ajax({
url: "php/notify.php",
url: NOTIFY_ENDPOINT,
data: {"message": "I'm a notification!"}
});
});
Expand All @@ -38,7 +41,7 @@
var message = $.trim($("#notifyMessage").val());
if(message) {
$.ajax({
url: "php/notify.php",
url: NOTIFY_ENDPOINT,
data: {"message": message}
});
}
Expand All @@ -52,14 +55,14 @@
<div class="topbar">
<div class="fill">
<div class="container">
<a class="brand" href="/">PHP HTML5 Push Notifications using Pusher</a>
<a class="brand" href="/">HTML5 Push Notifications using Pusher</a>
</div>
</div>
</div>

<div class="hero-unit">
<h1>PHP HTML5 Push Notifications</h1>
<p>This page demonstrates HTML5 Realtime Push Notifications using <a href="http://pusher.com">Pusher</a>. This functionality can easily be added to any PHP application.</p>
<h1>HTML5 Push Notifications</h1>
<p>This page demonstrates HTML5 Realtime Push Notifications using <a href="http://pusher.com">Pusher</a>. This functionality can easily be added to any web application.</p>
<p>The demo below <a href="#notify">Triggers a notification</a> using the <a href="https://github.com/squeeks/Pusher-PHP">PHP Pusher library</a> and displays it using the <a href="https://github.com/jboesch/Gritter">jQuery Gritter notification plugin</a>, which are included within the source.</p>
<p><a href="https://github.com/pusher/html5-realtime-push-notifications" class="btn primary large" target="_blank">Get the code &raquo;</a></p>
<p><small>We plan to update this sample to contain backend code in other technologies. Feel free to <a href="https://github.com/pusher/html5-realtime-push-notifications">fork</a> and try it for yourself.</small></p>
Expand Down
5 changes: 5 additions & 0 deletions examples/ruby-sinatra/Gemfile
@@ -0,0 +1,5 @@
source "http://rubygems.org"
gem 'sinatra'
gem 'pusher'
gem 'uuid'
gem 'json'
34 changes: 34 additions & 0 deletions examples/ruby-sinatra/Gemfile.lock
@@ -0,0 +1,34 @@
GEM
remote: http://rubygems.org/
specs:
json (1.6.5)
macaddr (1.5.0)
systemu (>= 2.4.0)
multi_json (1.0.4)
pusher (0.9.2)
multi_json (~> 1.0)
ruby-hmac (~> 0.4.0)
signature (~> 0.1.2)
rack (1.4.1)
rack-protection (1.2.0)
rack
ruby-hmac (0.4.0)
signature (0.1.2)
ruby-hmac
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
systemu (2.4.2)
tilt (1.3.3)
uuid (2.3.5)
macaddr (~> 1.0)

PLATFORMS
ruby

DEPENDENCIES
json
pusher
sinatra
uuid
5 changes: 5 additions & 0 deletions examples/ruby-sinatra/config_example.rb
@@ -0,0 +1,5 @@
require 'pusher'

Pusher.app_id = 'YOUR_APP_ID'
Pusher.key = 'YOUR_APP_KEY'
Pusher.secret = 'YOUR_APP_SECRET'
68 changes: 68 additions & 0 deletions examples/ruby-sinatra/notify.rb
@@ -0,0 +1,68 @@
require 'config.rb'

require 'sinatra'
require 'pusher'
require 'json'

include Rack::Utils

set :public_folder, '../'

get '/' do
send_file('../index.html')
end

get '/notify' do

message = params[:message]

if( !message )
status 400
body 'message must be provided'
end

message = sanitise_input(message)
data = {'message' => message}
response = Pusher['my_notifications'].trigger('notification', data)

result = {'activity' => data, 'pusherResponse' => response}

status 200
headers \
'Cache-Control' => 'no-cache, must-revalidate',
'Content-Type' => 'application/json'

body result.to_json
end

get '/src/*' do
path = params[:splat].join()
path = path.gsub(/(\.\.)|~/, '') # remove ../ and ~
path = '../../src/' + path
type = path[path.rindex('.')+1, path.length]
puts('getting: ' + path + ' type: ' + type)
if(File.exists?(path))
status 200
headers \
'Cache-Control' => 'no-cache, must-revalidate',
'Content-Type' => get_content_type(type)
File.read(path)
else
status 404
end
end

def get_content_type(type)
case type
when "css"
return 'text/css'
when "js"
return 'text/javascript'
else
return 'text/plain'
end
end

def sanitise_input(message)
return escape_html(message).slice(0, 300)
end

0 comments on commit 01837e6

Please sign in to comment.