Class: Vayacondios::Server::Event
- Defined in:
- lib/vayacondios/server/models/event.rb
Constant Summary collapse
- LIMIT =
The default number of events returned when searching.
50
- ORDER =
The default sort order when searching
'desc'
- SORT =
The default sort field when searching.
'time'
- WINDOW =
The default time window (measured relative to the current time) when searching.
3600
Constants inherited from Document
Class Method Summary collapse
Instance Method Summary collapse
-
#document ⇒ Object
An events internal database representation.
-
#event_filter(query) ⇒ Hash
Returns a Hash that can be used for selection criteria in a query to a MongoDB collection.
-
#external_document ⇒ Object
An event as presented to a user.
- #format_time(ts) ⇒ Object
-
#from_document(doc) ⇒ Object
Populate a new Event from a database representation.
-
#location ⇒ String
The name of the collection this event will store its data in.
-
#prepare_create(document) ⇒ Object
Prepare Event create request.
-
#prepare_destroy(query) ⇒ Object
Prepare Event remove request.
-
#prepare_find ⇒ Object
Prepare Event retrieve request.
-
#prepare_search(query) ⇒ Object
Prepare Event search request.
-
#receive_time(t) ⇒ Object
Normalizes the time.
-
#receive_topic(name) ⇒ String
Set the topic of this document, sanitizing it for the database.
-
#to_timestamp(obj, default = Time.now) ⇒ Time
Parses an object into a timestamp.
Methods inherited from Document
create, destroy, find, #format_response, #receive_organization, #sanitize_location_name, search
Class Method Details
.default_query_options ⇒ Object
67 68 69 |
# File 'lib/vayacondios/server/models/event.rb', line 67 def self. { limit: LIMIT, order: ORDER, sort: SORT } end |
.extract_query_options!(opts) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vayacondios/server/models/event.rb', line 71 def self. opts query = super if query[:sort] == ['time'] query[:sort] = ['_t'] elsif query[:sort].present? query[:sort].unshift '_d' end query[:fields].each{|field| field.replace(['_t']) if field == ['time'] field.unshift('_d') unless %w[_id _t].include?(field.first) } if query[:fields].present? query end |
Instance Method Details
#document ⇒ Object
An events internal database representation
130 131 132 |
# File 'lib/vayacondios/server/models/event.rb', line 130 def document { _id: id, _t: time, _d: body }.compact end |
#event_filter(query) ⇒ Hash
Returns a Hash that can be used for selection criteria in a query to a MongoDB collection.
For any given ‘query` object, this method should be run after Event.projector because Event.projector modifies the `query` by removing certain special options which would otherwise be interpreted by this method..
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/vayacondios/server/models/event.rb', line 166 def event_filter query filter = { _t: {} }.tap do |filter| if query.has_key? :after filter[:_t][:gt] = query.delete(:after) query.delete(:from) elsif query.has_key? :from filter[:_t][:gte] = query.delete(:from) end if query.has_key? :before filter[:_t][:lt] = query.delete(:before) query.delete(:upto) elsif query.has_key? :upto filter[:_t][:lte] = query.delete(:upto) end # sel['_id'] = Regexp.new(query.delete(:id)) if query[:id] query.each_pair{ |key, val| filter[:_d] ||= {} ; filter[:_d][key] = val } end end |
#external_document ⇒ Object
An event as presented to a user
146 147 148 |
# File 'lib/vayacondios/server/models/event.rb', line 146 def external_document { id: id, time: (time ? time.iso8601(3) : nil) }.merge(body).compact end |
#format_time(ts) ⇒ Object
118 119 120 |
# File 'lib/vayacondios/server/models/event.rb', line 118 def format_time ts ts.round(3).utc end |
#from_document(doc) ⇒ Object
Populate a new Event from a database representation
135 136 137 138 139 140 141 142 143 |
# File 'lib/vayacondios/server/models/event.rb', line 135 def from_document doc d = {}.tap do |d| d[:id] = doc[:_id] d[:time] = doc[:_t] d[:body] = doc[:_d] end.compact receive! d self end |
#location ⇒ String
The name of the collection this event will store its data in.
125 126 127 |
# File 'lib/vayacondios/server/models/event.rb', line 125 def location [organization, topic, 'events'].join('.') end |
#prepare_create(document) ⇒ Object
Prepare Event create request
199 200 201 202 203 204 |
# File 'lib/vayacondios/server/models/event.rb', line 199 def prepare_create document raise Error.new('Events must be Hash-like to create') unless document.is_a?(Hash) document.symbolize_keys! receive!(time: document.delete(:time), body: document) self end |
#prepare_destroy(query) ⇒ Object
Prepare Event remove request
207 208 209 210 |
# File 'lib/vayacondios/server/models/event.rb', line 207 def prepare_destroy query receive!(filter: event_filter(query)) self end |
#prepare_find ⇒ Object
Prepare Event retrieve request
193 194 195 196 |
# File 'lib/vayacondios/server/models/event.rb', line 193 def prepare_find raise Error.new('Cannot find an event without an ID') if id.blank? self end |
#prepare_search(query) ⇒ Object
Prepare Event search request
187 188 189 190 |
# File 'lib/vayacondios/server/models/event.rb', line 187 def prepare_search query receive!(filter: event_filter(query)) self end |
#receive_time(t) ⇒ Object
Normalizes the time
97 98 99 |
# File 'lib/vayacondios/server/models/event.rb', line 97 def receive_time t @time = format_time (t) end |
#receive_topic(name) ⇒ String
Set the topic of this document, sanitizing it for the database
92 93 94 |
# File 'lib/vayacondios/server/models/event.rb', line 92 def receive_topic name @topic = sanitize_location_name name end |
#to_timestamp(obj, default = Time.now) ⇒ Time
Parses an object into a timestamp.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vayacondios/server/models/event.rb', line 106 def (obj, default = Time.now) case obj when String then Time.parse(obj) when Date then obj.to_time when Time then obj when Numeric then Time.at(obj) else default end rescue ArgumentError => e default end |