Class: Switch
- Inherits:
-
Object
- Object
- Switch
- Defined in:
- lib/switch.rb
Constant Summary collapse
- ON_MESSAGE =
"ON"- OFF_MESSAGE =
"OFF"
Instance Method Summary collapse
- #call(env) ⇒ Object
- #get_switch ⇒ Object
-
#initialize(app_name, options) ⇒ Switch
constructor
A new instance of Switch.
- #set_switch(message) ⇒ Object
Constructor Details
#initialize(app_name, options) ⇒ Switch
Returns a new instance of Switch.
9 10 11 12 13 14 15 16 |
# File 'lib/switch.rb', line 9 def initialize app_name, raise RuntimeError unless app_name defaults = { :file_location => "/tmp", :url_namespace => "/switch" } = defaults.merge() @url_namespace = [:url_namespace] @app_name = app_name @file = File.join([:file_location], "#{@app_name}_switch") end |
Instance Method Details
#call(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/switch.rb', line 29 def call(env) request = Rack::Request.new env if request.get? if request.path == "#{@url_namespace}/#{@app_name}" case get_switch when ON_MESSAGE return [200, {}, ["#{@app_name} is #{ON_MESSAGE}"]] when OFF_MESSAGE return [503, {}, ["#{@app_name} is #{OFF_MESSAGE}"]] end end elsif request.post? case request.path when "#{@url_namespace}/#{@app_name}/on" set_switch ON_MESSAGE return [200, {}, ["#{@app_name} set to #{ON_MESSAGE}"]] when "#{@url_namespace}/#{@app_name}/off" set_switch OFF_MESSAGE return [200, {}, ["#{@app_name} set to #{OFF_MESSAGE}"]] end end return [404, {}, "Not found"] end |
#get_switch ⇒ Object
18 19 20 21 22 23 |
# File 'lib/switch.rb', line 18 def get_switch return ON_MESSAGE if ! File.exist? @file result = File.open(@file, 'r').read return ON_MESSAGE unless [ON_MESSAGE, OFF_MESSAGE].include?(result) result end |
#set_switch(message) ⇒ Object
25 26 27 |
# File 'lib/switch.rb', line 25 def set_switch File.open(@file, 'w') {|f| f.write() } end |