Class: Rubyment

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyment.rb

Overview

Collection of Ruby functions

Output

normally outputs to STDERR, with no mercy STDOUT, just qualified output: only if the function is expected to output something

Instance Method Summary collapse

Constructor Details

#initialize(memory = {}) ⇒ Rubyment

Returns a new instance of Rubyment.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubyment.rb', line 56

def initialize memory = {}
  @memory = {
    :invoke => [],
    :stderr => STDERR,
    :stdout => STDOUT,
    :stdin  => STDIN,
    :time   => Time.now,
    :major_version => "0.4",
    :basic_version => (Time.now.to_i  / 60), # new one every minute
    :filepath => __FILE__,
    :running_dir => Dir.pwd,
    :home_dir => Dir.home,
    :system_user => ENV['USER'] || ENV['USERNAME'],
    :system_user_is_super => ENV['USER']  == "root", # changed plan: platform indenpend.
    :static_separator_key => "strings_having_this_string_not_guaranteed_to_work",
    :static_end_key => "strings_havinng_this_string_also_not_guaranteed_to_work",
  }
  @memory.update memory.to_h
  invoke @memory[:invoke].to_a
end

Instance Method Details

#array_first_remainder(args = ARGV) ⇒ Object

this class very often needs to split first argument and remaining elements. args: args default: ARGV returns:

args, args[1..-1

]



20
21
22
# File 'lib/rubyment.rb', line 20

def array_first_remainder args=ARGV
  [ args[0], args[1..-1] ]
end

#containerize(args = ARGV) ⇒ Object

returns a Class object out of class_name (or itself if it is already a class)



112
113
114
# File 'lib/rubyment.rb', line 112

def containerize args=ARGV
  [args].flatten 1
end

#dec(args = ARGV) ⇒ String

decipher the data encoded by enc

password
String, nil

password to be used to encryption.

base64_iv
String, nil

initialization vectors encoded with Base64

base64_encrypted
String, nil

ciphered data (without metadata) encoded with Base64

ending
nil

deprecated

base64_salt
String, nil

#generate_pbkdf2_key salt encoded with Base64

base64_iter
String, nil

#generate_pbkdf2_key iterations encoded with Base64

Parameters:

  • args, (Array)

    an Array whose elements are expected to be:

Returns:

  • (String)

    decoded data



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/rubyment.rb', line 445

def dec args=ARGV
  require 'openssl'
  require 'base64'
  memory = @memory
  static_end_key = memory[:static_end_key]
  password, base64_iv, base64_encrypted, ending, base64_salt, base64_iter = args
  salt = Base64.decode64 base64_salt
  iter = Base64.decode64 base64_iter
  ending = ending.to_s.split("\0").first || static_end_key
  key, password, salt, iter =  (
    generate_pbkdf2_key [password, salt, iter]
  )|| [nil, password, salt, iter]

  decipher = OpenSSL::Cipher.new('aes-128-cbc')
  decipher.decrypt
  decipher.padding = 0

  decipher.key = key || (Digest::SHA256.hexdigest password)
  decipher.iv = Base64.decode64 base64_iv
  plain = decipher.update(Base64.decode64 base64_encrypted) + decipher.final
  # split is not the ideal, if ever ending is duplicated it won't
  # work. also may be innefficient.
  (plain.split ending).first

end

#dec_interactive(args = ARGV) ⇒ TrueClass, FalseClass

an alternative interface to dec – reads password if nil or empty.

iv
String, nil

If empty or nil, read (without echo) from @memory, which defaults to STDIN

password
String, nil

password to be used to encryption.

If empty or nil, read (without echo) from @memory, which defaults to STDIN

Parameters:

  • args, (Array)

    an Array whose elements are expected to be:

Returns:

  • (TrueClass, FalseClass)

    depending on whether test succeeds.



781
782
783
784
785
786
787
788
# File 'lib/rubyment.rb', line 781

def dec_interactive args=ARGV
  stderr = @memory[:stderr]
  iv, encrypted, base64_salt, base64_iter, password  = args
  stderr.print "[password]"
  password = (input_single_line_non_echo [password])
  stderr.puts
  dec [password, iv, encrypted, nil, base64_salt, base64_iter]
end

#deserialize_json_metadata(args = ARGV) ⇒ Object

deserialize_json_metadata args:

serialized_string (String), separator (String)

undo what serialize_json_metadata returns array:

payload (String), metadata (Hash or String), separator (String)

metadata is returned as Hash after a JSON.parse, but in case of any failure, it returns the String itself.



709
710
711
712
713
714
715
716
717
718
# File 'lib/rubyment.rb', line 709

def  args=ARGV
  require 'json'
  memory = @memory
  static_separator = memory[:static_separator_key]
  serialized_string, separator = args
  separator ||= static_separator
  , payload  = serialized_string.to_s.split separator
   = (JSON.parse ) rescue 
  [payload, , separator]
end

#enc(args = ARGV) ⇒ Object

encode data into aes-128-cbc cipher protected by a key generated by #generate_pbkdf2_key, using given password, salt, iter

password
String, nil

password to be used to encryption.

data
String, nil

data to be encoded data

ending
nil

deprecated

salt
String, nil

#generate_pbkdf2_key salt argument

iter
String, nil

#generate_pbkdf2_key iterations argument

base64_encrypted
String, nil

ciphered data (without metadata) encoded with Base64

base64_iv
String

initialization vectors encoded with Base64

base64_salt
String

#generate_pbkdf2_key salt encoded with Base64

base64_iter
String

#generate_pbkdf2_key iterations encoded with Base64

base64_key
String

#generate_pbkdf2_key return value

Parameters:

  • args, (Array)

    an Array whose elements are expected to be:



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/rubyment.rb', line 568

def enc args=ARGV
  require 'openssl'
  require 'base64'
  memory = @memory
  static_end_key = memory[:static_end_key]
  password, data, ending, salt, iter = args
  ending ||= static_end_key
  key, password, salt, iter = (
    generate_pbkdf2_key [password, salt, iter]
  )|| [nil, password, salt, iter]

  cipher = OpenSSL::Cipher.new('aes-128-cbc')
  cipher.encrypt

  cipher.key = key || (Digest::SHA256.hexdigest password)
  iv = cipher.random_iv
  encrypted = cipher.update(data + ending) + cipher.final

  base64_iv = Base64.encode64 iv
  base64_encrypted = Base64.encode64  encrypted
  base64_salt = Base64.encode64 salt.to_s
  base64_iter = Base64.encode64 iter.to_s
  base64_key = Base64.encode64 key.to_s

  [base64_encrypted, base64_iv,  base64_salt, base64_iter, base64_key]
