Class: ListenBrainz::Base

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/listenbrainz/models/base.rb

Overview

Base class for ListenBrainz models.

Instance Method Summary collapse

Instance Method Details

#as_jsonHash{Symbol => Object}

Returns the model as a ListenBrainz JSON object.

Returns:

  • (Hash{Symbol => Object})


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/listenbrainz/models/base.rb', line 11

def as_json
  attributes.transform_values do |value|
    case value
    when Array
      value.map { |item| item.try(:as_json) || item }
    when Time
      # HACK: ListenBrainz expects timestamps as Unix timestamps.
      #
      # TODO: Embed this in the `Types::Timestamp` class and use
      #       introspection here instead?
      value.to_i
    else
      value.try(:as_json) || value
    end
  end
end