ig3cmd 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/ig3cmd +252 -0
  2. metadata +61 -0
data/bin/ig3cmd ADDED
@@ -0,0 +1,252 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Client v0.1.2 by Lode
4
+
5
+ # ----------------
6
+ # Requirements:
7
+ require 'rubygems'
8
+ require 'net/http'
9
+ require 'yaml'
10
+ require 'timeout'
11
+ require 'ig3client'
12
+
13
+ # ----------------
14
+ # Items:
15
+
16
+ ITEMS = { "cola" => "5449000000996",
17
+ "colalight" => "5449000050205",
18
+ "colalightlemon" => "5449000089229",
19
+ "fantaorange" => "5449000011527",
20
+ "fantalemon" => "5449000006004",
21
+ "minutemaid" => "90494024",
22
+ "minutemaidtropical" => "5449000018243",
23
+ "nestea" => "5449000027382",
24
+ "flescola" => "54491472",
25
+ "flescolalight" => "54492387",
26
+ "flessprite" => "54491069",
27
+ "flesfantaorange" => "40822938",
28
+ "flesfantalemon" => "54492493",
29
+ "flesfantapomelo" => "42115731",
30
+ "snickers" => "40111261",
31
+ "mars" => "50159246",
32
+ "spa" => "5410013101703",
33
+ "minicalippo" => "8722700058212",
34
+ "jempyijshoorntje" => "5400141906080"
35
+ }
36
+
37
+ # ----------------
38
+ # Code
39
+
40
+ module Ig3tool
41
+
42
+ # Maak de functies publiek
43
+ module_function
44
+
45
+ class Config
46
+ @username
47
+ def initialize
48
+ # Ask userinfo if the configfile doesn't exists
49
+ _ask_userinfo unless File.exists?(File.expand_path("~/.ig3tool"))
50
+ c = YAML::load(File.open(File.expand_path('~/.ig3tool')))
51
+ @username = c.username
52
+ # Ask a password to generate a ig3token
53
+ _ask_password unless File.exists?(File.expand_path("~/.ig3token"))
54
+ end
55
+
56
+ def username
57
+ @username
58
+ end
59
+
60
+ def username=(newu)
61
+ @username = newu
62
+ # Automatically update the config file
63
+ f = File.new(File.expand_path('~/.ig3tool'),'wb')
64
+ f.write self.to_yaml
65
+ f.close
66
+ end
67
+
68
+ private
69
+
70
+ def _ask_userinfo
71
+ print "Username: "
72
+ self.username = STDIN.gets.chomp
73
+ end
74
+
75
+ def _ask_password
76
+ # Returns the password from the cmdline
77
+ print "Password: "
78
+ `stty -echo`
79
+ password = STDIN.gets.chomp ensure `stty echo`
80
+ # Newline
81
+ puts ""
82
+ begin
83
+ # Register a token
84
+ $client.wannabe!("username" => self.username, "password" => password)
85
+ rescue
86
+ puts "Error: #{$!}"
87
+ exit 1
88
+ ensure
89
+ # Clear password for faster GC (hopefuly...)
90
+ password = nil
91
+ end
92
+ end
93
+ end
94
+
95
+ def _buy(options)
96
+ begin
97
+ puts options if options.verbose
98
+ # Create array with barcodes
99
+ # Match string with Barcode-list
100
+ # Scribble expects a list with pairs
101
+ code = ITEMS[options.item]
102
+ code = options.item if code.nil?
103
+ # If it's not a valid barcode, quit
104
+ unless code =~ /^\d+$/
105
+ puts "No a valid barcode or product: #{code}"
106
+ exit 2
107
+ end
108
+ items = [[ code, options.amount ]]
109
+ username = options.username || @config.username
110
+ $client.scribble!({ :debugger => username, :items => items})
111
+ puts "ScrriiiiiiiiiblleeeDDDDDDDDDD #{options.amount} #{_plural(options.item, options.amount)} for #{username} - oh yeah"
112
+ exit # Correct executed, exit
113
+ rescue
114
+ puts "Error: #{$!}"
115
+ end
116
+ end
117
+
118
+ # Start functie
119
+ def start ()
120
+ # Connect to ig3tool
121
+ host = ENV['IG3TOOL_HOST'] || "igwe.vub.ac.be"
122
+ port = ENV['IG3TOOL_PORT'] || 2007
123
+ $client = Ig3tool::Client.new(host, port)
124
+ # Load the config
125
+ # Automatically asks for username if config file doesn't have it filled in
126
+ @config = Config.new
127
+ # Perform the parse of the arguments
128
+ options = OptparseIg3tool.parse(ARGV)
129
+ # Buy/Scribble the items if everything is ok
130
+ _buy options if options
131
+ end
132
+
133
+ private
134
+
135
+ require 'optparse'
136
+ require 'optparse/time'
137
+ require 'ostruct'
138
+
139
+ class OptparseIg3tool
140
+
141
+ #
142
+ # Return a structure describing the options.
143
+ #
144
+ def self.parse(args)
145
+ # The options specified on the command line will be collected in *options*.
146
+ # We set default values here.
147
+ options = OpenStruct.new
148
+ options.encoding = "utf8"
149
+ options.verbose = false
150
+ options.version = "v0.1"
151
+ options.item = :none
152
+ options.amount = 1
153
+
154
+ opts = OptionParser.new do |opts|
155
+
156
+ opts.banner = "\n Ig3tool cmdline (GEEK) Interface\n" +
157
+ "----------------------------------\n" +
158
+ "#{options.version} - 24/12/2007\n" +
159
+ "Usage: ig3cmdtool [options]\n"
160
+
161
+ # Actions
162
+ opts.separator ""
163
+ opts.separator "Specific options:"
164
+
165
+ opts.on("-c","--cola","Koop een cola") do |n|
166
+ options.item = "cola"
167
+ end
168
+
169
+ # with keyword completion ! =))
170
+ opts.on("-b", "--buy ITEM", String, "Koop een ITEM {#{ITEMS.keys.sort.join(',')}}") do |item|
171
+ options.item = item
172
+ end
173
+
174
+ # Boolean switch.
175
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
176
+ options.verbose = v
177
+ end
178
+
179
+ # Aantal items
180
+ opts.on("-a","--amount INTEGER", OptionParser::DecimalInteger, "Aantal items (Default 1)") do |n|
181
+ options.amount = n
182
+ end
183
+
184
+ # Username
185
+ opts.on("-u","--username STRING", String, "Scribble voor een andere user") do |u|
186
+ options.username = u
187
+ end
188
+
189
+ # Update Client -> via GEM
190
+ #opts.on("-U","--update","Update Client") do
191
+ # path = File.expand_path(__FILE__)
192
+ # puts "Executing: scp ig@igwe:/infogroep/igtool/ig3tool/client/ig3cmtool #{path}"
193
+ # puts "Need IG Password to copy file from igwe"
194
+ # `scp ig@igwe:/infogroep/igtool/ig3tool/client/ig3cmtool #{path}`
195
+ # puts "Executing: chmod 700 #{path}"
196
+ # `chmod 700 #{path}`
197
+ # puts "Ig3CmTool Update saved to #{path}!"
198
+ # exit
199
+ #end
200
+
201
+ opts.separator ""
202
+ opts.separator "Common options:"
203
+
204
+ # No argument, shows at tail. This will print an options summary.
205
+ opts.on_tail("-h", "--help", "Show this message") do
206
+ puts opts
207
+ exit
208
+ end
209
+
210
+ # Another typical switch to print the version.
211
+ opts.on_tail("-V", "--version", "Show version") do
212
+ puts options.version
213
+ exit
214
+ end
215
+ end
216
+
217
+ begin
218
+ opts.parse!(args)
219
+ if options.item == :none
220
+ puts opts
221
+ exit
222
+ else
223
+ options # Return options
224
+ end
225
+ rescue OptionParser::MissingArgument => e
226
+ puts e
227
+ rescue OptionParser::InvalidArgument => e
228
+ puts e
229
+ end
230
+
231
+ end # parse()
232
+
233
+ end # class OptparseIg3tool
234
+
235
+ def self._plural(name,number)
236
+ if number != 1 and name[-1,1] != "s"
237
+ name = name + "s"
238
+ end
239
+ name
240
+ end
241
+
242
+
243
+ end
244
+
245
+
246
+
247
+
248
+ # ----------------
249
+ # Start de commandline interface
250
+ Ig3tool::start()
251
+
252
+ # ----------------
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ig3cmd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Lode Hoste
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-02 00:00:00 +02:00
13
+ default_executable: ig3cmd
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ig3client
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.0
23
+ version:
24
+ description:
25
+ email: igtool@infogroep.be
26
+ executables:
27
+ - ig3cmd
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files: []
33
+
34
+ has_rdoc: false
35
+ homepage: http://infogroep.be
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.0.1
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: commandline ig3tool client
60
+ test_files: []
61
+