Class: Rubyment
- Inherits:
-
Object
- Object
- Rubyment
- 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
-
#array_first_remainder(args = ARGV) ⇒ Object
this class very often needs to split first argument and remaining elements.
-
#containerize(args = ARGV) ⇒ Object
returns a Class object out of class_name (or itself if it is already a class).
-
#dec(args = ARGV) ⇒ Object
decrypt encrypted (string), having password (string), iv (string), big_file(bool) is a flag to set padding to 0.
-
#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.
-
#enc(args = ARGV) ⇒ Object
encrypt data (string), with password (string) returns [base64_iv, base64_encrypted].
-
#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.
-
#expect_format_string(args = ARGV) ⇒ Object
format strings for expect_format_string.
-
#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.
-
#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).
-
#filepath_or_contents(file, contents = "") ⇒ Object
returns the contents of file (or empty, or a default if a second parameter is given).
-
#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).
-
#gem_build_push(args = ARGV) ⇒ Object
builds, validates and push a gem accordingly to the arguments.
-
#gem_files_args(args = ARGV) ⇒ Object
extract only the arguments referring to files from args.
-
#gem_get_api_key(args = ARGV) ⇒ Object
gets the api key needed to push gems to rubygems.
-
#gem_install(args = ARGV) ⇒ Object
gem_install args: [gem_spec (String)] returns: console output of gem install (String).
-
#gem_list(args = ARGV) ⇒ Object
gem_list args: [gem_spec (String)] returns: console output of gem install (String).
-
#gem_path(args = ARGV) ⇒ Object
returns the gem path given the params.
-
#gem_push(args = ARGV) ⇒ Object
gem_push args: [gem_spec (String)] returns: console output of gem push (String).
-
#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.
-
#gem_uninstall(args = ARGV) ⇒ Object
gem_uninstall args: [gem_spec (String)] returns: console output of gem uninstall (String).
-
#gem_validate(args = ARGV) ⇒ Object
validate the installation of a gem args rubyment_gem_defaults but planned to change.
-
#gem_validate_args(args = ARGV) ⇒ Object
extract only the arguments for validation from args.
-
#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].
-
#initialize(memory = {}) ⇒ Rubyment
constructor
A new instance of Rubyment.
-
#input_multi_line(args = ARGV) ⇒ Object
opens an echoing multiline prompt, if arg1 is nil or empty args: [ arg1 (String or nil)].
-
#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)].
-
#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.
-
#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.
- #input_shift(args = ARGV) ⇒ Object
- #input_shift_or_empty_string(args = ARGV, default = '') ⇒ Object
-
#input_single_line(args = ARGV) ⇒ Object
opens an echoing prompt, if arg1 is nil or empty args: [ arg1 (String or nil)].
-
#input_single_line_non_echo(args = ARGV) ⇒ Object
opens a non-echoing prompt, if arg1 is nil or empty args: [ arg1 (String or nil)].
-
#invoke(args = ARGV) ⇒ Object
invoke first arg with following args used by initialize.
-
#invoke_double(args = ARGV) ⇒ Object
enables the possibility to inkove a second method with the results of a first one.
-
#main(args = ARGV) ⇒ Object
print arguments given.
-
#object_method_args_call(args = ARGV) ⇒ Object
calls object.method call_args note: function closed for extension.
-
#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.
-
#rest_request(args = ARGV) ⇒ Object
makes a rest request.
-
#rubyment_gem_defaults(args = ARGV) ⇒ Object
defaults for the rubyment gem args: [gem_name, gem_version, gem_dir, gem_ext, gem_hifen] all Strings.
-
#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.
- #save_file(url, location, wtime = 0, more = {}) ⇒ Object
-
#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.
-
#shell_dec(args = ARGV) ⇒ Object
prompts for arguments to dec, calls dec, and output the decrypted data to stdout.
-
#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.
-
#shell_dec_output(args = ARGV) ⇒ Object
and output the decrypted data to stdout.
-
#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.
-
#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].
-
#shell_enc_output(args = ARGV) ⇒ Object
outputs the results from enc.
-
#shell_string_in_columns(args = ARGV) ⇒ Object
planned changes: use stdin from memory instead.
-
#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).
-
#system_rubyment(args = ARGV) ⇒ Object
system_rubyment requires a system’s Rubyment and invoke it using args args: args (Array) returns: Rubyment or false.
-
#test__enc_dec(args = ARGV) ⇒ Object
test for enc and dec.
-
#test__enc_dec_nil(args = ARGV) ⇒ Object
test for enc and dec.
-
#test__enc_dec_shell_programatically(args = ARGV) ⇒ Object
test for enc and dec and output_array_to_shell.
-
#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).
-
#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.
-
#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.
-
#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.
-
#test__json_metadata_serialization(args = ARGV) ⇒ Object
test__json_metadata_serialization sanity test for serialize_json_metadata and deserialize_json_metadata.
-
#test__rubyment_gem_spec(args = ARGV) ⇒ Object
test for rubyment_gem_spec.
-
#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.
-
#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.
-
#to_class(args = ARGV) ⇒ Object
returns a Class object out of class_name (or itself if it is already a class).
-
#to_method(args = ARGV) ⇒ Object
returns a Method (object.method if object is given).
-
#url_to_str(url, rescue_value = nil) ⇒ Object
returns url contents.
-
#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.
- #version(args = ARGV) ⇒ Object
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.1", :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) ⇒ Object
decrypt encrypted (string), having password (string), iv (string), big_file(bool) is a flag to set padding to 0.
planned changes: add metadata information to encrypted decipher.key = Digest::SHA256.hexdigest is not the best security. encrypted could be called base64_encrypted iv could be called base64_iv get only one string, not encrypted and iv (see enc planned changes) remove dependence on the uniqueness of ending
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/rubyment.rb', line 444 def dec args=ARGV require 'openssl' require 'base64' memory = @memory static_end_key = memory[:static_end_key] password, iv, 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 iv plain = decipher.update(Base64.decode64 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 |
#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.
699 700 701 702 703 704 705 706 707 708 |
# File 'lib/rubyment.rb', line 699 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
encrypt data (string), with password (string) returns [base64_iv, base64_encrypted]
planned changes: add metadata information to encrypted return only one string, having encrypted + metadata (having iv) add string length to metadata decipher.key = Digest::SHA256.hexdigest is not the best security.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
# File 'lib/rubyment.rb', line 558 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
741 742 743 744 745 746 747 748 |
# File 'lib/rubyment.rb', line 741 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
728 729 730 731 732 733 |
# File 'lib/rubyment.rb', line 728 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 |
# File 'lib/rubyment.rb', line 121 def file_backup file = __FILE__ , dir = '/tmp/', append = ('-' + Time.now.hash.abs.to_s), prepend='/' stderr = @memory[:stderr] (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 ) stderr.puts "location = dir:#{dir} + prepend:#{prepend} + (#{file_is_filename} && #{file} || '' ) + #{append}" location = dir + prepend + (file_is_filename && file || '' ) + append stderr.puts "FileUtils.mkdir_p File.dirname #{location}" # note "~" doesn't work 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)
204 205 206 |
# File 'lib/rubyment.rb', line 204 def 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.
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rubyment.rb', line 244 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)
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/rubyment.rb', line 969 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)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'lib/rubyment.rb', line 1261 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" = 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 , gem_api_key_file end |
#gem_files_args(args = ARGV) ⇒ Object
extract only the arguments referring to files from args
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/rubyment.rb', line 1090 def gem_files_args args=ARGV gem_name, gem_version, gem_dir, gem_ext, gem_hifen, gem_date, gem_summary, gem_description, , 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)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 |
# File 'lib/rubyment.rb', line 1230 def gem_get_api_key args=ARGV require 'fileutils' username, password, file_destination = args username = input_single_line [username] 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)
1009 1010 1011 1012 1013 1014 |
# File 'lib/rubyment.rb', line 1009 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)
1044 1045 1046 1047 |
# File 'lib/rubyment.rb', line 1044 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”, “-”]
958 959 960 961 |
# File 'lib/rubyment.rb', line 958 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)
1021 1022 1023 1024 1025 |
# File 'lib/rubyment.rb', line 1021 def gem_push args=ARGV gem_spec, future_arg = args p "gem push #{gem_spec}`" p `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
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'lib/rubyment.rb', line 818 def gem_spec args=ARGV memory = @memory gem_name, gem_version, gem_dir, gem_ext, gem_hifen, gem_date, gem_summary, gem_description, , 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 = #{.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)
1032 1033 1034 1035 1036 1037 |
# File 'lib/rubyment.rb', line 1032 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
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
# File 'lib/rubyment.rb', line 1165 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
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
# File 'lib/rubyment.rb', line 1117 def gem_validate_args args=ARGV gem_name, gem_version, gem_dir, gem_ext, gem_hifen, gem_date, gem_summary, gem_description, , 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
422 423 424 425 426 427 428 429 430 |
# File 'lib/rubyment.rb', line 422 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)
305 306 307 308 309 310 |
# File 'lib/rubyment.rb', line 305 def input_multi_line args=ARGV stderr = @memory[:stderr] stdin = @memory[:stdin] stderr.print "multiline[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)
316 317 318 319 320 321 322 |
# File 'lib/rubyment.rb', line 316 def input_multi_line_non_echo args=ARGV stderr = @memory[:stderr] stdin = @memory[:stdin] require "io/console" stderr.print "multiline[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.
272 273 274 275 276 |
# File 'lib/rubyment.rb', line 272 def input_non_empty_filepath_or_contents_or_multiline_prompt args=ARGV stderr = @memory[:stderr] stderr.print "multiline[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.
261 262 263 264 265 |
# File 'lib/rubyment.rb', line 261 def input_non_empty_string_or_multiline_prompt args=ARGV stderr = @memory[:stderr] stderr.print "multiline[control-D to stop]:" args.shift.to_s.split("\0").first || readlines.join end |
#input_shift(args = ARGV) ⇒ Object
329 330 331 |
# File 'lib/rubyment.rb', line 329 def input_shift args=ARGV args.shift end |
#input_shift_or_empty_string(args = ARGV, default = '') ⇒ Object
325 326 327 |
# File 'lib/rubyment.rb', line 325 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)
282 283 284 285 286 287 |
# File 'lib/rubyment.rb', line 282 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)
293 294 295 296 297 298 299 |
# File 'lib/rubyment.rb', line 293 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 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
366 367 368 369 370 |
# File 'lib/rubyment.rb', line 366 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:
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/rubyment.rb', line 181 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.
337 338 339 340 341 |
# File 'lib/rubyment.rb', line 337 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.
375 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 |
# File 'lib/rubyment.rb', line 375 def rest_request args=ARGV require 'base64' require 'rest-client' require 'json' stderr = @memory[:stderr] time = @memory[:time] atlasian_account="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
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 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 |
# File 'lib/rubyment.rb', line 884 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_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" ||= ["Ribamar Santarosa"] gem_email ||= '[email protected]' gem_files ||= ["lib/rubyment.rb"] gem_homepage ||= "http://rubygems.org/gems/#{gem_name}" gem_license ||= 'GPL-3.0' gem_validate_class ||= "Rubyment" 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_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
858 859 860 861 |
# File 'lib/rubyment.rb', line 858 def rubyment_gem_spec args=ARGV memory = @memory gem_spec rubyment_gem_defaults args end |
#save_file(url, location, wtime = 0, more = {}) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rubyment.rb', line 218 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.
680 681 682 683 684 685 686 687 688 |
# File 'lib/rubyment.rb', line 680 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.
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/rubyment.rb', line 532 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.
479 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 |
# File 'lib/rubyment.rb', line 479 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.
513 514 515 516 517 518 |
# File 'lib/rubyment.rb', line 513 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
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/rubyment.rb', line 654 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.
596 597 598 599 600 601 602 603 604 605 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 |
# File 'lib/rubyment.rb', line 596 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, 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
634 635 636 637 638 639 640 641 642 |
# File 'lib/rubyment.rb', line 634 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
356 357 358 359 360 361 362 |
# File 'lib/rubyment.rb', line 356 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)
347 348 349 350 351 |
# File 'lib/rubyment.rb', line 347 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
1083 1084 1085 |
# File 'lib/rubyment.rb', line 1083 def system_rubyment args=ARGV validate_require ['rubyment', 'Rubyment', {:invoke => args }] end |
#test__enc_dec(args = ARGV) ⇒ Object
test for enc and dec. good idea is to use this function once with the desired data, password, and use the stderr output
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 |
# File 'lib/rubyment.rb', line 794 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 "# 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_nil(args = ARGV) ⇒ Object
test for enc and dec. “” and nil are expected to be treated as the same.
765 766 767 768 769 770 771 772 |
# File 'lib/rubyment.rb', line 765 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
779 780 781 782 783 784 785 786 787 788 |
# File 'lib/rubyment.rb', line 779 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)
997 998 999 1000 1001 1002 |
# File 'lib/rubyment.rb', line 997 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
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 |
# File 'lib/rubyment.rb', line 1203 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
1287 1288 1289 1290 |
# File 'lib/rubyment.rb', line 1287 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
1249 1250 1251 |
# File 'lib/rubyment.rb', line 1249 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
713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'lib/rubyment.rb', line 713 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
868 869 870 |
# File 'lib/rubyment.rb', line 868 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
755 756 757 758 759 |
# File 'lib/rubyment.rb', line 755 def test__shell_enc_dec args=ARGV shell_enc ["my secret", "", "", "tijolo22", "", ""] # shell_dec will read from the output file: 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
1151 1152 1153 1154 |
# File 'lib/rubyment.rb', line 1151 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)
142 143 144 145 146 147 148 149 |
# File 'lib/rubyment.rb', line 142 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)
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rubyment.rb', line 159 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
232 233 234 235 |
# File 'lib/rubyment.rb', line 232 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
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 |
# File 'lib/rubyment.rb', line 1057 def validate_require args=ARGV p "{" p args p "}" 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" x= begin require requirement xy = validate_call && (object_method_args_call [validator_method, validator_class, validator_args]) || (!validate_call) && true xy rescue LoadError => e stderr.puts e nil end x 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 |