Module: SimpleSpotify

Defined in:
lib/simplespotify.rb,
lib/simplespotify/client.rb,
lib/simplespotify/errors.rb,
lib/simplespotify/request.rb,
lib/simplespotify/version.rb,
lib/simplespotify/response.rb,
lib/simplespotify/constants.rb,
lib/simplespotify/models/user.rb,
lib/simplespotify/models/album.rb,
lib/simplespotify/models/image.rb,
lib/simplespotify/models/track.rb,
lib/simplespotify/actions/users.rb,
lib/simplespotify/authorization.rb,
lib/simplespotify/models/artist.rb,
lib/simplespotify/actions/albums.rb,
lib/simplespotify/actions/browse.rb,
lib/simplespotify/actions/player.rb,
lib/simplespotify/actions/tracks.rb,
lib/simplespotify/actions/artists.rb,
lib/simplespotify/models/category.rb,
lib/simplespotify/models/playlist.rb,
lib/simplespotify/models/playevent.rb,
lib/simplespotify/actions/playlists.rb,
lib/simplespotify/models/collection.rb

Defined Under Namespace

Modules: Actions, Error, Model, Models Classes: Authorization, Client, Request, Response

Constant Summary collapse

VERSION =
"0.1.2"
AUTHORIZATION_URL =

Constants


'https://accounts.spotify.com/authorize'
TOKEN_URL =
'https://accounts.spotify.com/api/token'
ENDPOINT =
'https://api.spotify.com/v1/'
MAX_RETRIES =

Upper bound when trying to refresh the authentication token

2
RETRY_IF_RATELIMITED =

Should we block the whole program until we can retry a rate-limited request? set to “please” if you’d like to enable this potentially web-request blocking option

false

Class Method Summary collapse

Class Method Details

.default_clientObject



24
25
26
# File 'lib/simplespotify.rb', line 24

def self.default_client
  @@client
end

.default_client=(client) ⇒ Object



20
21
22
# File 'lib/simplespotify.rb', line 20

def self.default_client= client
  @@client = client
end

.dispatch(request, session: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/simplespotify.rb', line 39

def self.dispatch request, session: nil
  headers = request.headers
  headers = headers.merge(session.headers) if session && request.private?

  options = {headers: headers}.merge(request.options)

  request.tries += 1;

  log "#{request.method.to_s.upcase} <#{request.full_url}>"
  log request.options

  http = HTTParty.send(
    request.method,
    request.full_url,
    options
  )

  log "HTTP #{http.code}"
  log http.body
  log "---"

  if session && http.code == 401 && http.body =~ /The access token expired/
    if request.tries < SimpleSpotify::MAX_RETRIES
      session.refresh!
      return self.dispatch(request, session: session)
    else
      raise SimpleSpotify::Error::AuthorizationError(JSON.parse(http.body, symbolize_names: true))
    end
  end

  if http.code == 429 and SimpleSpotify::RETRY_IF_RATELIMITED == 'please'
    raise SimpleSpotify::Error::RateLimited(429, http.body, request) if request.tries > SimpleSpotify::MAX_RETRIES
    seconds = http.headers['Retry-After'].to_i + 1 # because computers (purely theoretic)
    $stderr.puts "[simplespotify] Rate limited. Waiting #{seconds} seconds."
    $stderr.puts "[simplespotify] waking up at #{Time.now + seconds}"
    sleep seconds
    return self.dispatch(request, session: session)
  end

  return Response.new(http.code, http.body, request)
end

.log(msg) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/simplespotify.rb', line 29

def self.log msg
  return true unless ENV['DEBUG']
  if msg.is_a? String
    $stdout.puts msg
  else
    $stdout.puts pp msg
  end
end