Class: JarvisbotSongfinder::SpotifyAPI
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #artist ⇒ Object
- #explicit? ⇒ Boolean
-
#initialize(url, config: JarvisbotSongfinder.configuration) ⇒ SpotifyAPI
constructor
A new instance of SpotifyAPI.
- #length ⇒ Object
- #provider ⇒ Object
- #title ⇒ Object
- #url ⇒ Object
Methods inherited from Provider
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
#artist ⇒ Object
36 37 38 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 36 def artist @track.artists.first.name end |
#explicit? ⇒ Boolean
48 49 50 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 48 def explicit? @track.explicit end |
#length ⇒ Object
28 29 30 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 28 def length @track.duration_ms / 1000 end |
#provider ⇒ Object
44 45 46 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 44 def provider "spotify" end |
#title ⇒ Object
32 33 34 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 32 def title @track.name end |
#url ⇒ Object
40 41 42 |
# File 'lib/jarvisbot_songfinder/providers/spotify_api.rb', line 40 def url "https://open.spotify.com/track/#{@track.id}" end |