my_scripts 0.0.9 → 0.0.11
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.
- data/README.rdoc +15 -9
- data/VERSION +1 -1
- data/lib/my_scripts/citi.rb +47 -31
- data/lib/my_scripts/extensions.rb +2 -2
- data/my_scripts.gemspec +3 -4
- data/spec/my_scripts/extensions_spec.rb +12 -0
- metadata +2 -4
- data/bin/recode.rb +0 -59
data/README.rdoc
CHANGED
@@ -15,7 +15,7 @@ paths, environments and all such nonsense... It is also not OS-specific, so
|
|
15
15
|
OK, I confess: I wanted to write scripts but had neither time nor desire to learn
|
16
16
|
all the esoterics of bash, cmd/powershell and mac scripting. So, I set out to do
|
17
17
|
all my scripting in Ruby. I also wanted my scripts to be easily testable, that's
|
18
|
-
why I created a simple scripting framework instead of stuffing each script
|
18
|
+
why I created a simple scripting framework instead of stuffing each script into a
|
19
19
|
separate executable Ruby file as most people do.
|
20
20
|
|
21
21
|
These scripts are very much targeted to my own specific needs, but if you find them
|
@@ -33,16 +33,20 @@ looks like too much pain. Ruby 1.9 is the way of the future, so let's move forwa
|
|
33
33
|
== SYNOPSIS:
|
34
34
|
=== Creating your own scripts
|
35
35
|
|
36
|
-
|
36
|
+
First, you need to either fork my_scripts on github or clone it to your computer with:
|
37
|
+
|
38
|
+
$ git clone git://github.com/arvicco/my_scripts.git
|
39
|
+
|
40
|
+
Now, put your Ruby file inside lib/my_scripts. It will be auto-loaded. Use MyScripts
|
37
41
|
module as a top level namespace. Subclass Script and redefine run method to do all
|
38
42
|
actual work:
|
39
43
|
|
40
|
-
#-------- lib/my_shiny_script.rb----------
|
41
|
-
module
|
44
|
+
#-------- lib/my_scripts/my_shiny_script.rb----------
|
45
|
+
module MyScripts
|
42
46
|
class MyShinyScript < Script
|
43
47
|
def run
|
44
|
-
#
|
45
|
-
#
|
48
|
+
# Here you put all the actual code of your script.
|
49
|
+
# You have following instance vars at your disposal:
|
46
50
|
# @name - your script name,
|
47
51
|
# @argv - your ARGV Array of command line arguments,
|
48
52
|
# @cli - CLI runner (holds references to stdin and stdout)
|
@@ -63,9 +67,11 @@ Put your executable into bin directory, with something like this:
|
|
63
67
|
Run 'rake install' from project directory, and enjoy greatness of your new script!
|
64
68
|
|
65
69
|
OK, so you're not advanced enough to follow even these simple instructions. No problemo.
|
66
|
-
I
|
67
|
-
|
68
|
-
|
70
|
+
I guess you still managed to install my_scripts gem if you are reading this, so just modify it.
|
71
|
+
Locate my_scripts gem in gem directory (usually it's something like ../ruby/lib/ruby/gems/1.9.1/gems).
|
72
|
+
I have dummy script skeleton in lib/my_scripts/dummy.rb that is ready for your use. Just put your
|
73
|
+
script code inside run method (if you use ARGV, change it to @argv). Done! You can
|
74
|
+
immediately run your script anywhere in the system using the following command:
|
69
75
|
|
70
76
|
$ dummy Whatever arguments your code expects and processes
|
71
77
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.11
|
data/lib/my_scripts/citi.rb
CHANGED
@@ -6,23 +6,32 @@ module MyScripts
|
|
6
6
|
class Citi < Script
|
7
7
|
INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of input file ('File external:Ruby internal')
|
8
8
|
OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of output file ('File external:Ruby internal')
|
9
|
+
# STDOUT_ENCODING = 'CP866:UTF-8' # Encoding pair of stdout ('Stdout external:Ruby internal')
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
REPLACE = {
|
12
|
+
'Universam '=> '',
|
13
|
+
'Supermarket '=> '',
|
14
|
+
'Pokupka Masterkard ' => '',
|
15
|
+
/.*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
|
16
|
+
/.*(Legioner).*/ => "Legioner\nLBooks & Media:DVDs",
|
17
|
+
/.*(Gudman).*/ => "Goodman\nLDining",
|
18
|
+
/.*(Sushi Vesla).*/ => "\\1\nLDining",
|
19
|
+
/.*(Starbucks).*/ => "\\1\nLDining",
|
20
|
+
/.*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
|
21
|
+
/.*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
|
22
|
+
/.*(Alye Parusa).*/ => "\\1\nLGroceries",
|
23
|
+
/.*(Perekrestok).*/ => "\\1\nLGroceries",
|
24
|
+
/.*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
|
25
|
+
/.*(Beeline).*/ => "\\1\nLCommunications:Telephone",
|
26
|
+
/(.+) (Moscow Ru.*|Moskva Ru.*)/ => "\\1\nM\\2\nLHousehold",
|
27
|
+
/Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM 2yr Prestige]",
|
28
|
+
/Platezh Cherez Citibank Online/ => "Incoming transfer\nL[Citi RUB]",
|
20
29
|
/Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
|
21
30
|
/Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
|
22
31
|
/Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
|
23
32
|
/Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
|
24
|
-
|
25
|
-
|
33
|
+
/Komissiya Za Snyatie Nalichnykh/ => "Citibank\nMCash withdrawal fee\nLFinance:Bank Charge"
|
34
|
+
|
26
35
|
}
|
27
36
|
|
28
37
|
def run
|
@@ -31,26 +40,33 @@ module MyScripts
|
|
31
40
|
in_file = @argv.first
|
32
41
|
|
33
42
|
# If 2nd Arg is given, it is out_file name, otherwise derive from in_file name
|
34
|
-
out_file = @argv[1] ? @argv[1] : in_file.sub(/\.qif
|
35
|
-
|
36
|
-
# All the other args lumped into message, or default message
|
37
|
-
message = @argv.empty? ? 'Commit' : @argv.join(' ')
|
38
|
-
# Timestamp added to message
|
39
|
-
message += " #{Time.now.to_s[0..-6]}"
|
40
|
-
|
41
|
-
puts "Committing (versionup =#{bump}) with message: #{message}"
|
43
|
+
out_file = @argv[1] ? @argv[1] : in_file.sub(/\.qif|$/i, '_out.qif')
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
# $stdout.set_encoding(STDOUT_ENCODING, :undef=>:replace)
|
46
|
+
File.open(out_file, 'w:'+ (OUTFILE_ENCODING), :undef => :replace) do |outfile|
|
47
|
+
File.open(in_file, 'r:'+ (INFILE_ENCODING), :undef => :replace).each_line do |line|
|
48
|
+
type = line[0]
|
49
|
+
text = line[1..-1]
|
50
|
+
case type # Indicates type of field
|
51
|
+
when 'D' # Date field - convert to MM/DD/YYYY format expected by Quicken
|
52
|
+
text.gsub! /(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3'
|
53
|
+
when 'P' # Payee field
|
54
|
+
# Convert payee from Cyrillic to Latin translit
|
55
|
+
text.translit!
|
56
|
+
# Capitalize each word and remove extra whitespaces (leaves first char intact)
|
57
|
+
text = text.scan(/[\w&]+/).map{|word| word.capitalize}.join(' ')
|
58
|
+
# Preprocess Payee field making pre-defined replacements
|
59
|
+
REPLACE.each {|key, value| text.gsub!(key, value)}
|
60
|
+
when 'M' # Memo field - drop if empty
|
61
|
+
if text.rstrip.empty?
|
62
|
+
text.clear
|
63
|
+
type.clear
|
64
|
+
end
|
65
|
+
end
|
66
|
+
line = type+text
|
67
|
+
outfile.puts line unless line.empty?
|
68
|
+
end
|
51
69
|
end
|
52
|
-
system %Q[git commit -a -m "#{message}" --author arvicco]
|
53
|
-
system %Q[git push]
|
54
70
|
end
|
55
71
|
end
|
56
|
-
end
|
72
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
class String
|
4
|
-
TRANSLIT_CYRILLIC =
|
5
|
-
TRANSLIT_LATIN =
|
4
|
+
TRANSLIT_CYRILLIC = %Q{АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ}
|
5
|
+
TRANSLIT_LATIN = %Q{ABVGDEEZIJKLMNOPRSTUF"Y'Eabvgdeezijklmnoprstuf"y'e}
|
6
6
|
TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
|
7
7
|
'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
|
8
8
|
|
data/my_scripts.gemspec
CHANGED
@@ -5,14 +5,14 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{my_scripts}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.11"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["arvicco"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-13}
|
13
13
|
s.description = %q{Simple framework for writing command-line scripts and collection of my own scripts (mostly dev-related)}
|
14
14
|
s.email = %q{arvitallian@gmail.com}
|
15
|
-
s.executables = ["citi", "dummy", "gitto", "jew", "mybones", "rabbit"
|
15
|
+
s.executables = ["citi", "dummy", "gitto", "jew", "mybones", "rabbit"]
|
16
16
|
s.extra_rdoc_files = [
|
17
17
|
"LICENSE",
|
18
18
|
"README.rdoc"
|
@@ -30,7 +30,6 @@ Gem::Specification.new do |s|
|
|
30
30
|
"bin/jew",
|
31
31
|
"bin/mybones",
|
32
32
|
"bin/rabbit",
|
33
|
-
"bin/recode.rb",
|
34
33
|
"features/my_scripts.feature",
|
35
34
|
"features/step_definitions/my_scripts_steps.rb",
|
36
35
|
"features/support/env.rb",
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
4
|
|
3
5
|
module MyScriptsTest
|
@@ -45,5 +47,15 @@ module MyScriptsTest
|
|
45
47
|
"::MyScriptsTest::A::B::C".to_class.should == MyScriptsTest::A::B::C
|
46
48
|
end
|
47
49
|
end
|
50
|
+
|
51
|
+
context '#translit!' do
|
52
|
+
it 'converts string from Cyrillic to Latin translit' do
|
53
|
+
"Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства".translit!.
|
54
|
+
should == "SHirokaya elektrifikatsiya yuzhnykh gubernij dast moshchnyj tolchok pod\"emu sel'skogo khozyajstva"
|
55
|
+
"ШИРОКАЯ ЭЛЕКТРИФИКАЦИЯ ЮЖНЫХ ГУБЕРНИЙ ДАСТ МОЩНЫЙ ТОЛЧОК ПОДЪЁМУ СЕЛЬСКОГО ХОЗЯЙСТВА".translit!.
|
56
|
+
should == "SHIROKAYA ELEKTRIFIKATSIYA YUZHNYKH GUBERNIJ DAST MOSHCHNYJ TOLCHOK POD\"EMU SEL'SKOGO KHOZYAJSTVA"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
48
60
|
end
|
49
61
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_scripts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- arvicco
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-13 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -41,7 +41,6 @@ executables:
|
|
41
41
|
- jew
|
42
42
|
- mybones
|
43
43
|
- rabbit
|
44
|
-
- recode.rb
|
45
44
|
extensions: []
|
46
45
|
|
47
46
|
extra_rdoc_files:
|
@@ -60,7 +59,6 @@ files:
|
|
60
59
|
- bin/jew
|
61
60
|
- bin/mybones
|
62
61
|
- bin/rabbit
|
63
|
-
- bin/recode.rb
|
64
62
|
- features/my_scripts.feature
|
65
63
|
- features/step_definitions/my_scripts_steps.rb
|
66
64
|
- features/support/env.rb
|
data/bin/recode.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
INFILE_NAME = 'ACCT_101.QIF'
|
3
|
-
OUTFILE_NAME = 'TEST.QIF'
|
4
|
-
INFILE_ENCODING = 'CP1251:UTF-8' # Encoding pair of log file ('File external:Ruby internal')
|
5
|
-
OUTFILE_ENCODING = 'CP1252:UTF-8' # Encoding pair of log file ('File external:Ruby internal')
|
6
|
-
#STDOUT_ENCODING = 'CP866:UTF-8' # Encoding pair of stdout ('Stdout external:Ruby internal')
|
7
|
-
REPLACES = {
|
8
|
-
/Pokupka Masterkard .*(Detskiy Mir).*/ => "Detski Mir\nLChildcare",
|
9
|
-
/Pokupka Masterkard .*(Medicina).*/ => "AO Medicina\nLMedical:Medicine",
|
10
|
-
/Pokupka Masterkard .*(Pharmacy).*/ => "\\1\nLMedical:Medicine",
|
11
|
-
/Pokupka Masterkard .*(Alye Parusa).*/ => "\\1\nLGroceries",
|
12
|
-
/Pokupka Masterkard .*(Perekrestok).*/ => "\\1\nLGroceries",
|
13
|
-
/Pokupka Masterkard .*(Ile De Beaute).*/ => "\\1\nLPersonal Care",
|
14
|
-
/Pokupka Masterkard .*(Beeline).*/ => "\\1\nLCommunications:Telephone",
|
15
|
-
/Pokupka Masterkard (.+) (Moscow Ru.+|Moskva Ru.+)/ => "\\1\nM\\2\nLHousehold",
|
16
|
-
/Vkhodyashchij Platezh(.+Bank Moskvy)/ => "Incoming transfer\nM\\1\nL[BM New Prestige]",
|
17
|
-
/Oplata Dolg Kr Karta(.+)/ => "Transfer to Credit card\nM\\1\nL[Citi MC]",
|
18
|
-
/Snyatie Nalichnykh(.+)/ => "Cash withdrawal\nM\\1\nL[Cash RUB]",
|
19
|
-
/Vznos Nalichnykh(.+)/ => "Cash deposit\nM\\1\nL[Cash RUB]",
|
20
|
-
/Kom Za Obsluzhivanie/ => "Citibank\nMService fee\nLFinance:Bank Charge",
|
21
|
-
'Universam '=> '',
|
22
|
-
'Supermarket '=> ''
|
23
|
-
}
|
24
|
-
|
25
|
-
class String
|
26
|
-
TRANSLIT_RUSSIAN = 'АБВГДЕЁЗИЙКЛМНОПРСТУФЪЫЬЭабвгдеёзийклмнопрстуфъыьэ'
|
27
|
-
TRANSLIT_LATIN = "ABVGDEEZIJKLMNOPRSTUF\"Y'Eabvgdeezijklmnoprstuf\"y'e"
|
28
|
-
TRANSLIT_DOUBLES = {'Ж'=>'ZH', 'Х'=>'KH', 'Ц'=>'TS', 'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHCH', 'Ю'=>'YU', 'Я'=>'YA',
|
29
|
-
'ж'=>'zh', 'х'=>'kh', 'ц'=>'ts', 'ч'=>'ch', 'ш'=>'sh', 'щ'=>'shch', 'ю'=>'yu', 'я'=>'ya'}
|
30
|
-
|
31
|
-
def translit!
|
32
|
-
TRANSLIT_DOUBLES.each {|key, value| self.gsub!(key, value)}
|
33
|
-
self.tr!(TRANSLIT_RUSSIAN, TRANSLIT_LATIN)
|
34
|
-
self
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
#$stdout.set_encoding(STDOUT_ENCODING, :undef=>:replace)
|
39
|
-
File.open(OUTFILE_NAME, 'w:'+ (OUTFILE_ENCODING), :undef => :replace) do |outfile|
|
40
|
-
File.open(INFILE_NAME, 'r:'+ (INFILE_ENCODING), :undef => :replace).each_line do |line|
|
41
|
-
puts line, '-----------'
|
42
|
-
case line.chars.first
|
43
|
-
when 'D' # Date field - convert to MM/DD/YYYY format expected by Quicken
|
44
|
-
line.gsub! /(\d+)\/(\d+)\/(\d+)/, '\2/\1/\3'
|
45
|
-
when 'P' # Payee field
|
46
|
-
# Convert payee from Russian to translit
|
47
|
-
line.translit!
|
48
|
-
# Capitalize each word and remove extra whitespaces (leaves first char intact)
|
49
|
-
line[1..-1] = line[1..-1].scan(/\w+/).map{|word| word.capitalize}.join(' ')
|
50
|
-
# Preprocess Payee field making pre-defined replacements
|
51
|
-
REPLACES.each {|key, value| line.gsub!(key, value)}
|
52
|
-
when 'M' # Memo field - drop if empty
|
53
|
-
line.clear if line.rstrip == 'M'
|
54
|
-
end
|
55
|
-
|
56
|
-
outfile.puts line unless line.empty?
|
57
|
-
puts line, '-----------'
|
58
|
-
end
|
59
|
-
end
|