Skip to content
Browse files

Add first draft of app

  • Loading branch information...
1 parent 42d8309 commit 420522048bb290240a4897d4b9f53e3a7bf13cc6 @richo committed Mar 24, 2012
Showing with 90 additions and 0 deletions.
  1. +1 −0 .rvmrc
  2. +4 −0 Gemfile
  3. +8 −0 config.ru
  4. +37 −0 lib/oauth_proxy.rb
  5. +40 −0 lib/worker.rb
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm 1.9.3
View
4 Gemfile
@@ -0,0 +1,4 @@
+source :rubygems
+
+gem "sinatra"
+gem "uuid"
View
8 config.ru
@@ -0,0 +1,8 @@
+require File.join(File.dirname(__FILE__), 'lib', 'oauth_proxy')
+require File.join(File.dirname(__FILE__), 'lib', 'worker')
+
+Thread.new do
+ OauthProxy::Worker.new.run
+end
+
+run OauthProxy::App
View
37 lib/oauth_proxy.rb
@@ -0,0 +1,37 @@
+require 'uuid'
+require 'sinatra/base'
+
+module OauthProxy
+ extend self
+
+ @uuid = UUID.new
+
+ attr_reader :callbacks
+
+ @callbacks = {}
+
+ def remember(v)
+ id = @uuid.generate
+ @callbacks[id] = v
+
+ return id
+ end
+
+ def forget(k)
+ @callbacks.delete(k)
+ end
+
+ class App < Sinatra::Base
+ get '/callback/:slug' do
+ unless OauthProxy.callbacks.include?(params['slug'])
+ halt 404, "Slug not found"
+ end
+
+ OauthProxy.callbacks[params['slug']].q.enq :done
+ end
+
+ get '/' do
+ "Rawr at index!"
+ end
+ end
+end
View
40 lib/worker.rb
@@ -0,0 +1,40 @@
+require 'socket'
+require 'thread'
+
+module OauthProxy
+ class Worker
+
+ def run
+ server = TCPServer.new 2000
+ loop do
+ Thread.start(server.accept) {|client| handle(client)}
+ end
+ end
+
+ def handle(s)
+ req = Request.new
+ id = ::OauthProxy.remember(req)
+ s.puts("#{id} Added to queue")
+ # Bind until we're done here- work out how to poll effciently
+ req.q.deq
+ s.puts("#{id} Processed")
+ #
+ # In the webapp we call req.q.enq :status
+ ensure
+ ::OauthProxy.forget(id)
+ s.close
+ end
+ end
+
+ class Request
+
+ def initialize
+ @q = Queue.new
+ end
+
+ attr_accessor :oauth_token, :oauth_token_secret
+ attr_reader :q
+
+ end
+
+end

0 comments on commit 4205220

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