aframe-switch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ = Switch
2
+
3
+ Set and retrieve an arbitrary switch flag via GET and POST. Requires Rack.
4
+
5
+ == Use
6
+
7
+ Switch.new("light", { :url_namespace => "/switch", :file_location => "/tmp" })
8
+
9
+ == Functionality
10
+
11
+ This will expose:
12
+
13
+ A route to get the current state of the switch:
14
+ /switch/light
15
+
16
+ A route for flicking the switch to ON:
17
+ /switch/light/on
18
+
19
+ A route for flicking the switch to OFF:
20
+ /switch/light/off
21
+
22
+ == Switch state possibilities
23
+
24
+ The switch state is stored in a file:
25
+ /tmp/light_switch
26
+
27
+ File doesn't exist
28
+ switch is on
29
+ File contains ON
30
+ switch is on
31
+ File contains OFF
32
+ server is off
33
+
34
+ == An example config.ru file for mounting alongside Padrino:
35
+
36
+ require ::File.dirname(__FILE__) + '/config/boot.rb'
37
+ require 'switch'
38
+ run Rack::Cascade.new([
39
+ Switch.new("aframe_webapp_status"),
40
+ Padrino.application,
41
+ ])
@@ -0,0 +1,18 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.require
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList["test/*_test.rb"]
10
+ t.verbose = true
11
+ end
12
+
13
+ begin
14
+ require "mg"
15
+ MG.new("aframe-switch.gemspec")
16
+ rescue LoadError
17
+ "Couldn't load mg tasks"
18
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "aframe-switch"
3
+ s.version = "0.0.1"
4
+ s.date = "2014-10-29"
5
+ s.summary = "Set and retrieve an arbitrary Rack switch."
6
+ s.description = "Set and retrieve an arbitrary switch flag via GET and POST."
7
+ s.homepage = "http://github.com/Allinthedata/switch"
8
+ s.email = "hi@aframe.com"
9
+ s.authors = ["mikesten"]
10
+ s.has_rdoc = false
11
+ s.files = %w[
12
+ README.rdoc
13
+ Rakefile
14
+ aframe-switch.gemspec
15
+ lib/switch.rb
16
+ test/switch_test.rb
17
+ ]
18
+ s.add_dependency("rack", [">= 1.2.0"])
19
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require "rack"
4
+
5
+ class Switch
6
+ ON_MESSAGE = "ON"
7
+ OFF_MESSAGE = "OFF"
8
+
9
+ def initialize app_name, options
10
+ raise RuntimeError unless app_name
11
+ defaults = { :file_location => "/tmp", :url_namespace => "/switch" }
12
+ @options = defaults.merge(options)
13
+ @url_namespace = @options[:url_namespace]
14
+ @app_name = app_name
15
+ @file = File.join(@options[:file_location], "#{@app_name}_switch")
16
+ end
17
+
18
+ def get_switch
19
+ result = unless File.exist? @file
20
+ ON_MESSAGE
21
+ else
22
+ File.open(@file, 'r').read
23
+ end
24
+ end
25
+
26
+ def set_switch message
27
+ File.open(@file, 'w') {|f| f.write(message) }
28
+ end
29
+
30
+ def call(env)
31
+ request = Rack::Request.new env
32
+ if request.get?
33
+ if request.path == "#{@url_namespace}/#{@app_name}"
34
+ case get_switch
35
+ when ON_MESSAGE
36
+ return [200, {}, ["#{@app_name} is #{ON_MESSAGE}"]]
37
+ when OFF_MESSAGE
38
+ return [503, {}, ["#{@app_name} is #{OFF_MESSAGE}"]]
39
+ end
40
+ end
41
+ elsif request.post?
42
+ case request.path
43
+ when "#{@url_namespace}/#{@app_name}/on"
44
+ set_switch ON_MESSAGE
45
+ return [200, {}, ["#{@app_name} set to #{ON_MESSAGE}"]]
46
+ when "#{@url_namespace}/#{@app_name}/off"
47
+ set_switch OFF_MESSAGE
48
+ return [200, {}, ["#{@app_name} set to #{OFF_MESSAGE}"]]
49
+ end
50
+ end
51
+ return [404, {}, "Not found"]
52
+ end
53
+ end
@@ -0,0 +1,82 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.require(:test)
4
+
5
+ require File.dirname(__FILE__) + "/../lib/switch"
6
+
7
+ class SwitchTest < Test::Unit::TestCase
8
+ def state_file
9
+ File.dirname(__FILE__) + "/light_switch"
10
+ end
11
+
12
+ def remove_state_file
13
+ File.delete(state_file) if File.exists?(state_file)
14
+ end
15
+
16
+ def setup
17
+ remove_state_file
18
+ @session = Rack::Test::Session.new(Switch.new("light", { :file_location => File.dirname(__FILE__) }))
19
+ end
20
+
21
+ def teardown
22
+ remove_state_file
23
+ end
24
+
25
+ def test_raises_unless_app_name_is_specified
26
+ assert_raise_kind_of(RuntimeError) do
27
+ Rack::Test::Session.new(Switch.new(nil, { :file_location => File.dirname(__FILE__) }))
28
+ end
29
+ end
30
+
31
+ def test_it_returns_not_found_for_other_urls
32
+ @session.get "/another"
33
+ assert_equal @session.last_response.status, 404
34
+ @session.get "/another/url"
35
+ assert_equal @session.last_response.status, 404
36
+ end
37
+
38
+ def test_it_returns_not_found_for_unknown_switch
39
+ @session.get "/switch/power"
40
+ assert_equal @session.last_response.status, 404
41
+ end
42
+
43
+ def test_it_defaults_to_on
44
+ @session.get "/switch/light"
45
+ assert_equal @session.last_response.status, 200
46
+ assert_include @session.last_response.body, "light is ON"
47
+ end
48
+
49
+ def test_it_respects_passed_url_namespace
50
+ session = Rack::Test::Session.new(Switch.new("door", { :url_namespace => "/latch", :file_location => File.dirname(__FILE__) }))
51
+ session.get "/latch/door"
52
+ assert_equal session.last_response.status, 200
53
+
54
+ session = Rack::Test::Session.new(Switch.new("door", { :url_namespace => "", :file_location => File.dirname(__FILE__) }))
55
+ session.get "/door"
56
+ assert_equal session.last_response.status, 200
57
+ end
58
+
59
+ def test_it_writes_state_file_to_specified_location
60
+ @session.post "/switch/light/off"
61
+ assert_equal File.exists?(state_file), true
62
+ end
63
+
64
+ def test_it_can_be_set_to_off
65
+ @session.post "/switch/light/off"
66
+ assert_equal @session.last_response.status, 200
67
+
68
+ @session.get "/switch/light"
69
+ assert_equal @session.last_response.status, 503
70
+ assert_include @session.last_response.body, "light is OFF"
71
+ end
72
+
73
+ def test_it_can_be_set_to_on
74
+ @session.post "/switch/light/off"
75
+ @session.post "/switch/light/on"
76
+ assert_equal @session.last_response.status, 200
77
+
78
+ @session.get "/switch/light"
79
+ assert_equal @session.last_response.status, 200
80
+ assert_include @session.last_response.body, "light is ON"
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aframe-switch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - mikesten
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2014-10-29 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 31
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 0
31
+ version: 1.2.0
32
+ requirement: *id001
33
+ name: rack
34
+ prerelease: false
35
+ type: :runtime
36
+ description: Set and retrieve an arbitrary switch flag via GET and POST.
37
+ email: hi@aframe.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - README.rdoc
46
+ - Rakefile
47
+ - aframe-switch.gemspec
48
+ - lib/switch.rb
49
+ - test/switch_test.rb
50
+ homepage: http://github.com/Allinthedata/switch
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.24
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Set and retrieve an arbitrary Rack switch.
83
+ test_files: []
84
+