class JarvisbotSongfinder::SpotifyAPI

Constants

ID_REGEX
URL_REGEX

Public Class Methods

new(url, config: JarvisbotSongfinder.configuration) click to toggle source
Calls superclass method JarvisbotSongfinder::Provider::new
# 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

Public Instance Methods

artist() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 36
def artist
  @track.artists.first.name
end
explicit?() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 48
def explicit?
  @track.explicit
end
length() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 28
def length
  @track.duration_ms / 1000
end
provider() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 44
def provider
  "spotify"
end
title() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 32
def title
  @track.name
end
url() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 40
def url
  "https://open.spotify.com/track/#{@track.id}"
end

Private Instance Methods

available_in_region?() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 75
def available_in_region?
  return true if @track.available_markets.empty?

  if @track.available_markets.include? @config.region
    return true
  else
    add_error ReplyMessage::Request.region_restricted(@config.region)
    return false
  end
end
check_id_validity() click to toggle source
# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 64
def check_id_validity
  @track = RSpotify::Track.find(@track_id, market: @config.region)
rescue RestClient::BadRequest
  add_error ReplyMessage::Request.invalid_video_id
end
get_track_id(url) click to toggle source

:nocov:

# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 55
def get_track_id(url)
  if !ID_REGEX.match(url)
    add_error ReplyMessage::Request.invalid_video_id
    nil
  else
    ID_REGEX.match(url)[1]
  end
end
in_music_category?() click to toggle source

:nocov:

# File lib/jarvisbot_songfinder/providers/spotify_api.rb, line 71
def in_music_category?
  true
end