Skip to content

Commit

Permalink
converting to google app-engine / datamapper
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Sep 22, 2009
1 parent 125707e commit cb53240
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
15 changes: 6 additions & 9 deletions config.ru
@@ -1,12 +1,9 @@
require 'rubygems' require 'appengine-rack'
require 'sinatra'
require 'sequel'
require 'rest_client'
require 'zlib'
require 'json'


Sinatra::Application.set :run => false AppEngine::Rack.configure_app(
Sinatra::Application.set :environment => ENV['RACK_ENV'] :application => 'watercoolr',
:version => 1
)


require 'watercoolr.rb' require 'watercoolr.rb'
run Sinatra::Application run Sinatra::Application
63 changes: 33 additions & 30 deletions watercoolr.rb
@@ -1,26 +1,26 @@
require 'rubygems' require 'rubygems'
require 'sinatra' require 'sinatra'
require 'sequel'
require 'httpclient'
require 'zlib' require 'zlib'
require 'json' require 'json/pure'
require 'uri'
require 'dm-core'


configure do # patch Net/HTTP interface
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://watercoolr.db') require 'appengine-apis/urlfetch'
unless DB.table_exists? "channels" Net::HTTP = AppEngine::URLFetch::HTTP
DB.create_table :channels do
primary_key :id
varchar :name, :size => 32
end
end


unless DB.table_exists? "subscribers" # Configure DataMapper to use the App Engine datastore
DB.create_table :subscribers do DataMapper.setup(:default, "appengine://auto")
primary_key :id
foreign_key :channel_id class Channel
varchar :url, :size => 128 include DataMapper::Resource
end property :name, String, :key => true
end end

class Subscriber
include DataMapper::Resource
property :channel_name, String, :key => true
property :url, String, :lazy => false
end end


helpers do helpers do
Expand All @@ -31,27 +31,26 @@ def gen_id
end end
end end




get '/' do get '/' do
erb :index erb :index
end end


post '/channels' do post '/channels' do
id = gen_id id = gen_id
DB[:channels] << { :name => id } channel = Channel.create(:name => id)
{ :id => id.to_s }.to_json { :id => channel.name }.to_json
end end


post '/subscribers' do post '/subscribers' do
res = false res = false
data = JSON.parse(params[:data]) data = JSON.parse(params[:data])
channel_name = data['channel'] || 'boo' channel_name = data['channel'] || 'boo'
url = data['url'] || nil url = data['url'] || nil
if rec = DB[:channels].filter(:name => channel_name).first
if url and rec[:id] if rec = Channel.first(:name => channel_name)
unless DB[:subscribers].filter(:channel_id => rec[:id], :url => url).first if url and rec.name
res = DB[:subscribers] << { :channel_id => rec[:id], :url => url } unless Subscriber.first(:channel_name => rec.name, :url => url)
res = Subscriber.create(:channel_name => rec.name, :url => url)
end end
end end
end end
Expand All @@ -67,18 +66,22 @@ def gen_id
data = JSON.parse(params[:data]) data = JSON.parse(params[:data])
channel_name = data['channel'] || 'boo' channel_name = data['channel'] || 'boo'
message = data['message'] || nil message = data['message'] || nil
if rec = DB[:channels].filter(:name => channel_name).first
if message and rec[:id] if rec = Channel.first(:name => channel_name)
subs = DB[:subscribers].filter(:channel_id => rec[:id]).to_a if message and rec.name
subs = Subscriber.all(:channel_name => rec.name)
if subs if subs
subs.each do |sub| subs.each do |sub|
begin begin
HTTPClient.post(sub[:url], :data => message) url = URI.parse(sub.url)
conn = Net::HTTP.new(url.host, url.port)
resp, body = conn.post(url.path, message)
rescue rescue
end end
res = true res = true
end end
end end

end end
end end
if res if res
Expand Down

0 comments on commit cb53240

Please sign in to comment.