From 0ac265a4a701809191145d6eb2a5b2c26d27a93a Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Wed, 24 Oct 2012 19:36:17 +0200 Subject: [PATCH] allow setting token manually --- config.ru | 1 + lib/travis/web.rb | 1 + lib/travis/web/set_token.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 lib/travis/web/set_token.rb diff --git a/config.ru b/config.ru index 667188a1..f9997d8e 100644 --- a/config.ru +++ b/config.ru @@ -5,6 +5,7 @@ ENV['RAILS_ENV'] = ENV['RACK_ENV'] $: << 'lib' require 'travis/web' +use Travis::Web::SetToken use Travis::Web::Allow use Travis::Web::ApiRedirect do |app| app.settings.api_endpoint = ENV['API_ENDPOINT'] if ENV['API_ENDPOINT'] diff --git a/lib/travis/web.rb b/lib/travis/web.rb index 664477df..7d8a6f09 100644 --- a/lib/travis/web.rb +++ b/lib/travis/web.rb @@ -3,5 +3,6 @@ module Travis autoload :Allow, 'travis/web/allow' autoload :ApiRedirect, 'travis/web/api_redirect' autoload :App, 'travis/web/app' + autoload :SetToken, 'travis/web/set_token' end end diff --git a/lib/travis/web/set_token.rb b/lib/travis/web/set_token.rb new file mode 100644 index 00000000..13a3222f --- /dev/null +++ b/lib/travis/web/set_token.rb @@ -0,0 +1,32 @@ +require 'rack/request' +require 'rack/response' + +module Travis + module Web + class SetToken + attr_accessor :app, :template + + def initialize(app) + @app, @template = app, File.read(__FILE__).split('__END__').last + end + + def call(env) + return app.call(env) unless info = info_for(env) + Rack::Response.new(template % info).finish + end + + def info_for(env) + return unless env['REQUEST_METHOD'] == 'POST' + info = Rack::Request.new(env).params.values_at('token', 'user') + info if info.first =~ /\A[a-zA-Z\-_\d]+\Z/ + end + end + end +end + +__END__ +