Sha256: 07b1205779b6e419c60b5ce6ae5a6d3c9dfe09409e8ec5d745762c765a597f04
Contents?: true
Size: 1.27 KB
Versions: 2
Compression:
Stored size: 1.27 KB
Contents
require "json" require "mechanize" module ADPDownloader class HttpClient def initialize # FIXME: https://github.com/andersonvom/adp-downloader/issues/6 #_raise_on_error(_login(Config.credentials)) end def get(url) res = agent.get(url) _raise_on_error(res) contents = res.body contents.to_s.empty? ? {} : JSON.parse(contents) end def post(url, data) agent.post(url, data) end def download(url) agent.get(url).body end private def agent headers = { "Accept" => "application/json, text/plain, */*", "Cookie" => "SMSESSION=#{Config.credentials[:smsession_cookie]}", } @agent ||= Mechanize.new {|a| a.request_headers = headers} end def _login(creds) agent.post(LOGIN_URL, { "target" => TARGET_URL, "user" => creds[:username], "password" => creds[:password], }) end 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 end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
adp-downloader-0.4.0 | lib/adp-downloader/http_client.rb |
adp-downloader-0.3.2 | lib/adp-downloader/http_client.rb |