end

#expect_equal(args = ARGV) ⇒ Object

expect_equal args:

value_before (Object), value_after (Object), value_label (String)

returns the value of testing value_before == value_after, printing to stderr upon failure



751
752
753
754
755
756
757
758
# File 'lib/rubyment.rb', line 751

def expect_equal args=ARGV
  memory = @memory
  stderr = memory[:stderr]
  value_before, value_after, value_label = args
  judgement = (value_before == value_after)
  (!judgement) && (stderr.puts  expect_format_string ["unexpected", value_before, value_after, value_label, "!="])
  judgement
end

#expect_format_string(args = ARGV) ⇒ Object

format strings for expect_format_string



738
739
740
741
742
743
# File 'lib/rubyment.rb', line 738

def expect_format_string args=ARGV
  memory = @memory
  debug = memory[:debug]
  prepend_text, value_before, value_after, value_label, comparison_text = args
  "#{prepend_text} #{value_label}: #{value_before} #{comparison_text} #{value_after}"
end

#file_backup(file = __FILE__, dir = '/tmp/', append = ('-' + Time.now.hash.abs.to_s), prepend = '/') ⇒ Object

if file is a nonexisting filepath, or by any reason throws any exception, it will be treated as contents instead, and the filename will treated as “” file can be a url, if ‘open-uri’ is available.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rubyment.rb', line 121

def file_backup file = __FILE__ , dir = '/tmp/', append = ('-' + Time.now.hash.abs.to_s), prepend='/'
  stderr = @memory[:stderr]
  debug  = @memory[:debug]
  (require 'open-uri') && open_uri = true
  require 'fileutils'
  file_is_filename = true
  open_uri && (
    contents = open(file).read rescue  (file_is_filename = false) || file
  ) || (
    contents = File.read file rescue  (file_is_filename = false) || file
  )
  debug && (stderr.puts "location = dir:#{dir} + prepend:#{prepend} + (#{file_is_filename} && #{file} || '' ) + #{append}")
  location = dir + prepend + (file_is_filename && file || '' ) + append
  debug && (stderr.puts "FileUtils.mkdir_p File.dirname #{location}")
  FileUtils.mkdir_p File.dirname location # note "~" doesn't work
  File.write location, contents
  contents
end

#file_permissions_octal(path) ⇒ Object

args: path (String) returns: file_permissions_octal (Integer, ready to go to chmod, or nil, if file doesn’t exist)



205
206
207
# File 'lib/rubyment.rb', line 205

def file_permissions_octal path
  File.stat(path).mode.to_s(8).split("")[-4..-1].join.to_i(8) rescue nil
end

#filepath_or_contents(file, contents = "") ⇒ Object

returns the contents of file (or empty, or a default if a second parameter is given). if file is a nonexisting filepath, or by any reason throws any exception, it will be treated as contents instead file can be a url, if ‘open-uri’ is available.



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubyment.rb', line 245

def filepath_or_contents file, contents = ""
  stderr = @memory[:stderr]
  (require 'open-uri') && open_uri = true
  require 'fileutils'
  file = file.to_s
  file_is_filename = true
  open_uri && (
    contents = open(file).read rescue  (file_is_filename = false) || file
  ) || (
    contents = File.read file rescue  (file_is_filename = false) || file
  )
  contents
end

#gem_build(args = ARGV) ⇒ Object

gem_build args:

gem_spec_path (String), gem_spec_contents (String), gem_is_current_file, gem_name

returns: console output of gem build (String)



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/rubyment.rb', line 1034

def gem_build args=ARGV
  gem_spec_path, gem_spec_contents, gem_is_current_file, gem_name  = args
  require 'fileutils'

  # this supposes that  the current file is listed by the
  # s.files
  # field of the specification. it is not currently checked.
  gem_is_current_file && (
    FileUtils.mkdir_p 'lib'
    file_backup "lib/#{gem_name}.rb", "lib/"
    save_file __FILE__, "lib/#{gem_name}.rb"
  )

  FileUtils.mkdir_p File.dirname gem_spec_path
  File.write gem_spec_path, gem_spec_contents || (File.read gem_spec_path)
  `gem build #{gem_spec_path}`
end

#gem_build_push(args = ARGV) ⇒ Object

builds, validates and push a gem accordingly to the arguments. if arguments not given, will do for the defaults (see rubyment_gem_defaults) args: args (Array or nil) returns: ignore (will change)



1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'lib/rubyment.rb', line 1323

def gem_build_push args=ARGV
  memory = @memory
  running_dir      = memory[:running_dir]
  home_dir         = memory[:home_dir]
  gem_username, gem_password, gem_api_key_file, gem_defaults = args
  gem_password = gem_password.to_s.split("\0").first
  gem_defaults ||=  rubyment_gem_defaults []
  gem_api_key_file ||= "#{home_dir}/.gem/credentials"
  permissions = file_permissions_octal gem_api_key_file
  credentials_contents = url_to_str gem_api_key_file, ""
  (gem_get_api_key [gem_username, gem_password, gem_api_key_file]) rescue nil
  validated = (
    gem_validate gem_defaults
  )
  puts validated && (gem_push gem_path gem_defaults )
  File.write gem_api_key_file, credentials_contents
  File.chmod permissions, gem_api_key_file
end

#gem_files_args(args = ARGV) ⇒ Object

extract only the arguments referring to files from args



1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
# File 'lib/rubyment.rb', line 1149

def gem_files_args args=ARGV
  gem_name,
  gem_version,
  gem_dir,
  gem_ext,
  gem_hifen,
  gem_date,
  gem_summary,
  gem_description,
  gem_authors,
  gem_email,
  gem_files,
  gem_homepage,
  gem_license,
  gem_validate_class,
  gem_validate_class_args,
  gem_validate_class_method,
  gem_is_current_file = args
  [
      gem_files,
      gem_is_current_file,
  ]
end

#gem_get_api_key(args = ARGV) ⇒ Object

