sinatra-switch 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,11 +4,13 @@ require 'sinatra/base'
4
4
 
5
5
  module Sinatra
6
6
  module Switch
7
- def use_switch app_name
8
- file = "/tmp/#{app_name}"
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sinatra-switch"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.date = "2014-10-28"
5
5
  s.summary = "Set and retrieve an arbitrary switch."
6
6
  s.description = "Set and retrieve an arbitrary switch flag via get and post."
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 SomeApp < Sinatra::Base
8
+ class AppWithLightSwitch < Sinatra::Base
9
9
  register Sinatra::Switch
10
- use_switch "some_app"
10
+ use_switch "light", :file_location => File.dirname(__FILE__)
11
11
  end
12
12
 
13
13
  class SinatraSwitchTest < Test::Unit::TestCase
14
- def cleanup
15
- File.delete("/tmp/some_app") if File.exists?("/tmp/some_app")
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
- cleanup
20
- @session = Rack::Test::Session.new(SomeApp)
23
+ remove_state_file
24
+ @session = Rack::Test::Session.new(AppWithLightSwitch)
21
25
  end
22
26
 
23
27
  def teardown
24
- cleanup
28
+ remove_state_file
25
29
  end
26
30
 
27
31
  def test_it_defaults_to_on
28
- @session.get "/switch/some_app"
32
+ @session.get "/switch/light"
29
33
  assert_equal @session.last_response.status, 200
30
- assert_include @session.last_response.body, "some_app is ON"
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/some_app/off"
35
- @session.get "/switch/some_app"
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, "some_app is OFF"
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/some_app/off"
42
- @session.post "/switch/some_app/on"
43
- @session.get "/switch/some_app"
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, "some_app is ON"
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.2
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: -3102425874624580051
55
+ hash: 4352394634967517741
56
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements: