gls_agent 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTBiMzI3NDdkNzU3NGRlYjAxYjNjNzg3MjNlY2Y3MTg1YWM3Yjc1YQ==
4
+ ODI0NTc3YTlhN2NhZTcxMzcyM2QyY2NkZjg1YjY0OWUzNjI1NjgyNw==
5
5
  data.tar.gz: !binary |-
6
- MDRkNDA0ODE0ZjUwYmQ2MTlhOGJmMDc0NWIwMzgyMzUwMzIyNDdkYg==
6
+ YTcyYmEyZWEyMjgwYTE1NjNjZGNiMjBmYzg3N2M1MThkMzUwYWJhMQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzBlYjE1NDFmMDYzNTI4NWIwMDU0OTU3ZTRmY2QxZWNhMTc5OTExMzQwMWNm
10
- NWU3NTQ2ZTE5YTdjZTU5ZmI0NDM1ZTVhMGJiMWZmN2ExZWIwYTRjMjNiYmFk
11
- OTU0NmI2YmFlMzQ1YjM0ZDc1ZmRiZTE2MjY0ZDJiNGIxNDI5MjU=
9
+ ODYxMWExZGI3Y2RlZjljMDljZWZlNDIwN2Q5YWY3MjQwODM5MWNiOTQ0Y2Q2
10
+ NmM4NTUwNzIxOTE5OWE5ZTdmNDA2YWQ4YmZlNjM1ZWY2MzBjMzIyYWI0Y2Rm
11
+ ODNlNThiYTQ0OWVmNWU3YWQwZjhlYWYwYTM3ODQ2NDhhMTJlOGU=
12
12
  data.tar.gz: !binary |-
13
- NDU4ZDJkMjI4ODZjZDNlZTEwMjcxOWNjNzE3MmFjM2VmOWRkY2IxNTAzZmQ3
14
- NTEyOTA5YjEwNWY5NmZkMzg2NWI3MTA5ZTJmYjNlZDgzYmVjODQ2YjhlNGE5
15
- NWU5YWZkNTkyMzdiMjYzYTEyZjA0ZGRkYzRiY2QyZWRhNGM4NzU=
13
+ OWM3YzNmODIwMjNjYzcyMzhhM2Q5MDA5OWQwNDg0YTMxYzI4NWEwY2M1ZmUx
14
+ MDExYTI0YzUyYjMxNzczMmE2OTY2YjcxYWFiYzRmMmI4MjRiZmQzZjQwNjU3
15
+ ZDNjN2EzMDlkMTEyMzY1MDcwMWU5YzYzZWY5YTJmNzgwZTZiMjc=
data/README.md CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ### Usage as standalone tool
24
+
23
25
  There is a small standalone script:
24
26
 
25
27
  $ gls_create_label -u glsuser -p glspass -d "John Doe,Home Street,1,1234,City,1" -o output.pdf
@@ -34,6 +36,18 @@ and
34
36
 
35
37
  to play around.
36
38
 
39
+ ### Usage as lib
40
+
41
+ To use gls_agent in your ruby project, install the gem and use something along these lines:
42
+
43
+ options = GLSAgent::Dotfile::get_opts
44
+ mech = GLSMech.new
45
+ mech.user = options[:user]
46
+ mech.pass = options[:pass]
47
+
48
+ GLSAgent::ParcelJob.new('Frank Sinatra','CloudStreet','1',1234,'HeavenCity','1')
49
+ saved_as = mech.save_parcel_label parcel,'gls_label_frank_sinatra.pdf'
50
+
37
51
  ## Configuration
38
52
 
39
53
  You can invoke gls_agent without any configuration by specifying command line parameters.
data/bin/gls_create_label CHANGED
@@ -2,34 +2,25 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'gls_agent'
5
- require 'json'
6
5
 
7
6
  options = {}
7
+ optparse = OptionParser.new do |opts|
8
+ opts.banner = "Usage: gls_create_label [OPTIONS]"
9
+
10
+ opts.separator ""
11
+ opts.separator "GLS login options"
8
12
 
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,$@
13
+ opts.on('-u', '--gls-user GLSUSER', 'username of GLS account.') do |u|
14
+ options[:user] = u
15
+ end
16
+ opts.on('-p', '--gls-password GLSPASSWORD', 'password of GLS account.') do |p|
17
+ options[:pass] = p
26
18
  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
19
 
31
- optparse = OptionParser.new do |opts|
32
- opts.on('-l', '--label-data DATA', 'label data, comma-separated.') do |l|
20
+ opts.separator ""
21
+ opts.separator "Label data definition"
22
+
23
+ opts.on('-l', '--label-data DATA', 'Label data, comma-separated.') do |l|
33
24
  fields = l.split(',')
34
25
  if fields.length != 6
35
26
  STDERR.puts 'Error: label data has to be in format "John Doe,Home Street,1,1234,City,1".'
@@ -42,15 +33,21 @@ optparse = OptionParser.new do |opts|
42
33
  options[:city] = fields.shift
43
34
  options[:weight] = fields.shift
44
35
  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|
36
+
37
+ opts.separator ""
38
+ opts.separator "Output options"
39
+
40
+ opts.on('-o', '--output-file FILE', 'Write output (pdf) to FILE, defaults to gls-label.pdf .') do |o|
52
41
  options[:output_file] = o
53
42
  end
43
+
44
+ opts.separator ""
45
+ opts.separator "General options"
46
+
47
+ opts.on_tail('--version', 'Show version.') do
48
+ puts "gls_create_label #{GLSAgent::VERSION}"
49
+ exit 0
50
+ end
54
51
  opts.on('-h', '--help', 'Show help.') do
55
52
  puts opts
56
53
  exit 0
@@ -59,6 +56,9 @@ end
59
56
 
60
57
  optparse.parse!
61
58
 
59
+ defaults = GLSAgent::Dotfile::get_opts
60
+ options = default.merge options
61
+
62
62
  if !options[:user] || !options[:pass]
63
63
  STDERR.puts 'Error: Need to specify user and pass in ~/.gls_agent or -u -p .'
64
64
  exit 1
@@ -71,13 +71,19 @@ end
71
71
 
72
72
  options[:output_file] ||= 'gls-label.pdf'
73
73
 
74
- parcel = GLSAgent::ParcelJob.new(options[:name], options[:street], options[:streetno], options[:zip], options[:city], options[:weight])
74
+ parcel = GLSAgent::job_from_hash(options)
75
75
 
76
76
  mech = GLSMech.new
77
77
  mech.user = options[:user]
78
78
  mech.pass = options[:pass]
79
79
 
80
80
  pdf_file_path = mech.save_parcel_label parcel, options[:output_file]
81
+
82
+ if !pdf_file_path
83
+ STDERR.puts "Error: Could not login or navigate GLS page."
84
+ exit 2
85
+ end
86
+
81
87
  if pdf_file_path != options[:output_file]
82
88
  STDERR.puts "Warning: #{options[:output_file]} already exists, saved to #{pdf_file_path} instead!."
83
89
  else
data/gls_agent.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
25
26
  end
@@ -0,0 +1,34 @@
1
+ module GLSAgent
2
+ module Dotfile
3
+ # Get keys and values from file in ~/.gls_agent and put them in a hash.
4
+ # keys are symbols.
5
+ # The file should follow the syntax
6
+ # option=value
7
+ # option2=value2
8
+ # Meaningful keys are i.e. user and pass.
9
+ def self.get_opts
10
+ options = {}
11
+ # Get defaults from ~/.gls_agent
12
+ begin
13
+ filecontent = File.open("#{Dir.home}/.gls_agent").read
14
+ options = hash_from_text filecontent
15
+ puts "Info: read configuration parameters from ~/.gls_agent, may be overriden with cmd line options."
16
+ rescue
17
+ STDERR.puts "Info: No configuration file in ~/.gls_agent found, all options need to be specified on command line."
18
+ #STDERR.puts $!.inspect,$@
19
+ end
20
+ options
21
+ end
22
+
23
+ def self.hash_from_text text
24
+ hash = {}
25
+ text.each_line do |line|
26
+ fields = line.split('=')
27
+ fields.each &:strip!
28
+ fields.each &:rstrip!
29
+ hash[fields[0].to_sym] = fields[1]
30
+ end
31
+ hash
32
+ end
33
+ end
34
+ end
@@ -1,6 +1,7 @@
1
1
  require 'mechanize'
2
2
  require 'logger'
3
3
 
4
+ # Utilize mechanize to do stuff on GLS webpage.
4
5
  class GLSMech
5
6
  attr_accessor :mech
6
7
  attr_accessor :user
@@ -19,8 +20,10 @@ class GLSMech
19
20
 
20
21
  # Saves parcel label as pdf, does not overwrite file if exists,
21
22
  # returns filename that label was saved to.
23
+ # returns nil if login or redirection failed.
22
24
  def save_parcel_label parcel_job, filename
23
- login! @user, @pass
25
+ return nil if !login! @user, @pass
26
+
24
27
  form = @mech.page.forms.first
25
28
 
26
29
  form.field_with(:name => 'txtName1').value = parcel_job.name
@@ -37,6 +40,7 @@ class GLSMech
37
40
  private
38
41
 
39
42
  # Login to GLS parcel creation web page using provided credentials.
43
+ # returns true if login and navigation afterwards succeeded.
40
44
  def login! username, password
41
45
  target_url = 'http://www.your-gls.eu/276-I-PORTAL-WEB/content/GLS/DE03/DE/15005.htm'
42
46
  page = @mech.get target_url
@@ -46,5 +50,6 @@ class GLSMech
46
50
  form.submit
47
51
  # Move on to target page.
48
52
  page = @mech.get target_url
53
+ page.uri.to_s == target_url
49
54
  end
50
55
  end
@@ -1,3 +1,3 @@
1
1
  module GLSAgent
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/gls_agent.rb CHANGED
@@ -1,6 +1,20 @@
1
1
  require "gls_agent/version"
2
2
  require 'gls_agent/gls_mech'
3
+ require 'gls_agent/dotfile'
3
4
 
4
5
  module GLSAgent
5
6
  ParcelJob = Struct.new(:name, :street, :streetno, :zip, :city, :weight)
7
+
8
+ def self.job_from_csv string
9
+ fields = string.split(',')
10
+ if fields.length != 6
11
+ fail 'job_from_csv needs 6 fields'
12
+ return nil
13
+ end
14
+ ParcelJob.new(*fields)
15
+ end
16
+
17
+ def self.job_from_hash hash
18
+ ParcelJob.new(hash[:name], hash[:street], hash[:streetno], hash[:zip], hash[:city], hash[:weight])
19
+ end
6
20
  end
@@ -0,0 +1,41 @@
1
+ require_relative '../lib/gls_agent'
2
+
3
+ describe 'GLSAgent Module helpers' do
4
+ it 'creates ParcelJob from csv string' do
5
+ job = GLSAgent::job_from_csv 'Name,Street,1,1234,City,1'
6
+ expect(job.name).to eq 'Name'
7
+ expect(job.street).to eq 'Street'
8
+ expect(job.streetno).to eq '1'
9
+ expect(job.zip).to eq '1234'
10
+ expect(job.city).to eq 'City'
11
+ expect(job.weight).to eq '1'
12
+ end
13
+
14
+ it 'creates ParcelJob from hash' do
15
+ hash = {:name => 'Name', :street => 'Street', :streetno => '1', :zip => '1234', :city => 'City', :weight => '1'}
16
+ job = GLSAgent::job_from_hash hash
17
+ expect(job.name).to eq 'Name'
18
+ expect(job.street).to eq 'Street'
19
+ expect(job.streetno).to eq '1'
20
+ expect(job.zip).to eq '1234'
21
+ expect(job.city).to eq 'City'
22
+ expect(job.weight).to eq '1'
23
+ end
24
+ end
25
+
26
+ describe 'Dotfile-Parsing' do
27
+ it 'makes a hash from abc=def options' do
28
+ hash = GLSAgent::Dotfile::hash_from_text 'abc=def'
29
+ expect(hash[:abc]).to eq 'def'
30
+ end
31
+ it 'is tolerant about spaces' do
32
+ hash = GLSAgent::Dotfile::hash_from_text ' abc =def '
33
+ expect(hash[:abc]).to eq 'def'
34
+ end
35
+ it 'handles multiple options' do
36
+ hash = GLSAgent::Dotfile::hash_from_text " abc =def \nuser= 123\n pass = h"
37
+ expect(hash[:abc]).to eq 'def'
38
+ expect(hash[:user]).to eq '123'
39
+ expect(hash[:pass]).to eq 'h'
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gls_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Wolfsteller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-03 00:00:00.000000000 Z
11
+ date: 2014-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Log into GLS site, create and save new parcel sticker for Germany.
56
70
  email:
57
71
  - felix.wolfsteller@gmail.com
@@ -69,8 +83,10 @@ files:
69
83
  - bin/gls_create_label
70
84
  - gls_agent.gemspec
71
85
  - lib/gls_agent.rb
86
+ - lib/gls_agent/dotfile.rb
72
87
  - lib/gls_agent/gls_mech.rb
73
88
  - lib/gls_agent/version.rb
89
+ - spec/gls_agent_spec.rb
74
90
  homepage: https://github.com/fwolfst/gls_agent
75
91
  licenses:
76
92
  - MIT
@@ -95,4 +111,5 @@ rubygems_version: 2.2.2
95
111
  signing_key:
96
112
  specification_version: 4
97
113
  summary: Fetches german parcel labels from GLS webpage.
98
- test_files: []
114
+ test_files:
115
+ - spec/gls_agent_spec.rb