gpxkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE +21 -0
- data/README.md +95 -0
- data/SPECIFICATION.md +306 -0
- data/lib/gpxkit/activity.rb +268 -0
- data/lib/gpxkit/error.rb +40 -0
- data/lib/gpxkit/field.rb +51 -0
- data/lib/gpxkit/limits.rb +63 -0
- data/lib/gpxkit/message.rb +63 -0
- data/lib/gpxkit/message_builder.rb +223 -0
- data/lib/gpxkit/reader.rb +112 -0
- data/lib/gpxkit/schema.rb +169 -0
- data/lib/gpxkit/types.rb +270 -0
- data/lib/gpxkit/version.rb +5 -0
- data/lib/gpxkit/xml/clock.rb +41 -0
- data/lib/gpxkit/xml/element.rb +60 -0
- data/lib/gpxkit/xml/parser.rb +193 -0
- data/lib/gpxkit/xml/source.rb +265 -0
- data/lib/gpxkit.rb +26 -0
- metadata +110 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GpxKit
|
|
4
|
+
# Convenience view of a GPX file, in the shape of FitKit::Activity.
|
|
5
|
+
#
|
|
6
|
+
# activity = GpxKit::Activity.read("ride.gpx", limits: { max_seconds: 30 })
|
|
7
|
+
# activity.sessions.each { |session| puts session.name }
|
|
8
|
+
# activity.records.each { |record| puts record.timestamp }
|
|
9
|
+
#
|
|
10
|
+
# A session is one <trk>; its segments hold its points. GPX has no laps
|
|
11
|
+
# and states no totals, so laps is always empty and totals are nil.
|
|
12
|
+
# Values are reported as the file states them: nothing is computed from
|
|
13
|
+
# positions or timestamps. The view does not assume an activity: a file
|
|
14
|
+
# with only routes or waypoints has no sessions or records.
|
|
15
|
+
#
|
|
16
|
+
# When two sources could fill a record value (hr from TrackPointExtension
|
|
17
|
+
# and from GPXDATA), the first valid one in the documented order wins
|
|
18
|
+
# (SPECIFICATION.md, decision 2); every source stays on the raw message.
|
|
19
|
+
class Activity
|
|
20
|
+
SUMMARY = %i[
|
|
21
|
+
sport sub_sport total_distance total_elapsed_time total_timer_time total_calories
|
|
22
|
+
avg_speed max_speed avg_heart_rate max_heart_rate avg_cadence max_cadence
|
|
23
|
+
avg_power max_power total_ascent total_descent num_laps
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
NO_LAPS = [].freeze
|
|
27
|
+
|
|
28
|
+
attr_reader :messages, :sessions, :records, :waypoints, :routes, :document
|
|
29
|
+
|
|
30
|
+
# options are passed to Reader.new, e.g. limits: { max_seconds: 30 }.
|
|
31
|
+
def self.read(source, **options)
|
|
32
|
+
new(Reader.new(source, **options).each_message.to_a)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.from_file(path, **options)
|
|
36
|
+
read(path, **options)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.from_io(io, **options)
|
|
40
|
+
read(io, **options)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def initialize(messages)
|
|
44
|
+
@messages = messages.freeze
|
|
45
|
+
points_by_segment = Hash.new { |hash, key| hash[key] = [] }
|
|
46
|
+
segments_by_track = Hash.new { |hash, key| hash[key] = [] }
|
|
47
|
+
points_by_route = Hash.new { |hash, key| hash[key] = [] }
|
|
48
|
+
@sessions = []
|
|
49
|
+
@records = []
|
|
50
|
+
@waypoints = []
|
|
51
|
+
@routes = []
|
|
52
|
+
@metadata = nil
|
|
53
|
+
@document = nil
|
|
54
|
+
|
|
55
|
+
@messages.each do |message|
|
|
56
|
+
context = message.context
|
|
57
|
+
case message.name
|
|
58
|
+
when :track_point
|
|
59
|
+
record = Record.new(message)
|
|
60
|
+
@records << record
|
|
61
|
+
points_by_segment[context.values_at(:track, :track_segment)] << record
|
|
62
|
+
when :track_segment
|
|
63
|
+
segment = Segment.new(message, points_by_segment.delete(context.values_at(:track, :track_segment)) || [])
|
|
64
|
+
segments_by_track[context[:track]] << segment
|
|
65
|
+
when :track
|
|
66
|
+
@sessions << Session.new(message, segments_by_track.delete(context[:track]) || [])
|
|
67
|
+
when :route_point then points_by_route[context[:route]] << Record.new(message)
|
|
68
|
+
when :route then @routes << Route.new(message, points_by_route.delete(context[:route]) || [])
|
|
69
|
+
when :waypoint then @waypoints << Record.new(message)
|
|
70
|
+
when :metadata then @metadata ||= message
|
|
71
|
+
when :document then @document = message
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
[@sessions, @records, @waypoints, @routes].each(&:freeze)
|
|
76
|
+
freeze
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def laps
|
|
80
|
+
NO_LAPS
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# The first session, or nil.
|
|
84
|
+
def session
|
|
85
|
+
@sessions.first
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The <metadata> message (GPX 1.1), or in GPX 1.0, which has none, the
|
|
89
|
+
# :document message that holds the same fields.
|
|
90
|
+
def metadata
|
|
91
|
+
@metadata || (version == "1.0" ? @document : nil)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# gpx@creator.
|
|
95
|
+
def creator
|
|
96
|
+
@document&.[](:creator)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The document's GPX version, "1.1" or "1.0" (the namespace decides).
|
|
100
|
+
def version
|
|
101
|
+
return unless @document
|
|
102
|
+
|
|
103
|
+
NAMESPACES[@document.namespace] || @document[:version]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def start_time
|
|
107
|
+
session&.start_time
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def end_time
|
|
111
|
+
@sessions.last&.end_time
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
SUMMARY.each do |name|
|
|
115
|
+
define_method(name) { session&.public_send(name) }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Base for the wrappers.
|
|
119
|
+
class Wrapper
|
|
120
|
+
attr_reader :raw_message
|
|
121
|
+
|
|
122
|
+
def initialize(message)
|
|
123
|
+
@raw_message = message
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def [](key)
|
|
127
|
+
@raw_message[key]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
# The value of the first valid field among sources, in order. A source
|
|
133
|
+
# is [namespace, element name], where :gpx means the document's GPX
|
|
134
|
+
# namespace (1.1, 1.0 or none). Only fields the schema puts there count.
|
|
135
|
+
def first_valid(sources)
|
|
136
|
+
sources.each do |namespace, name|
|
|
137
|
+
@raw_message.fields.each do |field|
|
|
138
|
+
next unless field.known? && field.valid? && leaf?(field.path, name)
|
|
139
|
+
next unless namespace == :gpx ? gpx_namespace?(field.namespace) : field.namespace == namespace
|
|
140
|
+
|
|
141
|
+
return field.value
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def gpx(name)
|
|
148
|
+
first_valid([[:gpx, name]])
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def leaf?(path, name)
|
|
152
|
+
path == name || path.end_with?("/#{name}")
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def gpx_namespace?(namespace)
|
|
156
|
+
namespace.nil? || NAMESPACES.key?(namespace)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# One <trk>.
|
|
161
|
+
class Session < Wrapper
|
|
162
|
+
attr_reader :segments
|
|
163
|
+
|
|
164
|
+
def initialize(message, segments)
|
|
165
|
+
super(message)
|
|
166
|
+
@segments = segments.freeze
|
|
167
|
+
freeze
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def name = gpx("name")
|
|
171
|
+
def description = gpx("desc")
|
|
172
|
+
def number = gpx("number")
|
|
173
|
+
|
|
174
|
+
# The track's <type>, as written ("running", "9", ...). GPX 1.0 has no
|
|
175
|
+
# type; one written there anyway is read as text.
|
|
176
|
+
def sport
|
|
177
|
+
field = @raw_message.fields.find { |candidate| candidate.path == "type" && gpx_namespace?(candidate.namespace) }
|
|
178
|
+
field&.raw_value
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def records
|
|
182
|
+
@segments.flat_map(&:records)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The first point with a valid time.
|
|
186
|
+
def start_time
|
|
187
|
+
@segments.each { |segment| segment.records.each { |record| return record.timestamp if record.timestamp } }
|
|
188
|
+
nil
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The last point with a valid time.
|
|
192
|
+
def end_time
|
|
193
|
+
@segments.reverse_each { |segment| segment.records.reverse_each { |record| return record.timestamp if record.timestamp } }
|
|
194
|
+
nil
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def laps = NO_LAPS
|
|
198
|
+
def num_laps = nil
|
|
199
|
+
|
|
200
|
+
# Not stated by GPX.
|
|
201
|
+
%i[sub_sport total_distance total_elapsed_time total_timer_time total_calories avg_speed max_speed
|
|
202
|
+
avg_heart_rate max_heart_rate avg_cadence max_cadence avg_power max_power total_ascent total_descent].each do |name|
|
|
203
|
+
define_method(name) { nil }
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# One <trkseg>.
|
|
208
|
+
class Segment < Wrapper
|
|
209
|
+
attr_reader :records
|
|
210
|
+
|
|
211
|
+
def initialize(message, records)
|
|
212
|
+
super(message)
|
|
213
|
+
@records = records.freeze
|
|
214
|
+
freeze
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# One <rte> with its points.
|
|
219
|
+
class Route < Wrapper
|
|
220
|
+
attr_reader :points
|
|
221
|
+
|
|
222
|
+
def initialize(message, points)
|
|
223
|
+
super(message)
|
|
224
|
+
@points = points.freeze
|
|
225
|
+
freeze
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def name = gpx("name")
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# One point: a track point, route point or waypoint.
|
|
232
|
+
class Record < Wrapper
|
|
233
|
+
TPX1 = Schema::TRACK_POINT_EXTENSION_V1
|
|
234
|
+
TPX2 = Schema::TRACK_POINT_EXTENSION_V2
|
|
235
|
+
POWER = Schema::POWER_EXTENSION_V1
|
|
236
|
+
GPXDATA = Schema::GPXDATA
|
|
237
|
+
|
|
238
|
+
# Where each value comes from, in order (decision 2).
|
|
239
|
+
SOURCES = {
|
|
240
|
+
heart_rate: [[TPX2, "hr"], [TPX1, "hr"], [GPXDATA, "hr"]],
|
|
241
|
+
cadence: [[TPX2, "cad"], [TPX1, "cad"], [GPXDATA, "cadence"]],
|
|
242
|
+
power: [[POWER, "PowerInWatts"], [:gpx, "power"]],
|
|
243
|
+
temperature: [[TPX2, "atemp"], [TPX1, "atemp"], [GPXDATA, "temp"]],
|
|
244
|
+
speed: [[:gpx, "speed"], [TPX2, "speed"]],
|
|
245
|
+
course: [[:gpx, "course"], [TPX2, "course"]],
|
|
246
|
+
distance: [[GPXDATA, "distance"]]
|
|
247
|
+
}.freeze
|
|
248
|
+
|
|
249
|
+
def initialize(message)
|
|
250
|
+
super
|
|
251
|
+
freeze
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def timestamp = gpx("time")
|
|
255
|
+
def latitude = gpx("@lat")
|
|
256
|
+
def longitude = gpx("@lon")
|
|
257
|
+
def altitude = gpx("ele")
|
|
258
|
+
def name = gpx("name")
|
|
259
|
+
|
|
260
|
+
SOURCES.each_key do |value|
|
|
261
|
+
define_method(value) { first_valid(SOURCES[value]) }
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Not in GPX.
|
|
265
|
+
def grade = nil
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
data/lib/gpxkit/error.rb
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GpxKit
|
|
4
|
+
# Base for every error the reader raises. `source` labels the input;
|
|
5
|
+
# `line` and `column` point at the place in the XML, when known.
|
|
6
|
+
class Error < StandardError
|
|
7
|
+
attr_reader :source, :line, :column
|
|
8
|
+
|
|
9
|
+
def initialize(message, source: nil, line: nil, column: nil)
|
|
10
|
+
@source = source
|
|
11
|
+
@line = line
|
|
12
|
+
@column = column
|
|
13
|
+
|
|
14
|
+
context = []
|
|
15
|
+
context << source if source
|
|
16
|
+
context << "line #{line}" if line
|
|
17
|
+
context << "column #{column}" if column
|
|
18
|
+
super(context.empty? ? message : "#{message} (#{context.join(', ')})")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Not well-formed XML.
|
|
23
|
+
class InvalidFileError < Error; end
|
|
24
|
+
# Well-formed XML, but not a document this gem reads.
|
|
25
|
+
class UnsupportedFormatError < Error; end
|
|
26
|
+
# The input ended before the root element closed.
|
|
27
|
+
class TruncatedFileError < Error; end
|
|
28
|
+
# A construct refused for safety, such as an entity declared in a DTD.
|
|
29
|
+
class UnsafeContentError < Error; end
|
|
30
|
+
|
|
31
|
+
# A Limits bound was crossed; `limit` names it.
|
|
32
|
+
class LimitExceededError < Error
|
|
33
|
+
attr_reader :limit
|
|
34
|
+
|
|
35
|
+
def initialize(message, limit:, **context)
|
|
36
|
+
@limit = limit
|
|
37
|
+
super(message, **context)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/gpxkit/field.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GpxKit
|
|
4
|
+
# One value inside a Message: a leaf element or an attribute.
|
|
5
|
+
#
|
|
6
|
+
# field.name # :hr
|
|
7
|
+
# field.path # "extensions/TrackPointExtension/hr", relative to the message element
|
|
8
|
+
# field.namespace # namespace URI of the element or attribute (nil for none)
|
|
9
|
+
# field.text # the text exactly as written
|
|
10
|
+
# field.raw_value # the text after the type's whitespace rule
|
|
11
|
+
# field.value # typed value, nil when invalid
|
|
12
|
+
# field.type # :decimal, :latitude, :date_time, :fix, :beats_per_minute, :string, :unknown, ...
|
|
13
|
+
# field.known? # false for elements and attributes the schema does not put here
|
|
14
|
+
# field.valid?
|
|
15
|
+
# field.invalid_reason # nil, :empty, :not_a_number, :not_finite, :not_an_integer,
|
|
16
|
+
# # :out_of_range, :not_a_date_time, :no_utc_offset,
|
|
17
|
+
# # :not_in_enumeration, :not_a_boolean, :not_a_year,
|
|
18
|
+
# # :too_short, :too_long, :no_pattern_match
|
|
19
|
+
# field.line
|
|
20
|
+
class Field
|
|
21
|
+
attr_reader :name, :path, :namespace, :text, :raw_value, :value, :type, :invalid_reason, :line
|
|
22
|
+
|
|
23
|
+
def initialize(name:, path:, namespace:, text:, spec:, line:)
|
|
24
|
+
@name = name
|
|
25
|
+
@path = path
|
|
26
|
+
@namespace = namespace
|
|
27
|
+
@text = text
|
|
28
|
+
@type = spec.name
|
|
29
|
+
@known = spec.known?
|
|
30
|
+
@raw_value, @value, @invalid_reason = Types.decode(spec, text)
|
|
31
|
+
@line = line
|
|
32
|
+
freeze
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def known?
|
|
36
|
+
@known
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def valid?
|
|
40
|
+
@invalid_reason.nil?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def attribute?
|
|
44
|
+
@path.split("/").last.start_with?("@")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def inspect
|
|
48
|
+
"#<#{self.class.name} #{path}=#{raw_value.inspect}#{valid? ? '' : " invalid: #{invalid_reason}"}>"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GpxKit
|
|
4
|
+
# Bounds on what one pass of a Reader will read, enforced while reading.
|
|
5
|
+
#
|
|
6
|
+
# The reader runs inside the caller's process and cannot be stopped safely
|
|
7
|
+
# from outside, so it checks its own limits and raises LimitExceededError as
|
|
8
|
+
# soon as one is crossed:
|
|
9
|
+
#
|
|
10
|
+
# - max_bytes: bytes read from the source.
|
|
11
|
+
# - max_elements: XML elements opened.
|
|
12
|
+
# - max_depth: element nesting; the root element is depth 1.
|
|
13
|
+
# - max_text_bytes: the text of one element, the value of one attribute, or
|
|
14
|
+
# the prolog before the root element.
|
|
15
|
+
# - max_seconds: time spent reading, on a monotonic clock. Time spent in
|
|
16
|
+
# the caller's block is not counted.
|
|
17
|
+
#
|
|
18
|
+
# nil switches a limit off.
|
|
19
|
+
class Limits
|
|
20
|
+
attr_reader :max_bytes, :max_elements, :max_depth, :max_text_bytes, :max_seconds
|
|
21
|
+
|
|
22
|
+
def initialize(max_bytes: 64 * 1024 * 1024, max_elements: 5_000_000, max_depth: 64,
|
|
23
|
+
max_text_bytes: 1024 * 1024, max_seconds: nil)
|
|
24
|
+
@max_bytes = positive(:max_bytes, max_bytes)
|
|
25
|
+
@max_elements = positive(:max_elements, max_elements)
|
|
26
|
+
@max_depth = positive(:max_depth, max_depth)
|
|
27
|
+
@max_text_bytes = positive(:max_text_bytes, max_text_bytes)
|
|
28
|
+
@max_seconds = positive(:max_seconds, max_seconds)
|
|
29
|
+
freeze
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.none
|
|
33
|
+
new(max_bytes: nil, max_elements: nil, max_depth: nil, max_text_bytes: nil, max_seconds: nil)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# A Limits, or a Hash of its keywords (missing keys keep their defaults).
|
|
37
|
+
def self.coerce(value)
|
|
38
|
+
case value
|
|
39
|
+
when Limits then value
|
|
40
|
+
when Hash then new(**value)
|
|
41
|
+
else raise ArgumentError, "limits must be a #{name} or a Hash"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def to_h
|
|
46
|
+
{ max_bytes: max_bytes, max_elements: max_elements, max_depth: max_depth,
|
|
47
|
+
max_text_bytes: max_text_bytes, max_seconds: max_seconds }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def positive(name, value)
|
|
53
|
+
return nil if value.nil?
|
|
54
|
+
raise ArgumentError, "#{name} must be a positive number or nil" unless value.is_a?(Numeric) && value.positive?
|
|
55
|
+
|
|
56
|
+
value
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# 64 MB, 5 million elements, depth 64, 1 MB of text per element, no time
|
|
61
|
+
# limit: generous for any real activity file.
|
|
62
|
+
Limits::DEFAULT = Limits.new
|
|
63
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GpxKit
|
|
4
|
+
# One meaningful GPX element, emitted when it closes (SPECIFICATION.md 6.2).
|
|
5
|
+
#
|
|
6
|
+
# message.name # :track_point, :track_segment, :track, :waypoint, ...
|
|
7
|
+
# message.element_name # the XML element's local name, e.g. "trkpt"
|
|
8
|
+
# message.context # { track: 0, track_segment: 1, track_point: 812 }
|
|
9
|
+
# message.line, message.column
|
|
10
|
+
# message.fields # every Field, in document order; names can repeat
|
|
11
|
+
# message[:ele] # value of the first field with that name
|
|
12
|
+
# message["extensions/TrackPointExtension/hr"] # value of the field at that path
|
|
13
|
+
class Message
|
|
14
|
+
attr_reader :name, :element_name, :namespace, :context, :line, :column, :fields
|
|
15
|
+
|
|
16
|
+
def initialize(name:, element_name:, namespace:, context:, line:, column:, fields:)
|
|
17
|
+
@name = name
|
|
18
|
+
@element_name = element_name
|
|
19
|
+
@namespace = namespace
|
|
20
|
+
@context = context
|
|
21
|
+
@line = line
|
|
22
|
+
@column = column
|
|
23
|
+
@fields = fields.freeze
|
|
24
|
+
freeze
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The typed value of a field, by name (Symbol) or path (String).
|
|
28
|
+
def [](key)
|
|
29
|
+
field(key)&.value
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def field(key)
|
|
33
|
+
case key
|
|
34
|
+
when Symbol then @fields.find { |field| field.name == key }
|
|
35
|
+
when String then @fields.find { |field| field.path == key }
|
|
36
|
+
else raise ArgumentError, "field lookup takes a name (Symbol) or a path (String)"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def fields_named(name)
|
|
41
|
+
@fields.select { |field| field.name == name.to_sym }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Fields for the attributes of the message element itself.
|
|
45
|
+
def attributes
|
|
46
|
+
@fields.select { |field| field.path.start_with?("@") }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The time field of a point (or of metadata), else nil.
|
|
50
|
+
def timestamp
|
|
51
|
+
self[:time]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Name => value, for the first field of each name.
|
|
55
|
+
def to_h
|
|
56
|
+
@fields.each_with_object({}) { |field, hash| hash[field.name] = field.value unless hash.key?(field.name) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def inspect
|
|
60
|
+
"#<#{self.class.name} #{name} #{context.inspect} #{fields.size} fields>"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|