Module: API::Spotify
- Included in:
- Hibot::Spotify
- Defined in:
- lib/api/spotify.rb
Constant Summary collapse
- WS_URL =
"https://api.spotify.com/v1"- PERMITTED_TYPES =
%w{album artist track}
- URI_REGEX =
/spotify:([a-z]+):(\w+)/
Instance Method Summary collapse
Instance Method Details
#parse_uri(uri) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/api/spotify.rb', line 30 def parse_uri(uri) matcher = uri.match(URI_REGEX) type = matcher[1] spotify_id = matcher[2] parsed_uri = nil response = HTTParty.get("#{WS_URL}/#{type}s/#{spotify_id}?client_id=#{@@client_id}") if response['name'] if response['artists'] parsed_uri = response['artists'][0]['name'] + " - " + response['name'] else parsed_uri = response['name'] end end parsed_uri end |
#search(query) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/api/spotify.rb', line 7 def search(query) # Check that parameters are correct or return the corresponding error query = query.gsub('!ssearch', '').strip type, q = query.split(' ') return "Invalid search request. You have to provide a valid type (artist, album or track) and then your query" if type.nil? || q.nil? return "Bad request. Allowed types are album, artist and track." unless PERMITTED_TYPES.include?(type) # Perform the search and return the result result = "" response = HTTParty.get("#{WS_URL}/search?type=#{type}&q=#{URI.encode(q)}") if response["#{type}s"] && response["#{type}s"]['items'].count > 0 i = 1 response["#{type}s"]['items'].each do |item| result += "#{i}. #{item['name']} --> #{item['uri']} --> #{item['external_urls']['spotify']}\n" i += 1 break if i == 5 end else result = "Nothing found with that parameter." end result end |