Permalink
Browse files

Broke API and Status handlers into two Sinatra modules. Fun times

  • Loading branch information...
1 parent 2d3e19a commit 6cada5776fd4507c1587830f91c6c4f1a5ddf5b4 @gus gus committed Oct 2, 2009
Showing with 127 additions and 118 deletions.
  1. +3 −1 config.ru
  2. +1 −1 config/boot.rb
  3. +90 −88 evoke.rb
  4. +1 −1 models/{status.rb → system_status.rb}
  5. +5 −24 test/{evoke_test.rb → evoke_api_test.rb}
  6. +27 −0 test/evoke_status_test.rb
  7. +0 −3 test/test_helper.rb
View
@@ -5,4 +5,6 @@ require 'evoke'
use Rack::CommonLogger, File.new("#{File.dirname(__FILE__)}/log/#{ENV['APP_ENV']}.log", 'a+')
-map("/") { run Evoke }
+use Evoke::Api
+use Evoke::Status
+run Sinatra::Base
View
@@ -7,7 +7,7 @@ def require_local_lib(path)
Configuration = YAML.load_file(File.join(File.dirname(__FILE__), 'config.yml'))
LIBS = %w[rubygems logger config/database rest_client delayed_job haml sass
- sinatra chicago sinatra/authorization]
+ sinatra/base chicago sinatra/authorization]
LIBS.each { |lib| require lib }
require_local_lib('../models')
View
178 evoke.rb
@@ -1,103 +1,105 @@
require File.join(File.dirname(__FILE__), 'config', 'boot')
-class Evoke < Sinatra::Base
- register Sinatra::Chicago
- helpers Sinatra::Chicago::Helpers
- helpers Sinatra::Chicago::Responders
- helpers Sinatra::Authorization
-
- error do
- $stdout.puts "Sorry there was a nasty error - #{request.env['sinatra.error'].inspect}"
- end
-
- # Resource management
-
- not_found { throw :halt, [404, json_response('')] }
-
- def valid_record(record, options={})
- options = {:status => 200, :response => record}.merge(options)
- status(options[:status])
- json_response(options[:response])
- end
-
- def invalid_record(record)
- # Need a simple logging facility
- # $stdout.puts "ERROR: record #{record.inspect} says #{record.errors.full_messages.inspect}"
- throw :halt, [422, json_response(:errors => record.errors.full_messages)]
- end
-
- def manage_resource(resource, options={})
- raise Sinatra::NotFound unless resource
- yield(resource) if block_given?
- valid_record(resource, options)
- rescue ActiveRecord::RecordInvalid => e
- invalid_record(e.record)
- end
-
- # Actions
-
- get "/callbacks/:guid" do
- manage_resource(Callback.by_guid(params['guid']))
- end
-
- post "/callbacks" do
- manage_resource(Callback.new(params), :status => 201) do |callback|
- callback.save!
- CallbackRunner.make_job_from_callback!(callback)
+module Evoke
+ class Api < Sinatra::Base
+ register Sinatra::Chicago
+ helpers Sinatra::Chicago::Helpers
+ helpers Sinatra::Chicago::Responders
+
+ error do
+ $stdout.puts "Sorry there was a nasty error - #{request.env['sinatra.error'].inspect}"
end
- end
- put "/callbacks/:guid" do
- manage_resource(Callback.by_guid(params['guid'])) do |callback|
- attributes = params.reject {|k,v| k == "guid"}
- callback.update_attributes!(attributes)
- CallbackRunner.replace_job_for_callback!(callback)
+ # Resource management
+
+ not_found { throw :halt, [404, json_response('')] }
+
+ def valid_record(record, options={})
+ options = {:status => 200, :response => record}.merge(options)
+ status(options[:status])
+ json_response(options[:response])
+ end
+
+ def invalid_record(record)
+ # Need a simple logging facility
+ # $stdout.puts "ERROR: record #{record.inspect} says #{record.errors.full_messages.inspect}"
+ throw :halt, [422, json_response(:errors => record.errors.full_messages)]
+ end
+
+ def manage_resource(resource, options={})
+ raise Sinatra::NotFound unless resource
+ yield(resource) if block_given?
+ valid_record(resource, options)
+ rescue ActiveRecord::RecordInvalid => e
+ invalid_record(e.record)
end
- end
- delete "/callbacks/:guid" do
- manage_resource(Callback.by_guid(params['guid']), :response => nil) do |callback|
- callback.destroy
+ # Actions
+
+ get "/callbacks/:guid" do
+ manage_resource(Callback.by_guid(params['guid']))
end
- # begin
- # callback = Callback.by_guid(params['guid'])
- # raise Sinatra::NotFound unless callback
- # callback.destroy
- # status(200)
- # json_response("")
- # rescue ActiveRecord::RecordInvalid => e
- # invalid_record(e.record)
- # end
- end
-
- #
- # Status and stuff
-
- catch_all_css
-
- helpers do
- def authorize(username, password)
- [username, password] == Configuration["authorization"].values_at("username", "password")
+
+ post "/callbacks/" do
+ manage_resource(Callback.new(params), :status => 201) do |callback|
+ callback.save!
+ CallbackRunner.make_job_from_callback!(callback)
+ end
end
- def truncate(str, n)
- str.length > n ? "#{str[0..n]}..." : str
+ put "/callbacks/:guid" do
+ manage_resource(Callback.by_guid(params['guid'])) do |callback|
+ attributes = params.reject {|k,v| k == "guid"}
+ callback.update_attributes!(attributes)
+ CallbackRunner.replace_job_for_callback!(callback)
+ end
end
- def verbal_status_message(callback)
- if callback.called_back?
- haml '.okay Already evoked callback', :layout => false
- elsif callback.should_have_been_called_back? && !callback.called_back?
- haml '.uhoh This callback should have been evoked but has not yet', :layout => false
- else
- haml '.okay Just waiting for callback time', :layout => false
+ delete "/callbacks/:guid" do
+ manage_resource(Callback.by_guid(params['guid']), :response => nil) do |callback|
+ callback.destroy
end
end
- end
+ end # Api
+
+ class Status < Sinatra::Base
+ register Sinatra::Chicago
+ helpers Sinatra::Chicago::Helpers
+ helpers Sinatra::Authorization
- get "/status" do
- login_required
- @status = Status.new
- haml :status, :layout => :application
- end
+ error do
+ $stdout.puts "Sorry there was a nasty error - #{request.env['sinatra.error'].inspect}"
+ end
+
+ #
+ # Status and stuff
+
+ catch_all_css
+
+ helpers do
+ def authorize(username, password)
+ [username, password] == Configuration["authorization"].values_at("username", "password")
+ end
+
+ def truncate(str, n)
+ str.length > n ? "#{str[0..n]}..." : str
+ end
+
+ def verbal_status_message(callback)
+ if callback.called_back?
+ haml '.okay Already evoked callback', :layout => false
+ elsif callback.should_have_been_called_back? && !callback.called_back?
+ haml '.uhoh This callback should have been evoked but has not yet', :layout => false
+ else
+ haml '.okay Just waiting for callback time', :layout => false
+ end
+ end
+ end
+
+ get "/status" do
+ login_required
+ @status = SystemStatus.new
+ haml :status, :layout => :application
+ end
+ end # Status
end # Evoke
@@ -1,7 +1,7 @@
require 'drb'
require 'god'
-class Status
+class SystemStatus
def recent_callbacks; Callback.recent; end
def total_callback_count; Callback.count; end
def jobs_in_queue_count; Delayed::Job.count; end
@@ -1,19 +1,19 @@
require File.join(File.dirname(__FILE__), 'test_helper')
-class EvokeTest < Test::Unit::TestCase
+class EvokeApiTest < Test::Unit::TestCase
def app
- @app = Evoke
+ @app = Evoke::Api
end
context "adding a callback" do
context "when missing url" do
- setup { post "/callbacks", Factory.attributes_for(:callback, :url => "") }
+ setup { post "/callbacks/", Factory.attributes_for(:callback, :url => "") }
should_have_response_status 422
should_have_json_response :errors => ["Url can't be blank"]
end
context "when missing call back at" do
- setup { post "/callbacks", Factory.attributes_for(:callback, :callback_at => "") }
+ setup { post "/callbacks/", Factory.attributes_for(:callback, :callback_at => "") }
should_have_response_status 422
should_have_json_response :errors => ["Callback at can't be blank"]
end
@@ -22,7 +22,7 @@ def app
setup do
@guid = Factory.next(:guid)
CallbackRunner.expects(:make_job_from_callback!).with(anything)
- post "/callbacks", Factory.attributes_for(:callback, :guid => @guid)
+ post "/callbacks/", Factory.attributes_for(:callback, :guid => @guid)
end
should_have_response_status 201
should_have_json_response { Callback.first }
@@ -148,23 +148,4 @@ def app
end
end # deleting a callback
- context "displaying status" do
- context "when logged in" do
- setup do
- 10.times { |n| Factory(:callback, :guid => "aphex-analord-#{n}") }
- credentials = ["foo:bar"].pack("m*")
- get '/status', {}, { "HTTP_AUTHORIZATION" => "Basic #{credentials}" }
- end
-
- should_have_response_body(/(li class='callback')/)
- should_have_response_status 200
- end
-
- context "when not logged in" do
- setup { get '/status' }
-
- should_have_response_status 401
- end
- end # displaying status
-
end
View
@@ -0,0 +1,27 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+class EvokeStatusTest < Test::Unit::TestCase
+ def app
+ @app = Evoke::Status
+ end
+
+ context "displaying status" do
+ context "when logged in" do
+ setup do
+ 10.times { |n| Factory(:callback, :guid => "aphex-analord-#{n}") }
+ credentials = ["foo:bar"].pack("m*")
+ get '/status', {}, { "HTTP_AUTHORIZATION" => "Basic #{credentials}" }
+ end
+
+ should_have_response_body(/(li class='callback')/)
+ should_have_response_status 200
+ end
+
+ context "when not logged in" do
+ setup { get '/status' }
+
+ should_have_response_status 401
+ end
+ end # displaying status
+
+end
View
@@ -33,7 +33,4 @@ def run(*args, &block)
end
end
end
-
- def O(*args) OpenStruct.new(*args); end # Shortcut for OpenStruct
-
end

0 comments on commit 6cada57

Please sign in to comment.