class ADPDownloader::HttpClient

Public Class Methods

new() click to toggle source
# File lib/adp-downloader/http_client.rb, line 6
def initialize
  # FIXME: https://github.com/andersonvom/adp-downloader/issues/6
  #_raise_on_error(_login(Config.credentials))
end

Public Instance Methods

download(url) click to toggle source
# File lib/adp-downloader/http_client.rb, line 22
def download(url)
  agent.get(url).body
end
get(url) click to toggle source
# File lib/adp-downloader/http_client.rb, line 11
def get(url)
  res = agent.get(url)
  _raise_on_error(res)
  contents = res.body
  contents.to_s.empty? ? {} : JSON.parse(contents)
end
post(url, data) click to toggle source
# File lib/adp-downloader/http_client.rb, line 18
def post(url, data)
  agent.post(url, data)
end

Private Instance Methods

_login(creds) click to toggle source
# File lib/adp-downloader/http_client.rb, line 35
def _login(creds)
  agent.post(LOGIN_URL, {
    "target" => TARGET_URL,
    "user" => creds[:username],
    "password" => creds[:password],
  })
end
_raise_on_error(res) click to toggle source
# File lib/adp-downloader/http_client.rb, line 43
def _raise_on_error(res)
  uri = res.uri.to_s.downcase
  #if not uri.start_with? TARGET_URL or uri.include? "login"
  if uri.include? "login"
    #raise "Unable to authenticate: make sure your username and password are correct"
    raise "Unable to authenticate: make sure the file cookie.txt is up to date"
  end
end
agent() click to toggle source
# File lib/adp-downloader/http_client.rb, line 27
def agent
  headers = {
    "Accept" => "application/json, text/plain, */*",
    "Cookie" => "SMSESSION=#{Config.credentials[:smsession_cookie]}",
  }
  @agent ||= Mechanize.new {|a| a.request_headers = headers}
end