Permalink
Browse files

App valid? API

  • Loading branch information...
1 parent 079b666 commit 088a78fb1158f6cbf37e6ad0eca6ba1f5d9d218b @avinasha avinasha committed Sep 13, 2013
Showing with 41 additions and 0 deletions.
  1. +7 −0 lib/base.rb
  2. +14 −0 run_app.rb
  3. +20 −0 spec/lib/base_spec.rb
View
@@ -172,6 +172,7 @@ def setup_for(sinatra_app)
attr_reader :auth
attr_reader :settings
attr_reader :store
+ attr_accessor :errors
attr_writer :ca_file
@@ -181,8 +182,14 @@ def initialize(data = {}, payload = {})
@settings = @data[:settings] || {}
@payload = pre_process_payload(payload)
@store = SupportBeeApp::Store.new(redis_key_prefix: redis_key_prefix)
+ @errors = {}
end
+ def valid?
+ return true unless self.respond_to?(:validate)
+ validate
+ end
+
def trigger_event(event)
@event = event
method = to_method(event)
View
@@ -36,6 +36,20 @@ def self.setup(app_class)
{app_class.slug => response}.to_json
end
+ post "/#{app_class.slug}/valid" do
+ data, payload = parse_request
+ response = {}
+ app = app_class.new(data, payload)
+ if app.valid?
+ status 200
+ else
+ status 400
+ response = app.errors
+ end
+ content_type :json
+ response.to_json
+ end
+
post "/#{app_class.slug}/event/:event" do
data, payload = parse_request
event = params[:event]
View
@@ -71,6 +71,26 @@ def create_dummy_instance
app.store.redis_key_prefix.should == "#{app.class.slug}:#{app.auth.subdomain}"
end
+ describe "Validation" do
+ describe "#valid?" do
+ context "does not respond to validate" do
+ it "returns true" do
+ dummy = create_dummy_instance
+ dummy.should be_valid
+ end
+ end
+
+ context "responds to validate" do
+ it "calls the validate method" do
+ dummy = create_dummy_instance
+ def dummy.validate; end;
+ flexmock(dummy).should_receive(:validate).and_return(true).once
+ dummy.valid?
+ end
+ end
+ end
+ end
+
describe "Receive" do
context "Event" do
it "should trigger an event" do

0 comments on commit 088a78f

Please sign in to comment.