Class: CPEE::NewInstance
- Inherits:
-
Riddl::Implementation
- Object
- Riddl::Implementation
- CPEE::NewInstance
- Defined in:
- lib/cpee/implementation.rb
Overview
}}}
Class Method Summary collapse
- .create(opts, redis, name, subs, doc = nil) ⇒ Object
-
.path(e) ⇒ Object
{{{.
- .sub(multi, id, doc, key) ⇒ Object
Instance Method Summary collapse
Class Method Details
.create(opts, redis, name, subs, doc = nil) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/cpee/implementation.rb', line 370 def self::create(opts,redis,name,subs,doc=nil) doc = XML::Smart::open_unprotected(opts[:properties_init]) if doc.nil? doc.register_namespace 'p', 'http://cpee.org/ns/properties/2.0' id = CPEE::Persistence::new_object(opts) uuid = SecureRandom.uuid instance = CPEE::Persistence::obj + ':' + id.to_s redis.multi do |multi| multi.zadd('instances',id,id) doc.root.find(PROPERTIES_PATHS_FULL.join(' | ')).each do |e| p = NewInstance::path(e) if e.class == XML::Smart::Dom::Element && e.element_only? val = e.find('*').map { |f| f.dump }.join multi.set(File.join(instance, p), val) else multi.set(File.join(instance, p), e.text) end end doc.root.find(PROPERTIES_PATHS_INDEX_UNORDERED.join(' | ')).each do |e| p = NewInstance::path(e) multi.sadd(File.join(instance, File.dirname(p)), File.basename(p)) end doc.root.find(PROPERTIES_PATHS_INDEX_ORDERED.join(' | ')).each_with_index do |e,i| p = NewInstance::path(e) multi.zadd(File.join(instance, File.dirname(p)), i, File.basename(p)) end Dir[File.join(opts[:notifications_init],'*','subscription.xml')].each do |f| XML::Smart::open_unprotected(f) do |doc| NewInstance::sub(multi,id,doc,File.basename(File.dirname(f))) end rescue nil # all the ones that are not ok, are ignored end subs.each do |s| begin NewInstance::sub(multi,id,s.to_doc,s.attributes['id'] || Digest::MD5.hexdigest(Kernel::rand().to_s)) end end multi.set(File.join(instance, 'attributes', 'uuid'), uuid) multi.zadd(File.join(instance, 'attributes'), -2, 'uuid') multi.set(File.join(instance, 'attributes', 'info'), name) multi.zadd(File.join(instance, 'attributes'), -1, 'info') multi.set(File.join(instance, 'state', '@changed'), Time.now.xmlschema(3)) end [id, uuid] end |
.path(e) ⇒ Object
{{{
349 350 351 352 353 354 355 356 |
# File 'lib/cpee/implementation.rb', line 349 def self::path(e) ret = [] until e.parent.is_a? XML::Smart::Dom ret << (e.class == XML::Smart::Dom::Attribute ? '@' : '') + e.qname.name e = e.parent end File.join(*ret.reverse) end |
.sub(multi, id, doc, key) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/cpee/implementation.rb', line 358 def self::sub(multi,id,doc,key) doc.register_namespace 'np', 'http://riddl.org/ns/common-patterns/notifications-producer/2.0' url = doc.find('string(/np:subscription/@url)') multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers",key) multi.set(CPEE::Persistence::obj + ":#{id}/handlers/#{key}/url",url) doc.find('/np:subscription/np:topic/*').each do |e| c = File.join(e.parent.attributes['id'],e.qname.name,e.text) multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers/#{key}",c) multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers/#{c}",key) end end |
Instance Method Details
#response ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/cpee/implementation.rb', line 416 def response opts = @a[0] redis = opts[:redis] name = @p[0].value id, uuid = NewInstance::create(opts,redis,name,[]) content = { :state => 'ready', :attributes => CPEE::Persistence::extract_list(id,opts,'attributes').to_h } CPEE::Message::send(:event,'state/change',File.join(opts[:url],'/'),id,uuid,name,content,redis) @headers << Riddl::Header.new("CPEE-INSTANCE", id.to_s) @headers << Riddl::Header.new("CPEE-INSTANCE-URL", File.join(opts[:url].to_s,id.to_s,'/')) @headers << Riddl::Header.new("CPEE-INSTANCE-UUID", uuid) Riddl::Parameter::Simple.new("id", id.to_s) end |