Class: SkyDB::Event
- Inherits:
-
Object
- Object
- SkyDB::Event
- Defined in:
- lib/skydb/event.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
The data associated with the event.
-
#timestamp ⇒ Object
The moment the event occurred.
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
-
#formatted_timestamp ⇒ Object
The timestamp as ISO8601 formatted string with fractional seconds.
-
#from_hash(hash, *a) ⇒ Object
Decodes a hash into a event.
-
#initialize(options = {}) ⇒ Event
constructor
Initializes the event.
-
#parse_timestamp(str) ⇒ DateTime
Parses an ISO8601 date string with fractional seconds.
-
#to_hash(*a) ⇒ Object
Encodes the event into a hash.
- #to_json(*a) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Event
Initializes the event.
10 11 12 13 |
# File 'lib/skydb/event.rb', line 10 def initialize(={}) self. = [:timestamp] || DateTime.now self.data = [:data] || {} end |
Instance Attribute Details
#data ⇒ Object
The data associated with the event.
26 27 28 |
# File 'lib/skydb/event.rb', line 26 def data @data end |
#timestamp ⇒ Object
The moment the event occurred.
23 24 25 |
# File 'lib/skydb/event.rb', line 23 def @timestamp end |
Instance Method Details
#as_json(*a) ⇒ Object
59 |
# File 'lib/skydb/event.rb', line 59 def as_json(*a); return to_hash(*a); end |
#formatted_timestamp ⇒ Object
The timestamp as ISO8601 formatted string with fractional seconds.
29 30 31 |
# File 'lib/skydb/event.rb', line 29 def () return SkyDB.() end |
#from_hash(hash, *a) ⇒ Object
Decodes a hash into a event.
53 54 55 56 57 |
# File 'lib/skydb/event.rb', line 53 def from_hash(hash, *a) self. = !hash.nil? ? (hash['timestamp']) : Time.now.utc.to_datetime() self.data = !hash.nil? ? hash['data'] : {} return self end |
#parse_timestamp(str) ⇒ DateTime
Parses an ISO8601 date string with fractional seconds.
71 72 73 74 75 76 77 78 79 |
# File 'lib/skydb/event.rb', line 71 def (str) # Try to parse with fractional seconds first and then fallback to # a regular ISO8601 format. begin return DateTime.strptime(str, '%Y-%m-%dT%H:%M:%S.%NZ') rescue ArgumentError return DateTime.strptime(str, '%Y-%m-%dT%H:%M:%SZ') end end |
#to_hash(*a) ⇒ Object
Encodes the event into a hash.
45 46 47 48 49 50 |
# File 'lib/skydb/event.rb', line 45 def to_hash(*a) { 'timestamp' => , 'data' => data } end |
#to_json(*a) ⇒ Object
60 |
# File 'lib/skydb/event.rb', line 60 def to_json(*a); return as_json(*a).to_json; end |