gets the api key needed to push gems to rubygems. prompts for arguments when username or password not provided. args: [username (String or nil), password (String or nil), file_destination (String, “/dev/null” if empty/nil given)] returns key_contents (String)



1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
# File 'lib/rubyment.rb', line 1289

def gem_get_api_key args=ARGV
  stderr = @memory[:stderr]
  require 'fileutils'
  username, password, file_destination = args
  stderr.print "username - "
  username = input_single_line [username]
  stderr.print "password - "
  password = input_single_line_non_echo [password]
  file_destination = file_destination.to_s.split("\0").first || "/dev/null"
  FileUtils.mkdir_p File.dirname file_destination
  key_contents = save_file "https://rubygems.org/api/v1/api_key.yaml",
    file_destination, 0, {:username => username, :password => password }
  (File.chmod 0600, file_destination)
  key_contents
end

#gem_install(args = ARGV) ⇒ Object

gem_install args:

gem_spec (String)

returns: console output of gem install (String)



1074
1075
1076
1077
1078
1079
# File 'lib/rubyment.rb', line 1074

def gem_install args=ARGV
  system_user_is_super = @memory[:system_user_is_super]
  gem_spec, user_install = args
  user_install ||= (!system_user_is_super) && "--user-install" || ""
  `gem install #{user_install}  #{gem_spec}`
end

#gem_list(args = ARGV) ⇒ Object

gem_list args:

gem_spec (String)

returns: console output of gem install (String)



1108
1109
1110
1111
# File 'lib/rubyment.rb', line 1108

def gem_list args=ARGV
  gem_spec, future_arg = args
  `gem list | grep #{gem_spec}`
end

#gem_path(args = ARGV) ⇒ Object

returns the gem path given the params. args:

gem_name, gem_version, gem_dir, gem_ext, gem_hifen

all Strings. defaults: [“rubyment”, version [], memory, “.gem”, “-”]



1023
1024
1025
1026
# File 'lib/rubyment.rb', line 1023

def gem_path args=ARGV
  gem_name, gem_version, gem_dir, gem_ext, gem_hifen = rubyment_gem_defaults args
  "#{gem_dir}/#{gem_name}#{gem_hifen}#{gem_version}#{gem_ext}"
end

#gem_push(args = ARGV) ⇒ Object

gem_push args:

gem_spec (String)

returns: console output of gem push (String)



1086
1087
1088
1089
# File 'lib/rubyment.rb', line 1086

def gem_push args=ARGV
  gem_spec, future_arg = args
  `gem push #{gem_spec}`
end

#gem_spec(args = ARGV) ⇒ Object

gem_spec args (Array like the one returned by rubyment_gem_defaults) returns: a gem spec string accordingly to args



883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/rubyment.rb', line 883

def gem_spec args=ARGV
  memory = @memory
  gem_name,
  gem_version,
  gem_dir,
  gem_ext,
  gem_hifen,
  gem_date,
  gem_summary,
  gem_description,
  gem_authors,
  gem_email,
  gem_files,
  gem_homepage,
  gem_license,
  gem_validate_class,
  gem_validate_class_args,
  gem_validate_class_method = args

  contents =<<-ENDHEREDOC
Gem::Specification.new do |s|
s.name        = '#{gem_name}'
s.version     = '#{gem_version}'
s.date        = '#{gem_date}'
s.summary     = '#{gem_summary}'
s.description = '#{gem_description}'
s.authors     = #{gem_authors.inspect}
s.email       = '#{gem_email}'
s.files       = #{gem_files.inspect}
s.homepage    = '#{gem_homepage}'
s.license     = '#{gem_license}'
end
  ENDHEREDOC
  contents
end

#gem_uninstall(args = ARGV) ⇒ Object

gem_uninstall args:

gem_spec (String)

returns: console output of gem uninstall (String)



1096
1097
1098
1099
1100
1101
# File 'lib/rubyment.rb', line 1096

def gem_uninstall args=ARGV
  system_user_is_super = @memory[:system_user_is_super]
  gem_spec, user_install = args
  user_install ||= (!system_user_is_super) && "--user-install" || ""
  `gem uninstall #{user_install}  #{gem_spec}`
end

#gem_validate(args = ARGV) ⇒ Object

validate the installation of a gem args rubyment_gem_defaults but planned to change. bug detected: require x won’t reload the gem. args (Array just like the one returned by rubyment_gem_defaults) returns: true or false



1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
# File 'lib/rubyment.rb', line 1224

def gem_validate args=ARGV
  memory = @memory
  gem_defaults = rubyment_gem_defaults args
  gem_name, gem_version = gem_defaults
  gem_files, gem_is_current_file = gem_files_args gem_defaults
  puts gem_build [
    "#{gem_name}.spec",
    gem_spec(gem_defaults),
    gem_is_current_file,
    gem_name
  ]
  already_installed = (
    validate_require gem_validate_args gem_defaults
  )
  sleep 1
  already_installed && (gem_uninstall [gem_name])
  puts gem_list [gem_name]
  p (gem_path [gem_name, gem_version])
  gem_install [(gem_path [gem_name, gem_version])]
  puts gem_list [gem_name]
  v = (
    validate_require gem_validate_args gem_defaults
  )
  gem_uninstall [gem_name]
  already_installed && (gem_install [gem_name])
  v
end

#gem_validate_args(args = ARGV) ⇒ Object

extract only the arguments for validation from args



1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/rubyment.rb', line 1176

def gem_validate_args args=ARGV
  gem_name,
  gem_version,
  gem_dir,
  gem_ext,
  gem_hifen,
  gem_date,
  gem_summary,
  gem_description,
  gem_authors,
  gem_email,
  gem_files,
  gem_homepage,
  gem_license,
  gem_validate_class,
  gem_validate_class_args,
  gem_validate_class_method,
  gem_is_current_file = args
  [
      gem_name,
      gem_validate_class,
      gem_validate_class_args,
      gem_validate_class_method,
  ]
end

#generate_pbkdf2_key(args = ARGV) ⇒ Object

generates (by default) a 128 bit key for a Cipher (e.g. AES) args:

password, salt, iter, key_len

returns:

key, password, salt, iter, key_len

planned changes:

ensure that random_bytes == key_len


423
424
425
426
427
428
429
430
431
# File 'lib/rubyment.rb', line 423

