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 17 |
# File 'lib/netprint/agent.rb', line 13 def initialize(userid, password) @userid = userid @password = password @page = nil 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
19 20 21 22 23 24 25 |
# File 'lib/netprint/agent.rb', line 19 def login @page = mechanize.get('https://www.printing.ne.jp/usr/web/NPCM0010.seam') form = @page.form_with(name: 'NPCM0010') form.field_with(name: 'NPCM0010:userIdOrMailads-txt').value = @userid form.field_with(name: 'NPCM0010:password-pwd').value = @password @page = form.submit(form.(name: 'NPCM0010:login-btn')) end |
#login? ⇒ Boolean
58 59 60 |
# File 'lib/netprint/agent.rb', line 58 def login? @page && @page.code == '200' end |
#upload(filename, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/netprint/agent.rb', line 27 def upload(filename, = {}) raise 'not logged in' unless login? form = @page.form_with(name: 'NPFL0010') @page = form.submit(form.(name: 'create-document')) = 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 form = @page.form_with(name: 'NPFL0020') form.file_uploads.first.file_name = upload_filename .apply(form) @page = form.submit(form.(name: 'update-ow-btn')) errors = @page.search('//ul[@id="svErrMsg"]/li') unless errors.empty? raise UploadError.new(errors.first.text) end get_code end end |