-
Notifications
You must be signed in to change notification settings - Fork 0
/
evoke.rb
105 lines (85 loc) · 2.94 KB
/
evoke.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require File.join(File.dirname(__FILE__), 'config', 'boot')
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
# 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)
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)
end
end
delete "/callbacks/:guid" do
manage_resource(Callback.by_guid(params['guid']), :response => nil) do |callback|
callback.destroy
end
end
end # Api
class Status < Sinatra::Base
register Sinatra::Chicago
helpers Sinatra::Chicago::Helpers
helpers Sinatra::Authorization
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