def generate_pbkdf2_key args=ARGV
  require 'openssl'
  password, salt, iter, key_len = args
  iter = (iter.to_i > 0) && iter.to_i || 20000
  key_len = (salt.to_s.split("\0").first && salt.to_s.size > 0 && salt.size || 16)
  salt = salt.to_s.split("\0").first || OpenSSL::Random.random_bytes(key_len)
  key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(password.to_s, salt.to_s, iter.to_i, key_len.to_i)
  [key, password, salt, iter, key_len]
end

#input_multi_line(args = ARGV) ⇒ Object

opens an echoing multiline prompt, if arg1 is nil or empty args:

arg1 (String or nil)


306
307
308
309
310
311
# File 'lib/rubyment.rb', line 306

def input_multi_line args=ARGV
  stderr = @memory[:stderr]
  stdin  = @memory[:stdin]
  stderr.print "multiline[enter + control-D to stop]:"
  args.shift.to_s.split("\0").first || stdin.readlines.join
end

#input_multi_line_non_echo(args = ARGV) ⇒ Object

opens a non-echoing multiline prompt, if arg1 is nil or empty args:

arg1 (String or nil)


317
318
319
320
321
322
323
# File 'lib/rubyment.rb', line 317

def input_multi_line_non_echo args=ARGV
  stderr = @memory[:stderr]
  stdin  = @memory[:stdin]
  require "io/console"
  stderr.print "multiline[enter + control-D to stop]:"
  args.shift.to_s.split("\0").first || stdin.noecho{ stdin.readlines}.join.chomp
end

#input_non_empty_filepath_or_contents_or_multiline_prompt(args = ARGV) ⇒ Object

returns the filepath_or_contents of the first value of args if it is a non empty string, or prompt for a multi line string. useful for reading file contents, e.g.



273
274
275
276
277
# File 'lib/rubyment.rb', line 273

def input_non_empty_filepath_or_contents_or_multiline_prompt args=ARGV
  stderr = @memory[:stderr]
  stderr.print "multiline[enter + control-D to stop]:"
  (filepath_or_contents args.shift).to_s.split("\0").first ||  readlines.join
end

#input_non_empty_string_or_multiline_prompt(args = ARGV) ⇒ Object

returns the first value of args if it is a non empty string, or prompt for a multi line string. useful for reading file contents, e.g.



262
263
264
265
266
# File 'lib/rubyment.rb', line 262

def input_non_empty_string_or_multiline_prompt args=ARGV
  stderr = @memory[:stderr]
  stderr.print "multiline[enter + control-D to stop]:"
  args.shift.to_s.split("\0").first ||  readlines.join
end

#input_shift(args = ARGV) ⇒ Object



330
331
332
# File 'lib/rubyment.rb', line 330

def input_shift args=ARGV
  args.shift
end

#input_shift_or_empty_string(args = ARGV, default = '') ⇒ Object



326
327
328
# File 'lib/rubyment.rb', line 326

def input_shift_or_empty_string args=ARGV, default = ''
  args.shift || default
end

#input_single_line(args = ARGV) ⇒ Object

opens an echoing prompt, if arg1 is nil or empty args:

arg1 (String or nil)


283
284
285
286
287
288
# File 'lib/rubyment.rb', line 283

def input_single_line args=ARGV
  stderr = @memory[:stderr]
  stdin  = @memory[:stdin]
  stderr.print "single line:"
  args.shift.to_s.split("\0").first || stdin.gets.chomp
end

#input_single_line_non_echo(args = ARGV) ⇒ Object

opens a non-echoing prompt, if arg1 is nil or empty args:

arg1 (String or nil)


294
295
296
297
298
299
300
# File 'lib/rubyment.rb', line 294

def input_single_line_non_echo args=ARGV
  stderr = @memory[:stderr]
  stdin  = @memory[:stdin]
  require "io/console"
  stderr.print "non echo single line:"
  args.shift.to_s.split("\0").first || stdin.noecho{ stdin.gets}.chomp
end

#invoke(args = ARGV) ⇒ Object

invoke first arg with following args used by initialize



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubyment.rb', line 27

def invoke args=ARGV
  stderr = @memory[:stderr]
  method_name, arg_list = array_first_remainder args
  !method_name && (return true)
  begin
    # this condition could become ambiguous only when
    # a function has only one argument (if it has
    # more, the rescue case would fail anyway. if
    # it has zero, both are equally OK).
    # In only one argument case, it is better not to
    # splat -- because most of the functions takes
    # an array as argument. the drawback is that
    # functions taking only one argument can't be
    # called from the command line (an array will
    # be given to them)
    self.method(method_name).call (arg_list)
  rescue ArgumentError => e
    begin
      self.method(method_name).call *(arg_list)
    rescue ArgumentError => e2
      # it didn't work -- the arguments given can't
      # be fit. better just try to let the caller
      # figure out when it first called the function.
      stderr.puts e2
      raise e
    end
  end
end

#invoke_double(args = ARGV) ⇒ Object

enables the possibility to inkove a second method with the results of a first one. eg, the results of a method

called file_permissions_octal which returns without output

can be output as:

“puts”, “file_permissions_octal”, “/”

TODO; flawed example



103
104
105
106
107
# File 'lib/rubyment.rb', line 103

def invoke_double args=ARGV
  second_invokation, first_invokation = [args[0], args[1..-1]]
  first_invokation_result = (invoke first_invokation)
  invoke [second_invokation] + [first_invokation_result].flatten(1)
end

#main(args = ARGV) ⇒ Object

print arguments given



367
368
369
370
371
# File 'lib/rubyment.rb', line 367

def main args=ARGV
  stderr = @memory[:stderr]
  time   = @memory[:time]
  puts args.join " "
end

#object_method_args_call(args = ARGV) ⇒ Object

calls object.method call_args note: function closed for extension. a new extension if ever made, will be created with a new function. args:

method (Method or String), object (Object), call_args (Array)

returns:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubyment.rb', line 182

def object_method_args_call args=ARGV
  stderr = @memory[:stderr]
  method, object, *call_args = containerize args
  object ||= self
  method = to_method [method, object]
  call_args = call_args && (containerize call_args)
  begin
    call_args && (method.call *call_args) || method.call
  rescue NameError => nameError
    # every object (even nil) has :method,
    # and every Method has :call: exception
    # is thrown in call
    stderr.puts nameError
    nil
  end
