Module: Shazamio::Serialize

Defined in:
lib/shazamio/serialize.rb

Overview

The Python project uses Pydantic + dataclass-factory to map Shazam's raw JSON onto a large tree of typed models (see shazamio/schemas/**). Porting that whole schema tree 1:1 would be a lot of low-value boilerplate, so this Ruby port instead exposes the handful of fields people actually use (mirroring factory.py's name_mappings) as plain structs, while always keeping the full raw Hash available too via .raw.

Defined Under Namespace

Classes: Artist, Track

Class Method Summary collapse

Class Method Details

.artist(data) ⇒ Object

Parameters:

  • data (Hash)

    one "artist" object, e.g. from #search_artist hits, or the top-level result of #artist_about (legacy v1 shape).



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shazamio/serialize.rb', line 48

def artist(data)
  attributes = data["attributes"] || data
  genres = data["genres"] || {}

  Artist.new(
    adam_id: data["adamid"] || data["id"],
    name: attributes["name"] || data["name"],
    avatar: data["avatar"],
    url: attributes["url"] || data["weburl"],
    genres_primary: genres["primary"],
    genres: genres["secondaries"],
    raw: data
  )
end

.track(data) ⇒ Object

Parameters:

  • data (Hash)

    one "track" object as returned by e.g. #track_about, #search_track, #top_world_tracks, etc.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shazamio/serialize.rb', line 26

def track(data)
  hub = data["hub"] || {}
  options = (hub["options"] || [])[0] || {}
  providers = (hub["providers"] || [])[0] || {}
  actions = hub["actions"] || []

  Track.new(
    title: data["title"],
    subtitle: data["subtitle"],
    artist_id: data.dig("artists", 0, "id"),
    photo_url: data.dig("images", "coverarthq"),
    ringtone: actions.dig(1, "uri"),
    apple_music_url: options.dig("actions", 0, "uri"),
    spotify_url: providers.dig("actions", 0, "uri"),
    spotify_uri: providers.dig("actions", 1, "uri"),
    shazam_url: data["url"],
    raw: data
  )
end