lib/netprint/agent.rb in netprint-0.0.2 vs lib/netprint/agent.rb in netprint-0.0.3
- old
+ new
@@ -13,29 +13,27 @@
@userid = userid
@password = password
end
def login
- page = mechanize.get(login_url)
+ page = mechanize.get(url.login)
@session_id = page.links[0].href.match(/s=([^&]+)/)[1]
end
- def upload(filename)
+ 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, File.basename(filename)].join('_'))).to_s
cp filename, upload_filename
- page = mechanize.get(upload_url)
+ page = mechanize.get(url.upload)
page = page.form_with(:name => 'uploadform') do |form|
form.file_uploads.first.file_name = upload_filename
- form['papersize'] = '0'
- form['color'] = '0'
- form['number'] = '0'
- form['secretcodesw'] = '0'
- form['mailsw'] = '0'
+ options.apply(form)
end.submit
raise UploadError if page.search('//img[@src="/img/icn_error.jpg"]').size == 1
get_code
@@ -50,11 +48,11 @@
def get_code
code = nil
loop do
- page = mechanize.get(list_url)
+ page = mechanize.get(url.list)
_, registered_name, status = page.search('//tr[@bgcolor="#CFCFE6"][1]/td')
if status.text =~ /^[0-9A-Z]{8}+$/
code = status.text
break
@@ -66,24 +64,11 @@
end
code
end
- def upload_url
- expand_url(:s => @session_id, :c => 0, :m => 1)
- end
-
- def list_url
- expand_url(:s => @session_id, :c => 0, :m => 0)
- end
-
- def login_url
- expand_url(:i => userid, :p => password)
- end
-
- def expand_url(params)
- Addressable::Template.new('https://www.printing.ne.jp/cgi-bin/mn.cgi?{-join|&|i,p,s,c,m}').
- expand(params)
+ def url
+ URL.new(@session_id, userid, password)
end
def mechanize
@mechanize ||= Mechanize.new
end