end

#output_array_to_shell(args = ARGV) ⇒ Object

outputs in such a way that it can be given as an array of parameters via bash shell not fully tested, use with caution.



338
339
340
341
342
# File 'lib/rubyment.rb', line 338

def output_array_to_shell args=ARGV
  args.map {|arg|
    "\"" << (arg && arg.to_s || "") << "\""
  }.join " "
end

#rest_request(args = ARGV) ⇒ Object

makes a rest request. for now, the parameters must still be hardcoded.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/rubyment.rb', line 376

def rest_request args=ARGV
  require 'base64'
  require 'rest-client'
  require 'json'
  stderr = @memory[:stderr]
  time   = @memory[:time]

  ="my_atlasian_account"
  jira_host = "https://mydomain.atlassian.net/"
  issue = "JIRAISSUE-6517"
  url = "#{jira_host}/rest/api/2/issue/#{issue}/comment"
  # ways to set base64_auth:
  # 1) programmatically: let pw in plain text:
  # auth = "my_user:my_pw"
  # base64_auth = Base64.encode64 auth
  # 2) a bit safer (this hash can be still used
  # to hijack your account):
  #  echo "my_user:my_pw" | base64 # let a whitespace in the beginning
  base64_auth = "bXlfdXNlcjpteV9wdwo="
  # todo: it has to be interactive, or fetch from a keying
  # to achieve good security standards
  method = :get
  method = :post
  timeout = 2000
  headers = {"Authorization" => "Basic #{base64_auth}" }
  verify_ssl = true
  json =<<-ENDHEREDOC
  {
      "body" : "my comment"
  }
  ENDHEREDOC
  payload = "#{json}"
  request_execution = RestClient::Request.execute(:method => method, :url => url, :payload => payload, :headers => headers, :verify_ssl => verify_ssl, :timeout => timeout)
  parsed_json = JSON.parse request_execution.to_s
  stderr.puts parsed_json
  parsed_json
end

#rubyment_gem_defaults(args = ARGV) ⇒ Object

defaults for the rubyment gem args:

gem_name, gem_version, gem_dir, gem_ext, gem_hifen

all Strings. defaults: [“rubyment”, version [], memory, “.gem”, “-”] returns:

gem_name, gem_version, gem_dir, gem_ext, gem_hifen


949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/rubyment.rb', line 949

def rubyment_gem_defaults args=ARGV
  memory = @memory
  running_dir   = memory[:running_dir]
  basic_version = memory[:basic_version]
  major_version = memory[:major_version]

  gem_name,
  gem_version,
  gem_dir,
  gem_ext,
  gem_hifen,
  gem_date,
  gem_summary,
  gem_description,
  gem_authors,
  gem_email,
  gem_files,
  gem_homepage,
  gem_license,
  gem_validate_class,
  gem_validate_class_args,
  gem_validate_class_method,
  gem_is_current_file = args

  gem_name ||= "rubyment"
  gem_version ||= (version [])
  gem_dir ||= running_dir
  gem_ext ||= ".gem"
  gem_hifen ||= "-"
  gem_ext ||= "date"
  gem_date ||= "2018-04-23"
  gem_summary     ||= "a set of ruby helpers"
  gem_description ||= "a gem for keeping Rubyment, a set of ruby helpers"
  gem_authors     ||= ["Ribamar Santarosa"]
  gem_email       ||= '[email protected]'
  gem_files       ||= ["lib/#{gem_name}.rb"]
  gem_homepage    ||=
    "http://rubygems.org/gems/#{gem_name}"
  gem_license     ||= 'GPL-3.0'
  gem_validate_class ||= self.class.to_s
  gem_validate_class_args ||= {:invoke => ["p", "installed and validated"] }
  gem_validate_class_method ||= "new"
  gem_is_current_file = true # this enables the possibility of building
  #  a gem for the calling file itself, but be aware that lib/gem_file.rb
  # is supposed to be overriden later.
  [
     gem_name,
     gem_version,
     gem_dir,
     gem_ext,
     gem_hifen,
     gem_date,
     gem_summary,
     gem_description,
     gem_authors,
     gem_email,
     gem_files,
     gem_homepage,
     gem_license,
     gem_validate_class,
     gem_validate_class_args,
     gem_validate_class_method,
     gem_is_current_file,
 ]
end

#rubyment_gem_spec(args = ARGV) ⇒ Object

rubyment_gem_spec args (Array, forwarded and transfomed by rubyment_gem_defaults) returns: a gem spec string for Rubyment



923
924
925
926
# File 'lib/rubyment.rb', line 923

def rubyment_gem_spec args=ARGV
  memory = @memory
  gem_spec rubyment_gem_defaults args
end

#save_file(url, location, wtime = 0, more = {}) ⇒ Object

save url contents to a local file location url can be a uri to a local file args: url (String), location (String), wtime (Integer) more (Hash) details: more, more for http_basic_authentication returns

contents of url (String)


219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rubyment.rb', line 219

def save_file url, location, wtime=0, more = {}
  require 'open-uri'
  require 'fileutils'
  FileUtils.mkdir_p File.dirname location # note "~" doesn't work
  user = more[:username]
  pw = more[:password]
  contents = open(url, :http_basic_authentication => [user, pw]).read
  r = File.write location, contents
  sleep wtime
  contents
end

#serialize_json_metadata(args = ARGV) ⇒ Object

serialize_json_metadata args:

payload (String), metadata (Hash), separator (String)

prepends a JSON dump of metadata followed by separator to a copy of payload, and returns it.



690
691
692
693
694
695
696
697
698
# File 'lib/rubyment.rb', line 690

def  args=ARGV
  require 'json'
  memory = @memory
  static_separator = memory[:static_separator_key]
  payload, , separator = args
   ||= { }
  separator ||= static_separator
  serialized_string = (JSON.pretty_generate ) + separator + payload
end

#shell_dec(args = ARGV) ⇒ Object

