Module: Julewire::Core::Integration::Values::Shape

Defined in:
lib/julewire/core/integration/values.rb

Class Method Summary collapse

Class Method Details

.append_compact_field(fields, key, value) ⇒ Object



174
175
176
# File 'lib/julewire/core/integration/values.rb', line 174

def append_compact_field(fields, key, value)
  append_field(fields, key, value, compact_empty: true)
end

.append_field(fields, key, value, compact_empty: false) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/julewire/core/integration/values.rb', line 166

def append_field(fields, key, value, compact_empty: false)
  return if value.nil?
  return if compact_empty && (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?

  fields[key] = value
  nil
end

.hash_or_empty(value) ⇒ Object



159
160
161
162
163
164
# File 'lib/julewire/core/integration/values.rb', line 159

def hash_or_empty(value)
  return empty_hash unless value.is_a?(Hash)
  return empty_hash if value.empty?

  Fields::FieldSet.deep_symbolize_keys(value)
end

.payload_hash(value) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/julewire/core/integration/values.rb', line 146

def payload_hash(value)
  case value
  when nil
    empty_hash
  when Hash
    return empty_hash if value.empty?

    Fields::FieldSet.deep_symbolize_keys(value)
  else
    { Fields::FieldSet::VALUE_KEY => value }
  end
end

.source_location_attributes(location) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/julewire/core/integration/values.rb', line 178

def source_location_attributes(location)
  return {} unless location.is_a?(Hash)

  Fields::AttributeKeys.fields(
    Fields::AttributeKeys::CODE_FILE_PATH => Read.first_value(location, keys: %i[filepath path file]),
    Fields::AttributeKeys::CODE_LINE_NUMBER => Read.first_value(location, keys: %i[lineno line]),
    Fields::AttributeKeys::CODE_FUNCTION_NAME => Read.first_value(location, keys: %i[label function])
  )
end

.timestamp(value) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/julewire/core/integration/values.rb', line 135

def timestamp(value)
  return unless value
  return value.utc.iso8601(9) if value.respond_to?(:utc) && value.respond_to?(:iso8601)
  return value unless value.respond_to?(:divmod)

  seconds, nanoseconds = value.divmod(1_000_000_000)
  Time.at(seconds, nanoseconds, :nanosecond).utc.iso8601(9)
rescue StandardError
  nil
end