lane_groove 0.0.4 → 0.0.5

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/lane_groove.rb CHANGED
@@ -6,7 +6,7 @@ require 'rack/contrib'
6
6
 
7
7
  class Hash
8
8
  def upcase_keys
9
- self.inject({}){ |new_hash, key_value|
9
+ inject({}){ |new_hash, key_value|
10
10
  key, value = key_value
11
11
  value = value.upcase_keys if value.is_a?(Hash)
12
12
  new_hash[key.upcase] = value
@@ -15,13 +15,26 @@ class Hash
15
15
  end
16
16
 
17
17
  def recursive_values
18
- self.inject([]){ |new_array, key_value|
18
+ inject([]){ |new_array, key_value|
19
19
  key, value = key_value
20
20
  value = value.recursive_values if value.is_a?(Hash)
21
21
  new_array << value
22
22
  new_array.flatten
23
23
  }
24
24
  end
25
+
26
+ def remove_nil_values
27
+ inject({}){ |new_hash, key_value|
28
+ key, value = key_value
29
+ value = value.remove_nil_values if value.is_a?(Hash)
30
+ new_hash[key] = value unless value.nil?
31
+ new_hash
32
+ }
33
+ end
34
+
35
+ def to_xml(root=nil)
36
+ XmlSimple.xml_out(self, 'RootName' => root, 'NoAttr' => true)
37
+ end
25
38
  end
26
39
 
27
40
  class LaneGroove < Sinatra::Base
@@ -35,7 +48,7 @@ class LaneGroove < Sinatra::Base
35
48
  if File.exists? static_dir
36
49
  puts "Serving static files from #{static_dir}"
37
50
  set :static, true
38
- set :public, static_dir
51
+ set :public_folder, static_dir
39
52
  end
40
53
 
41
54
  helpers do
@@ -58,10 +71,8 @@ class LaneGroove < Sinatra::Base
58
71
 
59
72
  config_files.each do |file|
60
73
  puts "Loading #{file}"
61
- self.class.config[File.basename(file, '.yaml').to_sym] = YAML.load_file(file)
74
+ self.class.config[File.basename(file, '.yaml')] = YAML.load_file(file)
62
75
  end
63
-
64
- config.to_yaml
65
76
  end
66
77
 
67
78
  def config(*path)
@@ -77,7 +88,7 @@ class LaneGroove < Sinatra::Base
77
88
  end
78
89
 
79
90
  def parse_path(path)
80
- path.split('/').delete_if{|n| n.empty?}.compact.map{|n| n.to_sym}
91
+ path.split('/').delete_if{|n| n.empty?}.compact.map{|n| n}
81
92
  end
82
93
  def extract_values(path)
83
94
  path.is_a?(Hash) ? path.recursive_values : path
@@ -94,14 +105,14 @@ class LaneGroove < Sinatra::Base
94
105
 
95
106
  get /^(.*)\.([\w\d]{2,4})$/ do |path, ext|
96
107
  ext = ext.to_sym
97
- path = parse_path(path)
108
+ conf = config *parse_path(path)
98
109
 
99
110
  case ext
100
- when :yaml then [200, content_type_for(:yaml), config(*path).to_yaml]
101
- when :json then [200, content_type_for(:json), config(*path).to_json]
102
- when :xml then [200, content_type_for(:xml), XmlSimple.xml_out(config(*path), 'RootName' => nil, 'NoAttr' => true)]
103
- when :XML then [200, content_type_for(:xml), XmlSimple.xml_out(config(*path).upcase_keys, 'RootName' => nil, 'NoAttr' => true)]
104
- when :rb then [200, content_type_for(:rb), config(*path).inspect]
111
+ when :yaml then [200, content_type_for(:yaml), conf.to_yaml]
112
+ when :json then [200, content_type_for(:json), conf.to_json]
113
+ when :xml then [200, content_type_for(:xml), conf.remove_nil_values.to_xml]
114
+ when :XML then [200, content_type_for(:xml), conf.remove_nil_values.upcase_keys.to_xml]
115
+ when :rb then [200, content_type_for(:rb), conf.inspect]
105
116
  else ; [404, {}, "unknown format #{ext}"]
106
117
  end
107
118
  end
@@ -1,6 +1,6 @@
1
1
  WORKING_DIR = File.join(Dir.getwd, 'config_files')
2
2
 
3
- require '../lib/app.rb'
3
+ require '../lib/lane_groove.rb'
4
4
  require 'test/unit'
5
5
  require 'rack/test'
6
6
  require 'fileutils'
@@ -13,7 +13,7 @@ class LaneGrooveTest < Test::Unit::TestCase
13
13
  end
14
14
 
15
15
  def config
16
- {:test=>{:db=>{:host=>"my-db-server", :user=>"root", :pass=>"none"}, :redis=>{:host=>"my-redis-server"}}}
16
+ {"test"=>{"db"=>{"host"=>"my-db-server", "user"=>"root", "pass"=>"none", "foo" => nil, "bar" => 1}, "redis"=>{"host"=>"my-redis-server"}}}
17
17
  end
18
18
 
19
19
  def config_all_xml
@@ -23,6 +23,7 @@ class LaneGrooveTest < Test::Unit::TestCase
23
23
  <host>my-db-server</host>
24
24
  <user>root</user>
25
25
  <pass>none</pass>
26
+ <bar>1</bar>
26
27
  </db>
27
28
  <redis>
28
29
  <host>my-redis-server</host>
@@ -36,6 +37,7 @@ class LaneGrooveTest < Test::Unit::TestCase
36
37
  <host>my-db-server</host>
37
38
  <user>root</user>
38
39
  <pass>none</pass>
40
+ <bar>1</bar>
39
41
  EOX
40
42
  end
41
43
 
@@ -46,6 +48,7 @@ class LaneGrooveTest < Test::Unit::TestCase
46
48
  <HOST>my-db-server</HOST>
47
49
  <USER>root</USER>
48
50
  <PASS>none</PASS>
51
+ <BAR>1</BAR>
49
52
  </DB>
50
53
  <REDIS>
51
54
  <HOST>my-redis-server</HOST>
@@ -59,60 +62,71 @@ class LaneGrooveTest < Test::Unit::TestCase
59
62
  <HOST>my-db-server</HOST>
60
63
  <USER>root</USER>
61
64
  <PASS>none</PASS>
65
+ <BAR>1</BAR>
62
66
  EOX
63
67
  end
64
68
 
65
69
  def test_all_rb
66
70
  get '/.rb'
71
+ assert_equal 200, last_response.status
67
72
  assert_equal 'application/x-ruby; charset=utf-8', last_response.content_type
68
73
  assert_equal config.inspect, last_response.body
69
74
  end
70
75
  def test_db_rb
71
76
  get '/test/db.rb'
77
+ assert_equal 200, last_response.status
72
78
  assert_equal 'application/x-ruby; charset=utf-8', last_response.content_type
73
- assert_equal config[:test][:db].inspect, last_response.body
79
+ assert_equal config["test"]["db"].inspect, last_response.body
74
80
  end
75
81
 
76
82
  def test_all_yaml
77
83
  get '/.yaml'
84
+ assert_equal 200, last_response.status
78
85
  assert_equal 'application/x-yaml; charset=utf-8', last_response.content_type
79
86
  assert_equal config.to_yaml, last_response.body
80
87
  end
81
88
  def test_db_yaml
82
89
  get '/test/db.yaml'
90
+ assert_equal 200, last_response.status
83
91
  assert_equal 'application/x-yaml; charset=utf-8', last_response.content_type
84
- assert_equal config[:test][:db].to_yaml, last_response.body
92
+ assert_equal config["test"]["db"].to_yaml, last_response.body
85
93
  end
86
94
 
87
95
  def test_all_json
88
96
  get '/.json'
97
+ assert_equal 200, last_response.status
89
98
  assert_equal 'application/json; charset=utf-8', last_response.content_type
90
99
  assert_equal config.to_json, last_response.body
91
100
  end
92
101
  def test_db_json
93
102
  get '/test/db.json'
103
+ assert_equal 200, last_response.status
94
104
  assert_equal 'application/json; charset=utf-8', last_response.content_type
95
- assert_equal config[:test][:db].to_json, last_response.body
105
+ assert_equal config['test']["db"].to_json, last_response.body
96
106
  end
97
107
 
98
108
  def test_all_xml
99
109
  get '/.xml'
110
+ assert_equal 200, last_response.status
100
111
  assert_equal 'application/xml; charset=utf-8', last_response.content_type
101
112
  assert_equal config_all_xml, last_response.body
102
113
  end
103
114
  def test_db_xml
104
115
  get '/test/db.xml'
116
+ assert_equal 200, last_response.status
105
117
  assert_equal 'application/xml; charset=utf-8', last_response.content_type
106
118
  assert_equal config_db_xml, last_response.body
107
119
  end
108
120
 
109
121
  def test_all_XML
110
122
  get '/.XML'
123
+ assert_equal 200, last_response.status
111
124
  assert_equal 'application/xml; charset=utf-8', last_response.content_type
112
125
  assert_equal config_all_XML, last_response.body
113
126
  end
114
127
  def test_db_XML
115
128
  get '/test/db.XML'
129
+ assert_equal 200, last_response.status
116
130
  assert_equal 'application/xml; charset=utf-8', last_response.content_type
117
131
  assert_equal config_db_XML, last_response.body
118
132
  end
@@ -132,15 +146,15 @@ class LaneGrooveTest < Test::Unit::TestCase
132
146
 
133
147
  def test_reloading
134
148
  get '/test/db.rb'
135
- assert_equal config[:test][:db].inspect, last_response.body
149
+ assert_equal config["test"]["db"].inspect, last_response.body
136
150
 
137
151
  FileUtils.mv('config_files/test.yaml', 'config_files/more_test.yaml')
138
152
 
139
153
  get '/test/db.rb'
140
- assert_equal config[:test][:db].inspect, last_response.body
154
+ assert_equal config["test"]["db"].inspect, last_response.body
141
155
 
142
156
  get '/more_test/db.rb?reload=true'
143
- assert_equal config[:test][:db].inspect, last_response.body
157
+ assert_equal config["test"]["db"].inspect, last_response.body
144
158
  end
145
159
 
146
160
  def test_network_restrictions
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lane_groove
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Niko
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-11-04 00:00:00 Z
14
+ date: 2011-11-05 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: xml-simple