Class: Netprint::Agent

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/netprint/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/netprint/agent.rb', line 9

def password
  @password
end

#useridObject (readonly)

Returns the value of attribute userid.



9
10
11
# File 'lib/netprint/agent.rb', line 9

def userid
  @userid
end

Instance Method Details

#loginObject



19
20
21
22
23
24
25
# File 'lib/netprint/agent.rb', line 19

def 
  @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.button_with(name: 'NPCM0010:login-btn'))
end

#login?Boolean

Returns:

  • (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, options = {})
  raise 'not logged in' unless login?

  form = @page.form_with(name: 'NPFL0010')
  @page = form.submit(form.button_with(name: 'create-document'))

  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

    form = @page.form_with(name: 'NPFL0020')
    form.file_uploads.first.file_name = upload_filename
    options.apply(form)
    @page = form.submit(form.button_with(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