Ravn::Tactical::ServiceV1::

Changes module

:nocov:

Public Class Methods

included( app )

Add changes-related routes to the given app.

   # File lib/ravn/tactical/service_v1/changes.rb
20 def self::included( app )
21     super
22 
23     self.log.info "Mounting change endpoints."
24     self.mount( app )
25 end
mount( app )

Mount node endpoints in the given app.

   # File lib/ravn/tactical/service_v1/changes.rb
29 def self::mount( app )
30 
31     app.hash_branch( :v1, 'changes' ) do |r|
32 
33         # GET /api/v1/changes
34         r.is do
35             r.get( &self.method(:get_changes) )
36         end
37 
38         # GET /api/v1/changes/next_by_node
39         r.on( 'next_by_node' ) do
40             r.get { self.get_next_changes }
41         end
42 
43         # GET /api/v1/changes/«id»
44         r.on( Integer ) do |id|
45             r.get { self.get_change( id ) }
46         end
47 
48     end
49 
50 end

Public Instance Methods

get_change( id )

Get the change by id

   # File lib/ravn/tactical/service_v1/changes.rb
58 def get_change( id )
59     self.log.info "Fetching change #%d" % [ id ]
60 
61     change = Ravn::Tactical::Change[ id ] or return nil
62 
63     return present( change )
64 end
get_changes()

Get all changes

   # File lib/ravn/tactical/service_v1/changes.rb
68 def get_changes
69     dataset = Ravn::Tactical::Change
70 
71     if (device_id = typecast_params.device_id( 'node' ))
72         if (node = Ravn::Tactical::Node.by_device_id( device_id ).first)
73             self.log.info "Fetching all known changes for node %s" % [ device_id ]
74             dataset = dataset.for_node( node )
75         else
76             self.log.warn "Returning empty dataset for non-existent node %s" % [ device_id ]
77             dataset = Ravn::Tactical::Change.where( nil )
78         end
79     else
80         self.log.info "Fetching all known changes"
81     end
82 
83     changes = dataset.all
84 
85     return present_collection( changes )
86 end
get_next_changes()

Get the next changes that will be run, one per node.

   # File lib/ravn/tactical/service_v1/changes.rb
90 def get_next_changes
91     changes = Ravn::Tactical::Change.earliest_for_each_node.eager( :node )
92 
93     present_collection( changes, with_device_ids: true )
94 end