Class: JarvisbotSongfinder::SpotifyAPI

Inherits:
Provider
  • Object
show all
Defined in:
lib/jarvisbot_songfinder/providers/spotify_api.rb

Constant Summary collapse

ID_REGEX =
%r{\Ahttps?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-){22})}.freeze
URL_REGEX =
%r{spotify\.com/}.freeze

Instance Attribute Summary

Attributes inherited from Provider

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Provider

#valid?

Constructor Details

#initialize(url, config: JarvisbotSongfinder.configuration) ⇒ SpotifyAPI

Returns a new instance of SpotifyAPI.



9
10
11
12
13
14
15
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 9

def initialize(url, config: JarvisbotSongfinder.configuration)
  super()
  @config = config
  @track_id = get_track_id(url)
  check_id_validity
  valid? unless @errors.any?
end

Class Method Details

.from_search(query, config: JarvisbotSongfinder.configuration) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 17

def self.from_search(query, config: JarvisbotSongfinder.configuration)
  @config = config
  results = RSpotify::Track.search(query, market: @config.region)
  if results.any?
    link = "https://open.spotify.com/track/#{results.first.id}"
    return new(link)
  else
    return nil
  end
end

Instance Method Details

#artistObject



36
37
38
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 36

def artist
  @track.artists.first.name
end

#explicit?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 48

def explicit?
  @track.explicit
end

#lengthObject



28
29
30
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 28

def length
  @track.duration_ms / 1000
end

#providerObject



44
45
46
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 44

def provider
  "spotify"
end

#titleObject



32
33
34
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 32

def title
  @track.name
end

#urlObject



40
41
42
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 40

def url
  "https://open.spotify.com/track/#{@track.id}"
end