sinatra-switch 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +34 -0
- data/Rakefile +17 -0
- data/lib/sinatra/switch.rb +36 -0
- data/sinatra-switch.gemspec +19 -0
- data/test/switch_test.rb +47 -0
- metadata +68 -0
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
= Sinatra Switch
|
2
|
+
|
3
|
+
Set and retrieve an arbitrary switch flag via get and post.
|
4
|
+
|
5
|
+
== Use
|
6
|
+
|
7
|
+
require "sinatra/switch"
|
8
|
+
|
9
|
+
use_switch "light"
|
10
|
+
|
11
|
+
== Functionality
|
12
|
+
|
13
|
+
This will expose:
|
14
|
+
|
15
|
+
A route to get the current state of the switch:
|
16
|
+
/switch/light
|
17
|
+
|
18
|
+
A route for flicking the switch to ON:
|
19
|
+
/switch/light/on
|
20
|
+
|
21
|
+
A route for flicking the switch to OFF:
|
22
|
+
/switch/light/off
|
23
|
+
|
24
|
+
The switch state is stored in a file:
|
25
|
+
/var/light_switch
|
26
|
+
|
27
|
+
== Switch state possibilities
|
28
|
+
|
29
|
+
File doesn't exist
|
30
|
+
switch is on
|
31
|
+
File contains ON
|
32
|
+
switch is on
|
33
|
+
File contains OFF
|
34
|
+
server is off
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
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("sinatra-switch.gemspec")
|
16
|
+
rescue LoadError
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'sinatra/base'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module Switch
|
7
|
+
def use_switch app_name
|
8
|
+
file = "/tmp/#{app_name}"
|
9
|
+
on_message = "ON"
|
10
|
+
off_message = "OFF"
|
11
|
+
|
12
|
+
get "/switch/#{app_name}" do
|
13
|
+
result = unless File.exist? file
|
14
|
+
on_message
|
15
|
+
else
|
16
|
+
File.open(file, 'r').read
|
17
|
+
end
|
18
|
+
|
19
|
+
if result.include? off_message
|
20
|
+
status 400
|
21
|
+
else
|
22
|
+
status 200
|
23
|
+
end
|
24
|
+
"Switch says #{app_name} is #{result}..."
|
25
|
+
end
|
26
|
+
|
27
|
+
post "/switch/#{app_name}/on" do
|
28
|
+
File.open(file, 'w') {|f| f.write(on_message) }
|
29
|
+
end
|
30
|
+
|
31
|
+
post "/switch/#{app_name}/off" do
|
32
|
+
File.open(file, 'w') {|f| f.write(off_message) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sinatra-switch"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.date = "2014-10-28"
|
5
|
+
s.summary = "Set and retrieve an arbitrary switch."
|
6
|
+
s.description = "Set and retrieve an arbitrary switch flag via get and post."
|
7
|
+
s.homepage = "http://github.com/Allinthedata/sinatra-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
|
+
sinatra-switch.gemspec
|
15
|
+
lib/sinatra/switch.rb
|
16
|
+
test/switch_test.rb
|
17
|
+
]
|
18
|
+
s.add_dependency("sinatra", [">= 1.4.0"])
|
19
|
+
end
|
data/test/switch_test.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler"
|
3
|
+
Bundler.require(:test)
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + "/../lib/sinatra/switch"
|
6
|
+
|
7
|
+
require "sinatra/base"
|
8
|
+
class SomeApp < Sinatra::Base
|
9
|
+
register Sinatra::Switch
|
10
|
+
use_switch "some_app"
|
11
|
+
end
|
12
|
+
|
13
|
+
class SinatraSwitchTest < Test::Unit::TestCase
|
14
|
+
def cleanup
|
15
|
+
File.delete("/tmp/some_app") if File.exists?("/tmp/some_app")
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup
|
19
|
+
cleanup
|
20
|
+
@session = Rack::Test::Session.new(SomeApp)
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
cleanup
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_it_defaults_to_on
|
28
|
+
@session.get "/switch/some_app"
|
29
|
+
assert_equal @session.last_response.status, 200
|
30
|
+
assert_include @session.last_response.body, "some_app is ON"
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_it_can_be_set_to_off
|
34
|
+
@session.post "/switch/some_app/off"
|
35
|
+
@session.get "/switch/some_app"
|
36
|
+
assert_equal @session.last_response.status, 400
|
37
|
+
assert_include @session.last_response.body, "some_app is OFF"
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_it_can_be_set_to_on
|
41
|
+
@session.post "/switch/some_app/off"
|
42
|
+
@session.post "/switch/some_app/on"
|
43
|
+
@session.get "/switch/some_app"
|
44
|
+
assert_equal @session.last_response.status, 200
|
45
|
+
assert_include @session.last_response.body, "some_app is ON"
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-switch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- mikesten
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-10-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.4.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.4.0
|
30
|
+
description: Set and retrieve an arbitrary switch flag via get and post.
|
31
|
+
email: hi@aframe.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- sinatra-switch.gemspec
|
39
|
+
- lib/sinatra/switch.rb
|
40
|
+
- test/switch_test.rb
|
41
|
+
homepage: http://github.com/Allinthedata/sinatra-switch
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: 3191485038812799727
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.25
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Set and retrieve an arbitrary switch.
|
68
|
+
test_files: []
|