prompts for arguments to dec, calls dec, and output the decrypted data to stdout. args: (forwarded to shell_dec_input, as of now: returns: nil

planned changes: stop argument shifting. call separate functions.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/rubyment.rb', line 533

def shell_dec args=ARGV
  require 'json'
  require 'base64'
  memory = @memory
  stderr = memory[:stderr]
  password, base64_json_serialized_data, iv_deprecated = shell_dec_input args
   = JSON.parse Base64.decode64 base64_json_serialized_data
  base64_iv = ["base64_iv"]
  base64_encrypted = ["base64_encrypted"]
  base64_salt = ["base64_salt"]
  base64_iter = ["base64_iter"]
  base64_key  = ["base64_key" ]
  ending = nil
  pw_plain = dec [password, base64_iv, base64_encrypted, ending, base64_salt, base64_iter]
  shell_dec_output [pw_plain]
end

#shell_dec_input(args = ARGV) ⇒ Object

prompts for arguments to dec function args:

iv, encrypted, password

(all Strings)

returns:

[password, encrypted, iv] (all Strings)

planned changes: call separate functions.



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/rubyment.rb', line 480

def shell_dec_input args=ARGV
   require 'stringio'
   require "io/console"
   memory = @memory
   debug  = memory[:debug]
   stderr = memory[:stderr]
   stdout = memory[:stdout]
   stdin  = memory[:stdin]

   stderr.print "iv:"
   # basically => any string other than "" or the default one:
   iv = args.shift.to_s.split("\0").first
   iv = (url_to_str iv) || iv.to_s.split("\0").first || (url_to_str 'out.enc.iv.base64')
   debug && (stderr.puts iv)
   stderr.puts
   stderr.print "encrypted:"
   # basically => any string other than "" or the default one:
   encrypted = args.shift.to_s.split("\0").first
   encrypted = (url_to_str encrypted) || encrypted.to_s.split("\0").first  || (url_to_str 'out.enc.encrypted.base64')
   debug && (stderr.puts "#{encrypted}")
   stderr.puts
   stderr.print "password:"
   password = args.shift.to_s.split("\0").first || begin stdin.noecho{ stdin.gets}.chomp rescue gets.chomp end
   stderr.puts
   [password, encrypted, iv]
end

#shell_dec_output(args = ARGV) ⇒ Object

and output the decrypted data to stdout.

planned changes: stop argument shifting. call separate functions.



514
515
516
517
518
519
# File 'lib/rubyment.rb', line 514

def shell_dec_output args=ARGV
  memory = @memory
  stdout = memory[:stdout]
  pw_plain, reserved_for_future = args
  stdout.puts pw_plain
end

#shell_enc(args = ARGV) ⇒ Object

shell_enc args

password, data, encrypted_base64_filename, enc_iv_base64_filename_deprecated

(all Strings)

encrypts data using password and stores to encrypted_base64_filename returns nil

planned changes: encrypted_base64_filename



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/rubyment.rb', line 664

def shell_enc args=ARGV
  require 'json'
  require 'base64'
  require 'openssl'
  password, data, encrypted_base64_filename, enc_iv_base64_filename_deprecated  = shell_enc_input args
  salt = nil
  iter = nil
  ending = nil
  base64_encrypted, base64_iv, base64_salt, base64_iter, base64_key = enc [password, data, ending, salt, iter]
   = {
    "metadata"  => "Metadata",
    "base64_iv" => base64_iv,
    "base64_encrypted" => base64_encrypted,
    "base64_salt" => base64_salt,
    "base64_iter" => base64_iter,
  }
  base64_json_serialized_data =  Base64.encode64 JSON.pretty_generate 
  shell_enc_output [base64_json_serialized_data, base64_iv, encrypted_base64_filename ]
end

#shell_enc_input(args = ARGV) ⇒ Object

prompts for arguments to dec args:

multiline_data, data_file, single_line_data, password, encrypted_base64_filename, enc_iv_base64_filename_deprecated

(all Strings)

returns:

[password, data, encrypted_base64_filename, enc_iv_base64_filename_deprecated]

planned changes: call separate functions. stop argument shifting.



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/rubyment.rb', line 606

def shell_enc_input args=ARGV
  memory = @memory
  stderr = @memory[:stderr]
  stdout = @memory[:stdout]
  stdin  = @memory[:stdin]
  debug = memory[:debug]
  require 'stringio'
  require "io/console"

  data = ""
  stderr.print "multi_line_data[ data 1/3, echoing, enter + control-D to stop]:"
  data += args.shift ||  (stdin.readlines.join rescue readlines.join)
  stderr.puts
  stderr.print "data_file [data 2/3]:"
  data_file = args.shift ||  (stdin.gets.chomp rescue gets.chomp)
  data  += url_to_str data_file, ""
  stderr.puts
  stderr.print "single_line_data[data 3/3, no echo part]:"
  data += args.shift || begin stdin.noecho{ stdin.gets}.chomp rescue gets.chomp end
  stderr.puts
  stderr.print "password:"
  password = args.shift.to_s.split("\0").first || begin stdin.noecho{ stdin.gets}.chomp rescue gets.chomp end
  stderr.puts
  stderr.print "encrypted_base64_filename[default=out.enc.encrypted.base64]:"
  # basically => any string other than "" or the default one:
  encrypted_base64_filename = args.shift.to_s.split("\0").first || "out.enc.encrypted.base64"
  stderr.puts encrypted_base64_filename
  stderr.puts
  stderr.print "enc_iv_base64_filename[DEPRECATED]:"
  # basically => any string other than "" or the default one:
  enc_iv_base64_filename_deprecated = args.shift.to_s.split("\0").first || "out.enc.iv.base64"
  stderr.puts enc_iv_base64_filename_deprecated

  [password, data, encrypted_base64_filename, enc_iv_base64_filename_deprecated]
end

#shell_enc_output(args = ARGV) ⇒ Object

outputs the results from enc



644
645
646
647
648
649
650
651
652
# File 'lib/rubyment.rb', line 644

def shell_enc_output args=ARGV
  memory = @memory
  stderr = memory[:stderr]
  base64_encrypted, base64_iv, encrypted_base64_filename, enc_iv_base64_filename_deprecated = args
  puts base64_iv
  puts base64_encrypted
  File.write  encrypted_base64_filename, base64_encrypted
  stderr.puts
end

#shell_string_in_columns(args = ARGV) ⇒ Object

planned changes: use stdin from memory instead



357
358
359
360
361
362
363
# File 'lib/rubyment.rb', line 357

def shell_string_in_columns args=ARGV
  stderr = @memory[:stderr]
  time   = @memory[:time]
  number_of_columns = input_shift args
  text = input_non_empty_filepath_or_contents_or_multiline_prompt args
  puts (string_in_columns text, number_of_columns)
end

#string_in_columns(s, max_column = 80) ⇒ Object

place a n at every max_column chars approximately (a word can be bigger than max_column, and some other situations)



348
349
350
351
352
# File 'lib/rubyment.rb', line 348

def string_in_columns s, max_column=80
  max_column = max_column.to_i
  as = 0 ; ln = 0 ; t =  s.split.chunk {|l| ((l.size + as) <= max_column ) && (as += l.size ) && ln || (as = l.size; ln += 1)  }.entries.map {|a| a.last.join(" ") }.join("\n")
t
end

#system_rubyment(args = ARGV) ⇒ Object

system_rubyment requires a system’s Rubyment and invoke it using args args: args (Array) returns: Rubyment or false



1142
1143
1144
# File 'lib/rubyment.rb', line 1142

def system_rubyment args=ARGV
  validate_require ['rubyment', 'Rubyment', {:invoke => args }]
end

#test__enc_dec(args = ARGV) ⇒ TrueClass, FalseClass

test for enc and dec.

data
String, nil

data to be encrypted.

If empty or nil, read (without echo) from @memory, which defaults to STDIN

password
String, nil

password to be used to encryption.

If empty or nil, read (without echo) from @memory, which defaults to STDIN

Parameters:

  • args, (Array)

    an Array whose elements are expected to be:

Returns:

  • (TrueClass, FalseClass)

    depending on whether test succeeds.



829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/rubyment.rb', line 829

def test__enc_dec args=ARGV
  stderr = @memory[:stderr]
  data, password = args
  stderr.print "[data]"
  data = input_multi_line_non_echo [data]
  stderr.print "[password]"
  password = input_single_line_non_echo [password]
  base64_encrypted, base64_iv, base64_salt, base64_iter, base64_key = enc [password, data]
  dec_args = [password, base64_iv, base64_encrypted, nil, base64_salt, base64_iter]
  stderr.puts "# WARNING: secrets, including password are printed here. Storing them may be a major security incident."
  stderr.puts "# programmatically:"
  stderr.puts "dec " + dec_args.to_s
  stderr.puts "# shell: "
  stderr.puts "#{$0} invoke_double p dec " + (output_array_to_shell dec_args).to_s
  data_plain = dec [password, base64_iv, base64_encrypted, nil, base64_salt, base64_iter]
  judgement =
    [
      [data, data_plain, "data"]
    ].map(&method("expect_equal")).all?
end

#test__enc_dec_interactive(args = ARGV) ⇒ Object

test for enc and dec_interactive. good idea is to use this function once with the desired data, password, and use the stderr output



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/rubyment.rb', line 854

def test__enc_dec_interactive args=ARGV
  stderr = @memory[:stderr]
  data, password = args
  stderr.print "[data]"
  data = input_multi_line_non_echo [data]
  stderr.print "[password]"
  password = input_single_line_non_echo [password]
  stderr.puts
  base64_encrypted, base64_iv, base64_salt, base64_iter, base64_key = enc [password, data]
  # the output is supposed to be safe to store,
  # so password is not placed in from dec_interactive_args:
  dec_interactive_args = [base64_iv, base64_encrypted, base64_salt, base64_iter]
  stderr.puts "# programmatically:"
  stderr.puts "dec_interactive " + dec_interactive_args.to_s
  stderr.puts "# shell: "
  stderr.puts "#{$0} invoke_double puts dec_interactive " + (output_array_to_shell dec_interactive_args).to_s
  stderr.puts "#or shell var:"
  stderr.puts "my_secret=$(#{$0} invoke_double puts dec_interactive " + (output_array_to_shell dec_interactive_args).to_s + ")\necho $my_secret\nunset mysecret"
  data_plain = dec_interactive(dec_interactive_args + [password])
  judgement =
    [
      [data, data_plain, "data"]
    ].map(&method("expect_equal")).all?
end

#test__enc_dec_nil(args = ARGV) ⇒ Object

test for enc and dec. “” and nil are expected to be treated as the same.



794
795
796
797
798
799
800
801
# File 'lib/rubyment.rb', line 794

def test__enc_dec_nil args=ARGV
  nil_case = dec [nil, "ltUQIxgRAeUNXPNTTps8FQ==\n", "xyeqxw/TzkyXtOxpDqAl58SNAvXPyNZ89B5JGtwDkcbjo0vObgPsh5FrgZJs\nHPjofsyXnljnTrHpDoQeDVezo9wBZ74NU+TSi/GssX605oE=\n", nil, "TU4o3IKiFWki3rZ3lMchLQ==\n", "MjAwMDA=\n"]
  empty = dec ["", "ltUQIxgRAeUNXPNTTps8FQ==\n", "xyeqxw/TzkyXtOxpDqAl58SNAvXPyNZ89B5JGtwDkcbjo0vObgPsh5FrgZJs\nHPjofsyXnljnTrHpDoQeDVezo9wBZ74NU+TSi/GssX605oE=\n", "", "TU4o3IKiFWki3rZ3lMchLQ==\n", "MjAwMDA=\n"]
  judgement =
    [
      [nil_case, empty, "empty_nil_equality"]
    ].map(&method("expect_equal")).all?
end

#test__enc_dec_shell_programatically(args = ARGV) ⇒ Object

test for enc and dec and output_array_to_shell. output_array_to_shell should create proper arguments

to dec

TODO: invalid test – that can’t yet be ensured



808
809
810
811
812
813
814
815
816
817
# File 'lib/rubyment.rb', line 808

def test__enc_dec_shell_programatically args=ARGV
  stderr = @memory[:stderr]
  stderr.puts "test invalid; skip"
  shell = nil
  programatically = nil
  judgement =
    [
      [shell, programatically, "shell_programatically_equality"]
    ].map(&method("expect_equal")).all?
end

#test__gem_build(args = ARGV) ⇒ Object

test for gem_build: builds gem for this rubyment file after it, these commands will install/uninstall it: sudo gem install $PWD/rubyment-0.0.#href=":basic_version">memory ; gem list | grep -i rubyment ; sudo gem uninstall rubyment dependee:

test__gem_install_validate_uninstall

args:

gem_spec_path (String), gem_spec_contents (String)

returns: none outputs of gem build (String)



1062
1063
1064
1065
1066
1067
# File 'lib/rubyment.rb', line 1062

def test__gem_build args=ARGV
  require 'fileutils'
  FileUtils.mkdir_p 'lib'
  save_file __FILE__, 'lib/rubyment.rb'
  puts gem_build ["rubyment.spec", rubyment_gem_spec(args) ]
end

#test__gem_build_install_validate_uninstall(args = ARGV) ⇒ Object

test for gem_build, gem_install, gem_list system_rubyment, gem_uninstall note that, if there is a “rubyment” gem already installed, it will be temporarily unavailable. args: args (Array or nil) returns: Rubyment or false



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
# File 'lib/rubyment.rb', line 1262

def test__gem_build_install_validate_uninstall args=ARGV
  memory = @memory
  basic_version = memory[:basic_version]
  major_version = memory[:major_version]
  running_dir = memory[:running_dir]
  test__gem_build []
  already_installed = (system_rubyment ["p", "already installed"])
  sleep 1
  gem_uninstall ["rubyment"]
  puts gem_list ["rubyment"]
  gem_install ["#{running_dir}/rubyment-#{major_version}.#{basic_version}.gem"]
  puts gem_list ["rubyment"]
  v = test__system_rubyment []
  gem_uninstall ["rubyment"]
  already_installed && (gem_install ["rubyment"])
  v
end

#test__gem_complete_flow(args = ARGV) ⇒ Object

test for gem_build, gem_install, gem_list system_rubyment, gem_uninstall args: args (Array or nil) returns: ignore



1349
1350
1351
1352
# File 'lib/rubyment.rb', line 1349

def test__gem_complete_flow args=ARGV
  memory = @memory
  gem_build_push args
end

#test__gem_get_api_key(args = ARGV) ⇒ Object

test for test__gem_get_api_key args: (Array - forwarded to gem_get_api_key) returns: nil



1311
1312
1313
# File 'lib/rubyment.rb', line 1311

def test__gem_get_api_key args=ARGV
  puts gem_get_api_key args
end

#test__json_metadata_serialization(args = ARGV) ⇒ Object

test__json_metadata_serialization sanity test for serialize_json_metadata and deserialize_json_metadata



723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/rubyment.rb', line 723

def  args=ARGV
  judgement = true
  payload = "Payload"
  # note: keys with : instead can't be recovered, because
  # they aren't described in JSON files
   = { "metadata"  => "Metadata" }
  serialized = ( [payload,  ])
  new_payload,  =  [serialized]
  judgement =
    [ [payload, new_payload, "payload"], [, , "metadata"]]
      .map(&method("expect_equal")).all?
end

#test__rubyment_gem_spec(args = ARGV) ⇒ Object

test for rubyment_gem_spec. outputs the contents returned by that function. args: none returns: none



933
934
935
# File 'lib/rubyment.rb', line 933

def test__rubyment_gem_spec args=ARGV
  puts rubyment_gem_spec
end

#test__shell_enc_dec(args = ARGV) ⇒ Object

test__shell_enc_dec sanity test for shell_enc and shell_dec planned changes: don’t output to stdout



765
766
767
768
# File 'lib/rubyment.rb', line 765

def test__shell_enc_dec args=ARGV
  shell_enc ["my secret",  "", "",  "tijolo22", "", ""]
  judgement = ( shell_dec ["", "", "tijolo22"] || true) rescue false
end

#test__system_rubyment(args = ARGV) ⇒ Object

test for system_rubyment dependee:

test__gem_install_validate_uninstall

args: args (Array or nil) returns: Rubyment or false



1210
1211
1212
1213
# File 'lib/rubyment.rb', line 1210

def test__system_rubyment args=ARGV
  rubyment_args = (args.to_a.size > 0 && args) || ["main", "tested system_rubyment"]
  p validate_require ['rubyment', 'Rubyment', {:invoke => rubyment_args }]
end

#to_class(args = ARGV) ⇒ Object

returns a Class object out of class_name (or itself if it is already a class)



143
144
145
146
147
148
149
150
# File 'lib/rubyment.rb', line 143

def to_class args=ARGV
  class_name, future_arg = containerize args
  begin
    class_object = ( class_name.is_a? Class ) && class_name || (Object.const_get class_name.to_s)
  rescue NameError => nameErrorE
    nil
  end
end

#to_method(args = ARGV) ⇒ Object

returns a Method (object.method if object is given). give self as object to look up at the current context args:

name (String), object (Object)

returns:

method_object (Method)


160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rubyment.rb', line 160

def to_method args=ARGV
  stderr = @memory[:stderr]
  name, object = containerize args
  begin
    method =  object.method("method").call(name)
  rescue NameError => nameError
    # every object (even nil) has :method,
    # and every Method has :call: exception
    # is thrown in call
    stderr.puts nameError
    nil
  end
end

#url_to_str(url, rescue_value = nil) ⇒ Object

returns url contents



233
234
235
236
# File 'lib/rubyment.rb', line 233

def url_to_str url, rescue_value=nil
require 'open-uri'
  contents = open(url).read rescue rescue_value
end

#validate_require(args = ARGV) ⇒ Object

validate_require requires a file/gem in the system returns nil if not found args: [requirement (String), validator_class (Class or String or nil),

validator_args (Array), validator_method (Method or String)]

returns: Rubyment, true or false



1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
# File 'lib/rubyment.rb', line 1121

def validate_require args=ARGV
  stderr = @memory[:stderr]
  requirement, validator_class, validator_args, validator_method = containerize args
  validate_call = validator_class && true
  validator_class = to_class validator_class
  validator_method ||=  "new"
  begin
    require requirement
    validate_call && (object_method_args_call [validator_method, validator_class, validator_args]) || (!validate_call) && true
  rescue LoadError => e
    stderr.puts e
    nil
  end
end

#version(args = ARGV) ⇒ Object

returns a version number comprised of a major and a minor number args:

major_version (String or nil), minor_version (String or nil)

defaults:

[@memory[:major_version]], @memory[:basic_version]]

returns: “#major.#minor”



86
87
88
89
90
91
92
93
94
# File 'lib/rubyment.rb', line 86

def version args=ARGV
  memory = @memory
  major_version = memory[:major_version]
  basic_version = memory[:basic_version]
  major, minor = args
  major ||= major_version
  minor ||= basic_version
  "#{major}.#{minor}"
end