class AppleMusic::Config

Constants

ALGORITHM
DEFAULT_STOREFRONT
TOKEN_EXPIRATION_TIME

Attributes

adapter[RW]
music_id[RW]
secret_key[RW]
secret_key_path[RW]
storefront[RW]
team_id[RW]
token_expiration_time[RW]

Public Class Methods

new() click to toggle source
# File lib/apple_music/config.rb, line 16
def initialize
  @secret_key_path = ENV['APPLE_MUSIC_SECRET_KEY_PATH']
  @secret_key = ENV['APPLE_MUSIC_SECRET_KEY']
  @team_id = ENV['APPLE_MUSIC_TEAM_ID']
  @music_id = ENV['APPLE_MUSIC_MUSIC_ID']
  @token_expiration_time = TOKEN_EXPIRATION_TIME
  @adapter = Faraday.default_adapter
  @storefront = ENV.fetch('APPLE_MUSIC_STOREFRONT') { DEFAULT_STOREFRONT }
end

Public Instance Methods

authentication_token() click to toggle source
# File lib/apple_music/config.rb, line 26
def authentication_token
  private_key = OpenSSL::PKey::EC.new(apple_music_secret_key)
  JWT.encode(authentication_payload, private_key, ALGORITHM, kid: music_id)
end

Private Instance Methods

apple_music_secret_key() click to toggle source
# File lib/apple_music/config.rb, line 33
def apple_music_secret_key
  @secret_key ||= File.read(secret_key_path)
end
authentication_payload(now = Time.now) click to toggle source
# File lib/apple_music/config.rb, line 37
def authentication_payload(now = Time.now)
  {
    iss: team_id,
    iat: now.to_i,
    exp: now.to_i + token_expiration_time
  }
end