Class: Rec

Inherits:
Object
  • Object
show all
Defined in:
lib/rec.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Rec

Returns a new instance of Rec.



4
5
6
7
8
9
10
11
# File 'lib/rec.rb', line 4

def initialize(user)
  @user_list = user.mylist
  @user = user
  @tracks = []
  @artists = []
  @genres = []
  @prompt = TTY::Prompt.new
end

Instance Method Details

#amount_of_suggestionsObject

Prompts user to choose how many suggestions to generate



14
15
16
17
18
19
20
21
22
# File 'lib/rec.rb', line 14

def amount_of_suggestions
  system('clear')
  puts '》  RECOMMENDATIONS  《'
  amount = @prompt.ask('How many recommendations would you like to generate?'.colorize(:light_green)) do |q|
    q.in '1-10'
    q.messages[:range?] = 'Number must be between 1 and 10'
  end
  recommend(amount.to_i)
end

#clean_recommendations(selections) ⇒ Object

Searches again for song selections to get the track object rather than just the name and artist



54
55
56
57
58
59
60
61
# File 'lib/rec.rb', line 54

def clean_recommendations(selections)
  selections.each do |track|
    details = track.split(' by ')
    song = RSpotify::Track.search("#{details[0]} #{details[1]}", limit: 1).first
    song_details(song)
  end
  update_file
end

#recommend(num) ⇒ Object

Prompts user to select which recommendations to add to their playlist



25
26
27
28
29
30
31
32
33
# File 'lib/rec.rb', line 25

def recommend(num)
  system('clear')
  recommendations = recommendations_generate(num)
  cleaned_recs = recommendations.tracks.map { |t| "#{t.name} by #{t.artists[0].name}" }
  puts '》  RECOMMENDATIONS  《'.colorize(:light_green)
  puts 'Select the recommendations you want to add to your playlist!'
  selections = @prompt.multi_select('Hit enter with none selected to skip.', cleaned_recs)
  clean_recommendations(selections)
end

#recommendations_generate(num) ⇒ Object

Generates recommendations with RSpotify



48
49
50
51
# File 'lib/rec.rb', line 48

def recommendations_generate(num)
  sort_my_list
  RSpotify::Recommendations.generate(limit: num, seed_artists: @artists, seed_genres: @genres, seed_tracks: @tracks)
end

#song_details(song) ⇒ Object

Pulls song details from track and stores in a hash. Pushes to user playlist array



64
65
66
67
68
69
70
71
# File 'lib/rec.rb', line 64

def song_details(song)
  song_details = {
    'name' => song.name,
    'id' => song.id,
    'artist' => song.artists[0].name
  }
  @user.playlist << song_details
end

#sort_my_listObject

Sorts list items into arrays to be used in generating recommendations



36
37
38
39
40
41
42
43
44
45
# File 'lib/rec.rb', line 36

def sort_my_list
  @tracks.clear
  @artists.clear
  @genres.clear
  @user_list.each do |item|
    @tracks << item['id'] if item['type'] == 'track'
    @artists << item['id'] if item['type'] == 'artist'
    @genres << item['name'].downcase if item['type'] == 'genre'
  end
end

#update_fileObject

Updates file with current user playlist



74
75
76
77
78
79
80
81
82
83
# File 'lib/rec.rb', line 74

def update_file
  updated_data = .load_data.each { |user| user['playlist'] = @user.playlist if user['id'] == @user.uid.to_s }
  File.open(userdata, 'w') do |f|
    f.puts JSON.pretty_generate(updated_data)
  end
  puts 'Sweet! Your list has been updated!'.colorize(:light_green)
  @prompt.keypress('Press any key to return to the previous menu..')
  menu = Menu.new(@user)
  menu.menu_router
end