class Blufin::YmlCommon
Public Class Methods
Darkens the path and highlights the filename. @return String
# File lib/core/yml/yml_common.rb, line 188 def self.colorize_path_and_file(file, site_path) file_split = split_to_path_file(file, site_path) "\x1B[38;5;240m#{file_split[0].gsub(/\/\z/, '')} → \x1B[38;5;140m#{file_split[1]}" end
Converts /Users/Albert/Repos/repos-blufin/[site-name]/yml/worker/amazon.yml -> amazon.yml @return String
# File lib/core/yml/yml_common.rb, line 168 def self.convert_filename_to_name(file) file_split = file.split('/') file_split[file_split.length - 1].gsub(/\.yml\z/, '') end
Converts an Array of lines to a string (for easier gsub replacement). @return String
# File lib/core/yml/yml_common.rb, line 293 def self.convert_line_array_to_string(array_of_lines) raise RuntimeError, "Expected Array of lines, instead got: #{array_of_lines.class}" unless array_of_lines.is_a?(Array) string = '' array_of_lines.each_with_index do |line, idx| newline_or_not = (idx == (array_of_lines.length - 1)) ? '' : "\n" string += "#{line}#{newline_or_not}" end string end
Converts Regex to string, IE: =(?-mix:A+z) -> A[A-Z_]+z
# File lib/core/yml/yml_common.rb, line 174 def self.convert_regex_to_string(regex) raise RuntimeError, "Expected Regex, instead got:#{regex.class}" unless regex.nil? || regex.is_a?(Regexp) regex_string = regex.to_s regex_string.gsub(/\A\(\?-mix:/, '').gsub(/\)\z/, '') end
Converts a string to an Array of lines to a string. @return String
# File lib/core/yml/yml_common.rb, line 305 def self.convert_string_to_line_array(string) raise RuntimeError, "Expected String, instead got: #{string.class}" unless string.is_a?(String) array_of_lines = [] string.split("\n").each { |line| array_of_lines << line.gsub("\n", '') } array_of_lines end
Content Hash is and Hash of Arrays where the inner Array consists of:
Key = Snake Cased String (such as: amazon, ebay, magento, etc). Value = Contents -> for Blufin::YmlCommon::validate_file_and_contents()
Value = Contents to Ignore -> for Blufin::YmlCommon::validate_file_and_contents()
Path parameter is where the root of where the directories will be, so if message handlers (for example) live at the following location: ../app-infrastructure/{site-name}-worker/src/main/java/io/{site-name}/worker/messages/amazon/AmazonMessageHandler.java
Then path would be: ../app-infrastructure/{site-name}-worker/src/main/java/io/{site-name}/worker/messages
@return void
# File lib/core/yml/yml_common.rb, line 325 def self.create_boilerplate_files(content_hash, path, site, error_handler, default_nested_paths = %w(data logic)) raise RuntimeError, "Expected Hash, instead got: #{content_hash.class}" unless content_hash.is_a?(Hash) raise RuntimeError, "Expected String, instead got: #{path.class}" unless path.is_a?(String) raise RuntimeError, "Expected YmlErrorHandler (or nil), instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler) || error_handler.nil? raise RuntimeError, "Expected Array, instead got: #{default_nested_paths.class}" unless default_nested_paths.is_a?(Array) site = Blufin::SiteResolver::validate_site(site) site_path = Blufin::SiteResolver::get_site_location(site) path = path.gsub(/\/\z/, '') path_last = path.split('/') path_last = path_last[path_last.length - 1] expected_paths = [] content_hash.each do |key, ca| key_camel_cased_lower = Blufin::Strings::snake_case_to_camel_case_lower(key) contents = ca[0] contents_ignore = ca[1] path_inner = "#{path}/#{key_camel_cased_lower}" file = "#{path_inner}/#{ca[2]}" raise RuntimeError, "Expected String, instead got: #{contents.class}" unless contents.is_a?(String) raise RuntimeError, "Expected Array, instead got: #{contents_ignore.class}" unless contents_ignore.is_a?(Array) expected_paths << path_inner Blufin::YmlCommon::validate_file_and_contents(site, file, Blufin::YmlCommon::convert_string_to_line_array(contents), contents_ignore, error_handler) # Check for Rogue files (inner). existing_files = [] Blufin::Files::get_files_in_dir(path_inner).each do |existing_file| begin existing_file_parts = existing_file.split("#{path_last}/#{key_camel_cased_lower}/") existing_file_parts = existing_file_parts[1].split('/') next unless existing_file_parts.length == 1 existing_files << existing_file rescue next end end existing_files.each do |existing_file| unless existing_file == file error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_file, site_path)) end end # Check for Rogue directories (inner) -- and create default_nested paths (if not exist). existing_directories = Blufin::Files::get_dirs_in_dir(path_inner) existing_directories.each do |existing_directory| default_nested_paths_found = false default_nested_paths.each { |default_nested_path| default_nested_paths_found = true if existing_directory == "#{path_inner}/#{default_nested_path}" } error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_directory, site_path)) unless default_nested_paths_found end # Create "default_nested_path" if no exists. default_nested_paths.each { |default_nested_path| Blufin::Files::create_directory("#{path_inner}/#{default_nested_path}") unless Blufin::Files::path_exists("#{path_inner}/#{default_nested_path}") } end # Check for Rogue directories. existing_directories = Blufin::Files::get_dirs_in_dir(path) existing_directories.each do |existing_directory| unless expected_paths.include?(existing_directory) error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_EXPECTED, nil, nil, nil, Blufin::YmlCommon::remove_site_path(existing_directory, site_path)) end end # Check for Missing directories (in theory this will never be reached as it's created above). expected_paths.each do |expected_path| unless existing_directories.include?(expected_path) error_handler.add_error(Blufin::YmlErrorHandler::DIRECTORY_NOT_FOUND, nil, nil, nil, Blufin::YmlCommon::remove_site_path(expected_path, site_path)) end end end
Extracts 13, 2 from DECIMAL(13,2) @return List
# File lib/core/yml/yml_common.rb, line 133 def self.decimal_extract_values(type) decimal_amount = Blufin::Strings::extract_using_regex(type, /\(\d{1,2},\d{1,2}\)\z/, %w{( )}) decimal_amount = decimal_amount.split(',') [decimal_amount[0].to_i, decimal_amount[1].to_i] end
Removes formatting (such as asterisks '*') from description strings. @return String
# File lib/core/yml/yml_common.rb, line 147 def self.description_without_formatting(description) description.gsub(/\s\*/, ' ').gsub(/\*\s/, ' ') end
Returns Array 'Measure' from ENUM_CUSTOM('Measure') @return Array
# File lib/core/yml/yml_common.rb, line 66 def self.enum_name_extractor(enum_string) if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM return enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM}\(/, '').gsub(/\)\z/, '').gsub("'", '') elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM return enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM}\(/, '').gsub(/\)\z/, '').gsub("'", '') else raise RuntimeError, "'#{enum_string}' doesn't match regex for ENUM, ENUM_CUSTOM or ENUM_SYSTEM." end end
Returns TYPE_ENUM, TYPE_ENUM_CUSTOM or TYPE_ENUM_SYSTEM from ENUM('a','b') string. @return String
# File lib/core/yml/yml_common.rb, line 25 def self.enum_type_extractor(enum_string) if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM Blufin::YmlSchemaValidator::TYPE_ENUM elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM else raise RuntimeError, "'enum_string' doesn't match regex --> #{enum_string}" end end
Returns Array ['a','b'] from ENUM('a','b') @return Array
# File lib/core/yml/yml_common.rb, line 39 def self.enum_value_extractor(enum_string, site) if enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM enum_string = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM}\(/, '').gsub(/\)\z/, '') enum_array = enum_string.split(',') enum_array = enum_array.map { |value| value.gsub!(/\A'/, '').gsub!(/'\z/, '') if value.is_a?(String) && value != '' } return enum_array elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_CUSTOM enum_array = [] enum_string = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_CUSTOM}\(/, '').gsub(/\)\z/, '').gsub(/\A'/, '').gsub(/'\z/, '') enum_custom_all = get_enum_scanner(site).get_custom_enums enum_array = enum_custom_all[enum_string] if enum_custom_all.has_key?(enum_string) return enum_array elsif enum_string =~ Blufin::YmlSchemaValidator::REGEX_ENUM_SYSTEM enum_array = [] enum_string = enum_string.gsub(/\A#{Blufin::YmlSchemaValidator::TYPE_ENUM_SYSTEM}\(/, '').gsub(/\)\z/, '').gsub(/\A'/, '').gsub(/'\z/, '') enum_system_all = get_enum_scanner(site).get_system_enums enum_array = enum_system_all[enum_string] if enum_system_all.has_key?(enum_string) return enum_array else raise RuntimeError, "'enum_string' doesn't match regex --> #{enum_string}" end end
Extracts OBJECT, OBJECT_LIST or nil from @schema_data Hash. @return String
# File lib/core/yml/yml_common.rb, line 458 def self.extract_child_type_from_schema_data(schema_data, schema, table) ct = extract_child_type_from_schema_data_for_java(schema_data, schema, table) ct.nil? ? nil : ct.split('.')[1] end
Extracts DataType.OBJECT, DataType.OBJECT_LIST or nil from @schema_data Hash. @return String
# File lib/core/yml/yml_common.rb, line 465 def self.extract_child_type_from_schema_data_for_java(schema_data, schema, table) schema_data[schema][table].each do |column_name, column_data| if column_data.has_key?(Blufin::YmlSchemaValidator::CHILD_TYPE) return column_data[Blufin::YmlSchemaValidator::CHILD_TYPE] end end nil end
Takes something like app.field[] and returns -> field. @return String
# File lib/core/yml/yml_common.rb, line 127 def self.extract_field_name(field) field.gsub(/\A(#{Blufin::YmlSchemaValidator::VALID_SCHEMAS_REGEX})\./, '').gsub(/(\[\]|\[link\])\z/, '') end
Abstract method for extracting flags used in Schema definition. @return Blufin::YmlSchemaFlags
# File lib/core/yml/yml_common.rb, line 78 def self.extract_flags(flags) raise RuntimeError, "Expected String, instead got:#{flags.class}" unless flags.is_a?(String) errors = [] sort_order = 1 definition_order = {} flags_object = Blufin::YmlSchemaFlags.new flags_object.flags_raw = flags flags.split(' ').each do |flag| flag_invalid = false case flag when Blufin::YmlSchemaValidator::FLAG_PRIMARY_KEY flags_object.primary_key = true flags_object.primary_key_sort_order = sort_order when Blufin::YmlSchemaValidator::FLAG_INDEX flags_object.index = true flags_object.index_sort_order = sort_order when Blufin::YmlSchemaValidator::FLAG_NULLABLE flags_object.nullable = true flags_object.nullable_sort_order = sort_order when Blufin::YmlSchemaValidator::FLAG_UNIQUE flags_object.unique = true flags_object.unique_sort_order = sort_order else if flag =~ /\A#{Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT}\z/ flags_object.auto_increment = true flags_object.auto_increment_amount = nil flags_object.auto_increment_sort_order = sort_order elsif flag =~ /\A#{Blufin::YmlSchemaValidator::FLAG_AUTO_INCREMENT}\(\d{1,10}\)\z/ auto_increment_amount = flag.match(/\(\d{1,10}\)/) auto_increment_amount = auto_increment_amount[0].gsub('(', '').gsub(')', '') flags_object.auto_increment = true flags_object.auto_increment_amount = auto_increment_amount flags_object.auto_increment_sort_order = sort_order else errors << ["Invalid 'flag' value.", flag] flag_invalid = true end end unless flag_invalid definition_order[flag] = sort_order sort_order = sort_order + 1 end end flags_object.definition_order = definition_order [flags_object, errors] end
Extracts the <groupID>, <artifactId> & <version> from -> [{“group-id”=>“com.zaxxer”}, {“artifact-id”=>“HikariCP”}, {“version”=>“3.2.0-BLUFIN”}] @return Hash
# File lib/core/yml/yml_common.rb, line 444 def self.extract_group_artifact_version_from_array(array) raise RuntimeError, "Expected Array, instead got: #{array.class}" unless array.is_a?(Array) obj = {} array.each do |fragment| raise RuntimeError, "Expected Hash, instead got: #{fragment.class}" unless fragment.is_a?(Hash) obj[Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID] = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_GROUP_ID) obj[Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID] = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_ARTIFACT_ID) obj[Blufin::YmlMavenValidator::DEPENDENCIES_VERSION] = fragment[Blufin::YmlMavenValidator::DEPENDENCIES_VERSION] if fragment.has_key?(Blufin::YmlMavenValidator::DEPENDENCIES_VERSION) end obj end
Extracts “reference” from “/{reference}.jpg” @return String
# File lib/core/yml/yml_common.rb, line 182 def self.extract_jersey_path_reference(value) value.gsub(/\A(.)*\{/, '').gsub(/\}(.)*\z/, '') end
Determines what a 'LINK' table should be named, @return String
# File lib/core/yml/yml_common.rb, line 153 def self.get_link_table_name(source_table, column_name) column = column_name.dup.gsub("[#{Blufin::YmlSchemaValidator::LINK}]", '') raise RuntimeError, "Expected clean column name without brackets, instead got:#{column}" unless column[-1, 1] =~ /[a-z]/i target_table = column.split('.')[1] "#{source_table}_to_#{target_table}_link" end
Returns TRUE if resource has at least one HTTP method, FALSE otherwise. @return Boolean
# File lib/core/yml/yml_common.rb, line 421 def self.has_at_least_one_http_method(resource_data, internal_or_oauth) raise RuntimeError, "Expected 'internal' or 'oauth', instead got: #{internal_or_oauth}" unless [Blufin::YmlSchemaValidator::CONFIG_INTERNAL, Blufin::YmlSchemaValidator::CONFIG_OAUTH].include?(internal_or_oauth) raise RuntimeError, "Expected Hash, instead got: #{resource_data.class}" unless resource_data.is_a?(Hash) methods_key = (internal_or_oauth == Blufin::YmlSchemaValidator::CONFIG_INTERNAL) ? :methods_internal : :methods_oauth if resource_data.has_key?(methods_key) resource_data[methods_key].keys.each do |http_method| if [ Blufin::YmlConfigValidator::GET, Blufin::YmlConfigValidator::POST, Blufin::YmlConfigValidator::PATCH, Blufin::YmlConfigValidator::DELETE ].include?(http_method) return true else raise RuntimeError, "Un-handled HTTP method: #{http_method}" end end end false end
Returns TRUE if resource has specified HTTP method, FALSE otherwise. @return Boolean
# File lib/core/yml/yml_common.rb, line 406 def self.has_http_method(http_method_name, resource_data, internal_or_oauth) raise RuntimeError, "Expected 'http_method_name' to be [#{Blufin::YmlConfigValidator::VALID_METHODS}], instead got: #{http_method_name}" unless Blufin::YmlConfigValidator::VALID_METHODS.include?(http_method_name) raise RuntimeError, "Expected 'internal' or 'oauth', instead got: #{internal_or_oauth}" unless [Blufin::YmlSchemaValidator::CONFIG_INTERNAL, Blufin::YmlSchemaValidator::CONFIG_OAUTH].include?(internal_or_oauth) raise RuntimeError, "Expected Hash, instead got: #{resource_data.class}" unless resource_data.is_a?(Hash) methods_key = (internal_or_oauth == Blufin::YmlSchemaValidator::CONFIG_INTERNAL) ? :methods_internal : :methods_oauth if resource_data.has_key?(methods_key) resource_data[methods_key].keys.each do |http_method| return true if http_method_name.downcase == http_method.downcase end end false end
Return TRUE if value is nil, false or empty string. @return boolean
# File lib/core/yml/yml_common.rb, line 162 def self.is_empty(value) value.nil? || value == false || value == '' end
Return TRUE if file is .yml – FALSE if not. @return boolean
# File lib/core/yml/yml_common.rb, line 9 def self.is_yml_file(file_path) File.extname(file_path).downcase == '.yml' end
Shortens filenames by removing @site_path. @return String
# File lib/core/yml/yml_common.rb, line 195 def self.remove_site_path(path_and_file, site_path) raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String) raise RuntimeError, "Expected String, instead got:#{site_path.class}" unless site_path.is_a?(String) sp = site_path.dup sp[0] = '' path_and_file = path_and_file.gsub(/\A\//, '') path_and_file.gsub("#{sp}/", '') end
Splits the path & filename. @return Array
# File lib/core/yml/yml_common.rb, line 206 def self.split_to_path_file(path_and_file, site_path) raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String) raise RuntimeError, "Expected String, instead got:#{site_path.class}" unless site_path.is_a?(String) sp = site_path.dup sp[0] = '' path = '' file = '' file_parts = path_and_file.strip.gsub(/\A\//, '').split('/') file_parts.each_with_index do |part, idx| if idx < file_parts.length - 1 path << "#{part}/" else file << part end end [path.gsub("#{sp}/", '').gsub(/\/\z/, ''), file] end
Checks that a directory structure (YML directory) matches a certain criteria. Returns FALSE if fails, TRUE if success @return boolean
# File lib/core/yml/yml_common.rb, line 227 def self.validate_directory_structure(site_path, path, files_expected, files_optional, error_handler) raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String) raise RuntimeError, "Expected Array, instead got:#{files_expected.class}" unless files_expected.is_a?(Array) raise RuntimeError, "Expected Array, instead got:#{files_optional.class}" unless files_optional.is_a?(Array) raise RuntimeError, "Expected YmlErrorHandler, instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler) path = "#{site_path}/#{path}" current_errors = error_handler.get_error_count files_expected = files_expected.map { |value| "#{path}/#{value}" } files_optional = files_optional.map { |value| "#{path}/#{value}" } files_present = [] Blufin::Files::get_files_in_dir(path).each do |file| error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_EXPECTED, file, nil, nil, remove_site_path(file, site_path)) unless files_expected.include?(file) || files_optional.include?(file) files_present << file end files_expected.each do |file| error_handler.add_error(Blufin::YmlErrorHandler::FILE_NOT_FOUND, file, nil, nil, remove_site_path(file, site_path)) unless files_present.include?(file) end current_errors == error_handler.get_error_count end
Checks that a file exists (and if not, creates it) and validates that it matches a basic template. @return void
# File lib/core/yml/yml_common.rb, line 249 def self.validate_file_and_contents(site, file, contents = [], contents_ignore = [], error_handler = nil, add_to_git = true) raise RuntimeError, "Expected String, instead got:#{file.class}" unless file.is_a?(String) raise RuntimeError, "Expected Array, instead got:#{contents.class}" unless contents.is_a?(Array) raise RuntimeError, "Expected Array, instead got:#{contents_ignore.class}" unless contents_ignore.is_a?(Array) raise RuntimeError, "Expected YmlErrorHandler (or nil), instead got:#{error_handler.class}" unless error_handler.is_a?(Blufin::YmlErrorHandler) || error_handler.nil? site = Blufin::SiteResolver::validate_site(site) site_path = Blufin::SiteResolver::get_site_location(site) site_name = Blufin::SiteResolver::get_site_name(site) if Blufin::Files::file_exists(file) lines_expected = [] contents.each do |line| unless contents_ignore.include?(line) unless line =~ /\Aimport\s/ lines_expected << line unless line.strip == '' end end end contents = Blufin::Files::read_file(file) contents.each do |line| line = line.gsub("\n", '') lines_expected.delete(line) if lines_expected.include?(line) end if lines_expected.length > 0 && !error_handler.nil? lines_expected.map! { |line| "\x1B[38;5;154m#{line}" } file = Blufin::YmlCommon::remove_site_path(file, site_path) file = file.gsub(/(#{Blufin::Site::REGEX_APP_DIRS})\/#{site_name}-(#{Blufin::SiteServices::REGEX_JAVA})\/src\/main\/java\//, '') error_output = ["File: \x1B[38;5;196m#{file}\x1B[0m", 'Line(s) expected but not found:'] error_output = error_output + lines_expected + [nil] error_handler.add_error(Blufin::YmlErrorHandler::FILE_CONTENT_MISMATCH, nil, nil, nil, error_output) end else if file =~ /\.java$/ Blufin::Files::write_file_java(file, contents, Blufin::Files::JAVA_AUTO_GENERATED_ONCE) else Blufin::Files::write_file(file, contents) end Blufin::Terminal::command("git add #{file}", site_path, false, false) if add_to_git Blufin::Terminal::output(colorize_path_and_file(file, site_path), Blufin::Terminal::MSG_GENERATED) end end
Returns nil on success, otherwise an array of invalid keys. @return nil|Array
# File lib/core/yml/yml_common.rb, line 15 def self.validate_keys(key_set, valid_keys_as_array) invalid_keys = [] key_set.each do |key| invalid_keys << key unless valid_keys_as_array.include?(key) end return invalid_keys if invalid_keys.any? end
Extracts 45 from VARCHAR(45) @return List
# File lib/core/yml/yml_common.rb, line 141 def self.varchar_extract_max_length(type) Blufin::Strings::extract_using_regex(type, /\(\d{1,3}\)\z/, %w{( )}) end
Private Class Methods
Returns a cached instance of the ENUM scanner. @return Object
# File lib/core/yml/yml_common.rb, line 478 def self.get_enum_scanner(site) if @@enum_scanner.nil? @@enum_scanner = Blufin::ScannerJavaEnums.new(site) end @@enum_scanner end