Created
August 19, 2009 17:13
-
-
Save marks/170474 to your computer and use it in GitHub Desktop.
This file contains 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 'rubygems' | |
require 'sinatra' | |
require 'appengine-apis/logger' | |
require 'appengine-apis/datastore' | |
require 'json' | |
get '/' do | |
logger.info "Sinatra in your Google Appengine p0wning your JRuby" | |
"I AM SINATRA! Doing a ditty for you on Google Appengine with JRuby!" | |
end | |
get '/chats' do | |
chats = AppEngine::Datastore::Query.new('Chats').fetch | |
chats_array = [] | |
chats.each do |chat| | |
#chats_array << chat.to_hash | |
begin | |
logger.info chat.to_hash.inspect | |
rescue => err | |
logger.info err | |
end | |
end | |
chats_array.to_json | |
end | |
post '/bot' do | |
logger.info "Received: " + params.inspect | |
begin | |
log_chat params | |
#rebuilt_chat = rebuild_chat params | |
"Echo: " + params["msg"] | |
rescue => err | |
err | |
end | |
end | |
def logger | |
AppEngine::Logger.new | |
end | |
def log_chat(params) | |
chat = AppEngine::Datastore::Entity.new('Chats') | |
params.each do |key, value| | |
chat[key.to_sym] = "#{value}" | |
end | |
result = AppEngine::Datastore.put chat | |
logger.info chat.inspect | |
end | |
def rebuild_chat(chat) | |
rebuilt_chat = chat["msg"] | |
if chat["step"] | |
chat["step"].to_i.times do | |
rebuilt_chat = rebuilt_chat + chat["value#{chat["step"].to_i - 1}"] | |
end | |
end | |
rebuilt_chat | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment