Class: Netprint::Agent
- Inherits:
-
Object
- Object
- Netprint::Agent
- Includes:
- FileUtils
- Defined in:
- lib/netprint/agent.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#userid ⇒ Object
readonly
Returns the value of attribute userid.
Instance Method Summary collapse
-
#initialize(userid, password) ⇒ Agent
constructor
A new instance of Agent.
- #login ⇒ Object
- #login? ⇒ Boolean
- #upload(filename, options = {}) ⇒ Object
Constructor Details
#initialize(userid, password) ⇒ Agent
Returns a new instance of Agent.
13 14 15 16 |
# File 'lib/netprint/agent.rb', line 13 def initialize(userid, password) @userid = userid @password = password end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
9 10 11 |
# File 'lib/netprint/agent.rb', line 9 def password @password end |
#userid ⇒ Object (readonly)
Returns the value of attribute userid.
9 10 11 |
# File 'lib/netprint/agent.rb', line 9 def userid @userid end |
Instance Method Details
#login ⇒ Object
18 19 20 21 |
# File 'lib/netprint/agent.rb', line 18 def login page = mechanize.get(url.login) @session_id = page.links[0].href.match(/s=([^&]+)/)[1] end |
#login? ⇒ Boolean
48 49 50 |
# File 'lib/netprint/agent.rb', line 48 def login? @session_id end |
#upload(filename, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/netprint/agent.rb', line 23 def upload(filename, = {}) raise 'not logged in' unless login? = Options.new() 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 .apply(form) end.submit raise UploadError if page.search('//img[@src="/img/icn_error.jpg"]').size == 1 get_code end end |