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
# File 'lib/netprint/agent.rb', line 13

def initialize(userid, password)
  @userid   = userid
  @password = password
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



18
19
20
21
# File 'lib/netprint/agent.rb', line 18

def 
  page        = mechanize.get(url.)
  @session_id = page.links[0].href.match(/s=([^&]+)/)[1]
end

#login?Boolean

Returns:

  • (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, 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