rumx 0.0.7 → 0.0.8

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.
Files changed (3) hide show
  1. data/History.md +4 -0
  2. data/lib/rumx/server.rb +75 -33
  3. metadata +2 -2
data/History.md CHANGED
@@ -1,6 +1,10 @@
1
1
  Rumx Changelog
2
2
  =====================
3
3
 
4
+ 0.0.8
5
+
6
+ - Added /attributes query which can retrieve attributes for multiple beans in one call.
7
+
4
8
  0.0.7
5
9
 
6
10
  - Add .properties format for brain-dead hyperic. Might as well allow extending while we're at it.
data/lib/rumx/server.rb CHANGED
@@ -92,38 +92,6 @@ module Rumx
92
92
  def attribute_value_tag(attribute, param_name, value)
93
93
  partial :attribute_value_tag, :locals => {:attribute => attribute, :param_name => param_name, :value => value}
94
94
  end
95
-
96
- # Brain-dead hyperic doesn't know about json?
97
- def to_properties(val, prefix=nil)
98
- str = ''
99
- new_prefix = (prefix + '.') if prefix
100
- new_prefix = new_prefix || ''
101
- if val.kind_of?(Hash)
102
- val.each do |key, value|
103
- str += to_properties(value, new_prefix + key.to_s)
104
- end
105
- elsif val.kind_of?(Array)
106
- val.each_with_index do |value, i|
107
- str += to_properties(value, new_prefix + i.to_s)
108
- end
109
- else
110
- str += "#{prefix}=#{val}\n"
111
- end
112
- return str
113
- end
114
- end
115
-
116
- def handle_attributes(attribute_hash, format)
117
- case format
118
- when 'json'
119
- content_type :json
120
- attribute_hash.to_json
121
- when 'properties'
122
- content_type :properties
123
- to_properties(attribute_hash)
124
- else
125
- 404
126
- end
127
95
  end
128
96
 
129
97
  get '/' do
@@ -146,7 +114,6 @@ module Rumx
146
114
  path = params[:splat][0]
147
115
  bean = Bean.find(path.split('/'))
148
116
  return 404 unless bean
149
- #puts "params=#{params.inspect}"
150
117
  # For post we write, then read. get is the other way around.
151
118
  if params[:format]
152
119
  handle_attributes(bean.bean_set_and_get_attributes(params), params[:format])
@@ -155,6 +122,26 @@ module Rumx
155
122
  end
156
123
  end
157
124
 
125
+ # Allow a monitor to get the attributes from multiple beans.
126
+ # Use with params such as prefix_0=bean0&bean_0=MyFolder/MyBean&prefix_1=bean1&bean_1=MyOtherFolder/SomeOtherBean
127
+ get '/attributes.?:format?' do
128
+ hash = {}
129
+ with_indexed_params(params) do |prefix, bean, new_params|
130
+ return 404 unless bean
131
+ hash[prefix.to_sym] = bean.bean_get_and_set_attributes(new_params)
132
+ end
133
+ handle_attributes(hash, params[:format])
134
+ end
135
+
136
+ post '/attributes.?:format?' do
137
+ hash = {}
138
+ with_indexed_params(params) do |prefix, bean, new_params|
139
+ return 404 unless bean
140
+ hash[prefix.to_sym] = bean.bean_set_and_get_attributes(new_params)
141
+ end
142
+ handle_attributes(hash, params[:format])
143
+ end
144
+
158
145
  get '/*/attribute.?:format?' do
159
146
  path = params[:splat][0]
160
147
  bean, attribute, param_name, value = Bean.find_attribute(path.split('/'))
@@ -202,5 +189,60 @@ module Rumx
202
189
  operation.run(bean, params).to_json
203
190
  end
204
191
 
192
+ #######
193
+ protected
194
+ #######
195
+
196
+ def handle_attributes(attribute_hash, format)
197
+ case format
198
+ when 'json'
199
+ content_type :json
200
+ attribute_hash.to_json
201
+ when 'properties'
202
+ content_type :properties
203
+ to_properties(attribute_hash)
204
+ else
205
+ 404
206
+ end
207
+ end
208
+
209
+ #######
210
+ private
211
+ #######
212
+
213
+ def to_properties(val, prefix=nil)
214
+ str = ''
215
+ new_prefix = (prefix + '.') if prefix
216
+ new_prefix = new_prefix || ''
217
+ if val.kind_of?(Hash)
218
+ val.each do |key, value|
219
+ str += to_properties(value, new_prefix + key.to_s)
220
+ end
221
+ elsif val.kind_of?(Array)
222
+ val.each_with_index do |value, i|
223
+ str += to_properties(value, new_prefix + i.to_s)
224
+ end
225
+ else
226
+ str += "#{prefix}=#{val}\n"
227
+ end
228
+ return str
229
+ end
230
+
231
+ def with_indexed_params(params)
232
+ i = 0
233
+ while (prefix = params[prefix_key = "prefix_#{i}"]) && (bean_name = params[bean_key = "bean_#{i}"])
234
+ new_params = {}
235
+ postfix = "_#{i}"
236
+ bean = Bean.find(bean_name.split('/'))
237
+ params.each do |key, value|
238
+ if key.end_with?(postfix) && key != prefix_key && key != bean_key
239
+ new_params[key[0...-(postfix.size)]] = value
240
+ end
241
+ end
242
+ yield prefix, bean, new_params
243
+ i += 1
244
+ end
245
+ end
246
+
205
247
  end
206
248
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rumx
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brad Pardee
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-12-14 00:00:00 Z
13
+ date: 2011-12-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra