Class: Vayacondios::Server::Event

Inherits:
Document
  • Object
show all
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

Document::Error

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

create, destroy, find, #format_response, #receive_organization, #sanitize_location_name, search

Class Method Details

.default_query_optionsObject



67
68
69
# File 'lib/vayacondios/server/models/event.rb', line 67

def self.default_query_options
  { 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.extract_query_options! 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

#documentObject

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..

Parameters:

  • query (Hash)

Options Hash (query):

  • from (String, Numeric, Time, nil)

    the earliest time for a matched event (>=)

  • upto (String, Numeric, Time, nil)

    the latest time for a matched event (<=)

  • after (String, Numeric, Time, nil)

    the earliest time for a matched event (>)

  • before (String, Numeric, Time, nil)

    the latest time for a matched event (<)

  • ] (String, Regexp)

    id a regular expression that matches the ID of the event

Returns:

  • (Hash)

    the selector Hash

See Also:

  • Event.projector


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]  = to_timestamp query.delete(:after)
      query.delete(:from)
    elsif query.has_key? :from
      filter[:_t][:gte] = to_timestamp query.delete(:from)
    end

    if query.has_key? :before
      filter[:_t][:lt]  = to_timestamp query.delete(:before)
      query.delete(:upto)
    elsif query.has_key? :upto
      filter[:_t][:lte] = to_timestamp 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_documentObject

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

#locationString

The name of the collection this event will store its data in.

Returns:

  • (String)


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

Raises:



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_findObject

Prepare Event retrieve request

Raises:



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 to_timestamp(t)
end

#receive_topic(name) ⇒ String

Set the topic of this document, sanitizing it for the database

Parameters:

  • name (String)

Returns:

  • (String)

    the sanitized ‘name`



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.

Parameters:

  • obj (String, Numeric, Time, nil)
  • default (Time) (defaults to: Time.now)

    the time value to return if none could be found in the ‘obj`

Returns:

  • (Time)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vayacondios/server/models/event.rb', line 106

def to_timestamp(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