Sha256: 4b6a8a16c41c0a6029cbabf6e510a1240fcca6c4903c5d0768e8a9ac645ef7a9
Contents?: true
Size: 1.76 KB
Versions: 2
Compression:
Stored size: 1.76 KB
Contents
# -*- coding: utf-8 -*- require "tmpdir" require "fileutils" require "pathname" require 'digest/md5' module Netprint class Agent attr_reader :userid, :password include FileUtils def initialize(userid, password) @userid = userid @password = password end def login page = mechanize.get(url.login) @session_id = page.links[0].href.match(/s=([^&]+)/)[1] end def upload(filename, options = {}) raise 'not logged in' unless login? options = Options.new(options) Dir.mktmpdir do |dir| upload_filename = (Pathname(dir) + ([ Time.now.to_f.to_s, Digest::MD5.hexdigest(filename).to_s, File.basename(filename).gsub(/[^\w]+/, '') + File.extname(filename) ].join('_'))).to_s cp filename, upload_filename page = mechanize.get(url.upload) page = page.form_with(:name => 'uploadform') do |form| form.file_uploads.first.file_name = upload_filename options.apply(form) end.submit raise UploadError if page.search('//img[@src="/img/icn_error.jpg"]').size == 1 get_code end end def login? @session_id end private def get_code code = nil loop do page = mechanize.get(url.list) _, registered_name, status = page.search('//tr[@bgcolor="#CFCFE6" or @bgcolor="#ff6666"][1]/td') if status.text =~ /^[0-9A-Z]{8}+$/ code = status.text break elsif status.text == 'エラー' raise RegistrationError end sleep 1 end code end def url URL.new(@session_id, userid, password) end def mechanize @mechanize ||= Mechanize.new end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
netprint-0.2.0 | lib/netprint/agent.rb |
netprint-0.1.1 | lib/netprint/agent.rb |