Sha256: 1c89fd8fd9b1a6a1188cb81d38611d2dcd43aaa76390c7a49e9f46c42600e1e9

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require 'mechanize'
require 'logger'

class GLSMech
  attr_accessor :mech
  attr_accessor :user
  attr_accessor :pass

  # Setup the mech.
  def initialize
    @mech = Mechanize.new
    @mech.user_agent_alias = 'Windows Mozilla'
  end

  # Enable logging to given file.
  def log_to filename
    @mech.log = Logger.new filename
  end

  # Saves parcel label as pdf, does not overwrite file if exists,
  # returns filename that label was saved to.
  def save_parcel_label parcel_job, filename
    login! @user, @pass
    form = @mech.page.forms.first

    form.field_with(:name => 'txtName1').value = parcel_job.name
    form.field_with(:name => 'txtStreet').value = parcel_job.street
    form.field_with(:name => 'txtBlockNo').value = parcel_job.streetno
    form.field_with(:name => 'txtZipCodeDisplay').value = parcel_job.zip
    form.field_with(:name => 'txtCity').value = parcel_job.city
    form.field_with(:name => 'txtWeight').value = parcel_job.weight
    
    @mech.submit(form, form.buttons.first)
    @mech.page.iframes.first.content.save_as filename
  end

  private

  # Login to GLS parcel creation web page using provided credentials.
  def login! username, password
    target_url = 'http://www.your-gls.eu/276-I-PORTAL-WEB/content/GLS/DE03/DE/15005.htm'
    page = @mech.get target_url
    form = page.forms.first
    form.fields[5].value = username
    form.fields[6].value = password
    form.submit
    # Move on to target page.
    page = @mech.get target_url
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gls_agent-0.1.1 lib/gls_agent/gls_mech.rb
gls_agent-0.1.0 lib/gls_agent/gls_mech.rb