class Pandata::Downloader

Retrieves data from Pandora.com and handles network errors.

Constants

CONFIG_URL

A GitHub Gist that contains an updated cookie allowing access to ‘login-only’ visible data.

Public Class Methods

get_pat() click to toggle source

Returns a pat token needed for mobile requests. @return [String]

# File lib/pandata/downloader.rb, line 22
def self.get_pat
  session['pat']
end
read_page(url) click to toggle source

Downloads and reads a page from a URL. @param url [String] @return [String] contents of page

# File lib/pandata/downloader.rb, line 16
def self.read_page(url)
  download(url, session['cookie']).read
end
set_session(session) click to toggle source

Manually sets the cached session. @return [Hash]

# File lib/pandata/downloader.rb, line 28
def self.set_session(session)
  @@session = session
end

Private Class Methods

download(url, cookie = '') click to toggle source

Downloads a page and handles errors. @param url [String] @param cookie [String] @return [File]

# File lib/pandata/downloader.rb, line 38
def self.download(url, cookie = '')
  escaped_url = URI.escape(url)

  open(escaped_url, 'Cookie' => cookie, :read_timeout => 5)
rescue OpenURI::HTTPError => error
  puts "The network request for:\n  #{url}\nreturned an error:\n  #{error.message}"
  puts "Please try again later or update Pandata. Sorry about that!\n\nFull error:"
  raise PandataError
end
get_random_session() click to toggle source
# File lib/pandata/downloader.rb, line 52
def self.get_random_session
  config = JSON.parse download(CONFIG_URL).read

  if Gem::Version.new(Pandata::Version::STRING) <= Gem::Version.new(config['required_update_for'])
    raise PandataError, 'Pandora.com has changed something and you need to update Pandata!'
  end

  config['sessions'].sample
end
session() click to toggle source
# File lib/pandata/downloader.rb, line 48
def self.session
  @@session ||= get_random_session
end