Created
February 23, 2012 17:29
-
-
Save mort/1893923 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'faye' | |
Faye::Logging.log_level = :debug | |
Faye.logger = lambda { |m| puts m } | |
class LinkResolver | |
def self.fetch(m) | |
link = begin | |
OGResolver.fetch(m) | |
rescue OGFail | |
PismoResolver.fetch(m) | |
rescue PismoFail | |
{:url => m, :grabber => nil} | |
end | |
link | |
end | |
end | |
class OGResolver | |
require 'opengraph' | |
def self.fetch(url) | |
doc = OpenGraph.fetch(url) | |
raise OGFail unless doc | |
doc.merge(:grabber => :og) | |
end | |
end | |
class PismoResolver | |
require 'pismo' | |
def self.fetch(url) | |
doc = Pismo::Document.new(url) | |
raise PismoFail unless doc | |
{:url => url, :title => doc.title, :description => doc.lede, :grabber => :pismo} | |
end | |
end | |
class OGFail < StandardError | |
end | |
class PismoFail < StandardError | |
end | |
class Foo | |
def incoming(msg, callback) | |
# [{"channel":"/foo","data":{"text":"http://www.guardian.co.uk/media/2012/feb/27/sun-culture-illegal-payments-leveson"},"clientId":"e4s6s2z6lj0tw7k1rq2phugss","id":"7"}] | |
puts "Foo" | |
callback.call(msg) | |
end | |
end | |
bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25) | |
bayeux.add_extension(Foo.new) | |
bayeux.listen(9292) | |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<!-- | |
Created using / | |
Source can be edited via /ezuke/2498/edit | |
--> | |
<head> | |
<meta charset=utf-8 /> | |
<title>Manantial</title> | |
<style> | |
#drop { | |
height: 600px; | |
width: 800px; | |
border: 3px dashed #ccc; | |
margin: 0 auto; | |
padding: 10px; | |
overflow: hidden; | |
} | |
p { | |
margin: 3px 0; | |
} | |
p.link { | |
border: 1px solid #CCC; | |
padding: 20px; | |
} | |
p.link a { | |
display: block; | |
} | |
</style> | |
<script src="http://html5demos.com/h5utils.js"></script> | |
<script src="http://0.0.0.0:9292/faye.js"></script> | |
<script type="text/javascript"> | |
var client = new Faye.Client('http://0.0.0.0:9292/faye'); | |
var subscription = client.subscribe('/foo', function(message) { | |
//console.log(message); | |
var msg = '<p class="link"><a href="' + message.text + '">'+message.text+'</a></p>'; | |
if ((message.text.indexOf('.jpg') != -1) || (message.text.indexOf('.gif') != -1)) { | |
var msg = '<p class="link"><a href="' + message.text + '"><img src="'+message.text+'" /></a></p>'; | |
} | |
document.querySelector('#drop').innerHTML = msg + document.querySelector('#drop').innerHTML; | |
}); | |
subscription.callback(function() { | |
//console.log('Subscription is now active!'); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="drop"></div> | |
<script> | |
function cancel(e) { | |
if (e.preventDefault) { | |
e.preventDefault(); | |
} | |
return false; | |
} | |
var drop = document.querySelector('#drop'); | |
// Tells the browser that we *can* drop on this target | |
addEvent(drop, 'dragover', cancel); | |
addEvent(drop, 'dragenter', cancel); | |
addEvent(drop, 'drop', function (e) { | |
if (e.preventDefault) e.preventDefault(); // stops the browser from redirecting off to the text. | |
var msg = e.dataTransfer.getData('Text'); | |
//this.innerHTML += '<p>' + msg + '</p>'; | |
client.publish('/foo', {text: msg}); | |
return false; | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment