gls_agent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTYyODU5ZTE4NTYyZjk1OTkwMTI0OGYyNDM4M2Q5MDlkM2M1MDkxZQ==
5
+ data.tar.gz: !binary |-
6
+ NjQ4ZTdmOWVmNTA2Y2ZhZDIzMTIxYjcwNTQ2ZjA3Njk5Zjk0YjU2MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZWMwNTQwYzJhZjcxMDBiNDUzNjI1MGJiMGQ1NTU1OWI1ZjIwMzNjNTJjN2Q5
10
+ YmE4NTJiMzJjNDIwNmRmYTEzZTc0MGZmMWU4MWFlYjNmMjg0ZWJjOTY2NjNh
11
+ ZjY2ZDNjZGM2Njc1OTVjMjU3MTA4YzhkYzJhNTYwY2IzZDQ1ZjY=
12
+ data.tar.gz: !binary |-
13
+ MjUwMzVhNzYyMjk0MDcwYWY3ZmQ1ZGE2ZTIyYTZhNDgzOGI4YzM1MDNkY2Y3
14
+ NzhkMGFkM2U2NTdkNTViZTQ3ZjYzNTVhODJjMWViYjAzNDk2ZjU3N2I2MDlm
15
+ NzkzMDU2YTYxODA5MjgyMTkwNjgwMGFjMGNiOTM3N2U5YTM2YzU=
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *.swp
24
+ **/*.swp
data/.rvmrc ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p484@gls_agent"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.25.19 (stable)" # 1.10.1 seems like a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
+ " # show the user the ruby and gemset they are using in green
38
+ else printf "%b" "Using: $GEM_HOME
39
+ " # don't use colors in non-interactive shells
40
+ fi
41
+ fi
42
+ else
43
+ # If the environment file has not yet been created, use the RVM CLI to select.
44
+ rvm --create "$environment_id" || {
45
+ echo "Failed to create RVM environment '${environment_id}'."
46
+ return 1
47
+ }
48
+ fi
49
+
50
+ # If you use bundler, this might be useful to you:
51
+ # if [[ -s Gemfile ]] && {
52
+ # ! builtin command -v bundle >/dev/null ||
53
+ # builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
54
+ # }
55
+ # then
56
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
57
+ # gem install bundler
58
+ # fi
59
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
60
+ # then
61
+ # bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
62
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gls_agent.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Felix Wolfsteller
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # GLSAgent
2
+
3
+ Log into GLS (General Logistics Systems) webpage, create a new parcel sticker and save it to disk.
4
+
5
+ You'll need a "Easy-Start"/"Your GLS" account to do so.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'gls_agent'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gls_agent
20
+
21
+ ## Usage
22
+
23
+ There is a small standalone script:
24
+
25
+ $ gls_create_label -u glsuser -p glspass -d "John Doe,Home Street,1,1234,City,1" -o output.pdf
26
+
27
+ Note that while not gemified, use
28
+
29
+ $ bundle exec bin/gls_create_label ...
30
+
31
+ and
32
+
33
+ $ bundle console
34
+
35
+ to play around.
36
+
37
+ ## Configuration
38
+
39
+ You can invoke gls_agent without any configuration by specifying command line parameters.
40
+ If you do not want to retype your gls credentials or do not want them to travel the wire (ssh), be present in the history (i.e. bash) or visible in the process list, you can put credentials or delivery details in ~/.gls_agent .
41
+
42
+ Its syntax follows the
43
+
44
+ option=value
45
+
46
+ scheme, where option can be anything of: user, pass, name, street, streetno, zip, city, weight, output_file .
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/gls_agent/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'gls_agent'
5
+ require 'json'
6
+
7
+ options = {}
8
+
9
+ # Get defaults from ~/.gls_agent
10
+ begin
11
+ filecontent = File.open("#{Dir.home}/.gls_agent").read
12
+ # from option=value\noption2=value2 make {'option':'value','option2':'value2'}
13
+ filecontent.gsub!(/ *= */, ':')
14
+ filecontent.gsub!(/(\w+)/, '"\1"')
15
+ filecontent.gsub!("\n", ",\n")
16
+ filecontent.chomp!("\n")
17
+ filecontent.chomp!(',')
18
+ filecontent = "{ #{filecontent} }"
19
+ begin
20
+ j_options = JSON.parse filecontent
21
+ j_options.each {|o,v| options[o.to_sym] = v}
22
+ puts "Info: read configuration parameters from ~/.gls_agent, may be overriden with cmd line options."
23
+ rescue
24
+ STDERR.puts "Error: configuration file in ~/.gls_agent found but could not be parsed."
25
+ STDERR.puts $!.inspect,$@
26
+ end
27
+ rescue
28
+ STDERR.puts "Info: No configuration file in ~/.gls_agent found, all options need to be specified on command line."
29
+ end
30
+
31
+ optparse = OptionParser.new do |opts|
32
+ opts.on('-l', '--label-data DATA', 'label data, comma-separated.') do |l|
33
+ fields = l.split(',')
34
+ if fields.length != 6
35
+ STDERR.puts 'Error: label data has to be in format "John Doe,Home Street,1,1234,City,1".'
36
+ exit 1
37
+ end
38
+ options[:name] = fields.shift
39
+ options[:street] = fields.shift
40
+ options[:streetno] = fields.shift
41
+ options[:zip] = fields.shift
42
+ options[:city] = fields.shift
43
+ options[:weight] = fields.shift
44
+ end
45
+ opts.on('-u', '--gls-user GLSUSER', 'username of GLS account.') do |u|
46
+ options[:user] = u
47
+ end
48
+ opts.on('-p', '--gls-password GLSPASSWORD', 'password of GLS account.') do |p|
49
+ options[:pass] = p
50
+ end
51
+ opts.on('-o', '--output-file FILE', 'Write output (pdf) to FILE') do |o|
52
+ options[:output_file] = o
53
+ end
54
+ opts.on('-h', '--help', 'Show help.') do
55
+ puts opts
56
+ exit 0
57
+ end
58
+ end
59
+
60
+ optparse.parse!
61
+
62
+ if !options[:user] || !options[:pass]
63
+ STDERR.puts 'Error: Need to specify user and pass in ~/.gls_agent or -u -p .'
64
+ exit 1
65
+ end
66
+
67
+ if !options[:name] || !options[:street] || !options[:streetno] || !options[:zip] || !options[:city] || !options[:weight]
68
+ STDERR.puts 'Error: Need to specify delivery data in ~/.gls_agent or -l .'
69
+ exit 1
70
+ end
71
+
72
+ options[:output_file] ||= 'gls-label.pdf'
73
+
74
+ parcel = GLSAgent::ParcelJob.new(options[:name], options[:street], options[:streetno], options[:zip], options[:city], options[:weight])
75
+
76
+ mech = GLSMech.new
77
+ mech.user = options[:user]
78
+ mech.pass = options[:pass]
79
+
80
+ pdf_file_path = mech.save_parcel_label parcel, options[:output_file]
81
+ if pdf_file_path != options[:output_file]
82
+ STDERR.puts "Warning: #{options[:output_file]} already exists, saved to #{pdf_file_path} instead!."
83
+ else
84
+ puts "Success: Saved label to #{pdf_file_path}."
85
+ end
86
+
87
+ # Be cool, explicitely.
88
+ exit 0
data/gls_agent.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gls_agent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gls_agent"
8
+ spec.version = GlsAgent::VERSION
9
+ spec.authors = ["Felix Wolfsteller"]
10
+ spec.email = ["felix.wolfsteller@gmail.com"]
11
+ spec.summary = %q{Fetches parcel labels from GLS webpage.}
12
+ spec.description = %q{Log into GLS site, create and save new parcel sticker.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mechanize'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,50 @@
1
+ require 'mechanize'
2
+ require 'logger'
3
+
4
+ class GLSMech
5
+ attr_accessor :mech
6
+ attr_accessor :user
7
+ attr_accessor :pass
8
+
9
+ # Setup the mech.
10
+ def initialize
11
+ @mech = Mechanize.new
12
+ @mech.user_agent_alias = 'Windows Mozilla'
13
+ end
14
+
15
+ # Enable logging to given file.
16
+ def log_to filename
17
+ @mech.log = Logger.new filename
18
+ end
19
+
20
+ # Saves parcel label as pdf, does not overwrite file if exists,
21
+ # returns filename that label was saved to.
22
+ def save_parcel_label parcel_job, filename
23
+ login! @user, @pass
24
+ form = @mech.page.forms.first
25
+
26
+ form.field_with(:name => 'txtName1').value = parcel_job.name
27
+ form.field_with(:name => 'txtStreet').value = parcel_job.street
28
+ form.field_with(:name => 'txtBlockNo').value = parcel_job.streetno
29
+ form.field_with(:name => 'txtZipCodeDisplay').value = parcel_job.zip
30
+ form.field_with(:name => 'txtCity').value = parcel_job.city
31
+ form.field_with(:name => 'txtWeight').value = parcel_job.weight
32
+
33
+ @mech.submit(form, form.buttons.first)
34
+ @mech.page.iframes.first.content.save_as filename
35
+ end
36
+
37
+ private
38
+
39
+ # Login to GLS parcel creation web page using provided credentials.
40
+ def login! username, password
41
+ target_url = 'http://www.your-gls.eu/276-I-PORTAL-WEB/content/GLS/DE03/DE/15005.htm'
42
+ page = @mech.get target_url
43
+ form = page.forms.first
44
+ form.fields[5].value = username
45
+ form.fields[6].value = password
46
+ form.submit
47
+ # Move on to target page.
48
+ page = @mech.get target_url
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module GlsAgent
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gls_agent.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "gls_agent/version"
2
+ require 'gls_agent/gls_mech'
3
+
4
+ module GLSAgent
5
+ ParcelJob = Struct.new(:name, :street, :streetno, :zip, :city, :weight)
6
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gls_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Felix Wolfsteller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Log into GLS site, create and save new parcel sticker.
56
+ email:
57
+ - felix.wolfsteller@gmail.com
58
+ executables:
59
+ - gls_create_label
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rvmrc
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/gls_create_label
70
+ - gls_agent.gemspec
71
+ - lib/gls_agent.rb
72
+ - lib/gls_agent/gls_mech.rb
73
+ - lib/gls_agent/version.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Fetches parcel labels from GLS webpage.
98
+ test_files: []