sinatra-switch 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sinatra/switch.rb +5 -3
- data/sinatra-switch.gemspec +1 -1
- data/test/switch_test.rb +25 -16
- metadata +2 -2
data/lib/sinatra/switch.rb
CHANGED
@@ -4,11 +4,13 @@ require 'sinatra/base'
|
|
4
4
|
|
5
5
|
module Sinatra
|
6
6
|
module Switch
|
7
|
-
def use_switch app_name
|
8
|
-
|
7
|
+
def use_switch app_name, options = {}
|
8
|
+
defaults = { :file_location => "/tmp" }
|
9
|
+
options = defaults.merge(options)
|
10
|
+
file = File.join(options[:file_location], app_name)
|
9
11
|
on_message = "ON"
|
10
12
|
off_message = "OFF"
|
11
|
-
|
13
|
+
|
12
14
|
get "/switch/#{app_name}" do
|
13
15
|
result = unless File.exist? file
|
14
16
|
on_message
|
data/sinatra-switch.gemspec
CHANGED
data/test/switch_test.rb
CHANGED
@@ -5,43 +5,52 @@ Bundler.require(:test)
|
|
5
5
|
require File.dirname(__FILE__) + "/../lib/sinatra/switch"
|
6
6
|
|
7
7
|
require "sinatra/base"
|
8
|
-
class
|
8
|
+
class AppWithLightSwitch < Sinatra::Base
|
9
9
|
register Sinatra::Switch
|
10
|
-
use_switch "
|
10
|
+
use_switch "light", :file_location => File.dirname(__FILE__)
|
11
11
|
end
|
12
12
|
|
13
13
|
class SinatraSwitchTest < Test::Unit::TestCase
|
14
|
-
def
|
15
|
-
File.
|
14
|
+
def state_file
|
15
|
+
File.dirname(__FILE__) + "/light"
|
16
|
+
end
|
17
|
+
|
18
|
+
def remove_state_file
|
19
|
+
File.delete(state_file) if File.exists?(state_file)
|
16
20
|
end
|
17
21
|
|
18
22
|
def setup
|
19
|
-
|
20
|
-
@session = Rack::Test::Session.new(
|
23
|
+
remove_state_file
|
24
|
+
@session = Rack::Test::Session.new(AppWithLightSwitch)
|
21
25
|
end
|
22
26
|
|
23
27
|
def teardown
|
24
|
-
|
28
|
+
remove_state_file
|
25
29
|
end
|
26
30
|
|
27
31
|
def test_it_defaults_to_on
|
28
|
-
@session.get "/switch/
|
32
|
+
@session.get "/switch/light"
|
29
33
|
assert_equal @session.last_response.status, 200
|
30
|
-
assert_include @session.last_response.body, "
|
34
|
+
assert_include @session.last_response.body, "light is ON"
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_it_writes_state_file_to_specified_location
|
38
|
+
@session.post "/switch/light/off"
|
39
|
+
assert_equal File.exists?(state_file), true
|
31
40
|
end
|
32
41
|
|
33
42
|
def test_it_can_be_set_to_off
|
34
|
-
@session.post "/switch/
|
35
|
-
@session.get "/switch/
|
43
|
+
@session.post "/switch/light/off"
|
44
|
+
@session.get "/switch/light"
|
36
45
|
assert_equal @session.last_response.status, 400
|
37
|
-
assert_include @session.last_response.body, "
|
46
|
+
assert_include @session.last_response.body, "light is OFF"
|
38
47
|
end
|
39
48
|
|
40
49
|
def test_it_can_be_set_to_on
|
41
|
-
@session.post "/switch/
|
42
|
-
@session.post "/switch/
|
43
|
-
@session.get "/switch/
|
50
|
+
@session.post "/switch/light/off"
|
51
|
+
@session.post "/switch/light/on"
|
52
|
+
@session.get "/switch/light"
|
44
53
|
assert_equal @session.last_response.status, 200
|
45
|
-
assert_include @session.last_response.body, "
|
54
|
+
assert_include @session.last_response.body, "light is ON"
|
46
55
|
end
|
47
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-switch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -52,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
52
|
version: '0'
|
53
53
|
segments:
|
54
54
|
- 0
|
55
|
-
hash:
|
55
|
+
hash: 4352394634967517741
|
56
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|