class BeautifulUrl::BeautifulUrl
Constants
- DEFAULT_URL
#¶ ↑
DEFAULT_URL
¶ ↑#¶ ↑
- RUN_ALREADY
#¶ ↑
RUN_ALREADY
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
Usage examples:
x = BeautifulUrl::BeautifulUrl.new('palemoon', :replace_localhost) x = BeautifulUrl::BeautifulUrl.new('palemoon' {{ replace_localhost: true }}
#¶ ↑
# File lib/beautiful_url/class/initialize.rb, line 54 def initialize( commandline_arguments = ARGV, run_already = RUN_ALREADY, &block ) reset # Must come first. case commandline_arguments # case tag # ======================================================================= # # === :dont_run_yet # ======================================================================= # when :dont_run_yet, :do_not_run_yet commandline_arguments = DEFAULT_URL run_already = false end set_commandline_arguments( commandline_arguments ) case run_already # ======================================================================= # # === :replace_localhost # ======================================================================= # when :replace_localhost, :replace, :replace_localhost_with_data, :do_replace_localhost @replace_localhost_with_data = true # Default, as this is more important. run_already = RUN_ALREADY else if run_already.is_a? Hash # Must come after reset(). # =================================================================== # # === :replace_localhost # =================================================================== # if run_already.has_key? :replace_localhost @replace_localhost_with_data = run_already[:replace_localhost] run_already = RUN_ALREADY # Restore the default again. # =================================================================== # # === :replace # =================================================================== # elsif run_already.has_key? :replace @replace_localhost_with_data = run_already[:replace] run_already = RUN_ALREADY # Restore the default again. end end end # ======================================================================= # # === Handle blocks given to us # # This has to occur before run() is called. # ======================================================================= # if block_given? yielded = yield case yielded # ===================================================================== # # === :do_not_replace_the_localhost # ===================================================================== # when :do_not_replace_the_localhost # In this case do nothing. # ===================================================================== # # ===:replace_localhost # ===================================================================== # when :replace_localhost @replace_localhost_with_data = true end end run if run_already end
Public Instance Methods
#¶ ↑
consider_replacing_localhost_with_data
¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 152 def consider_replacing_localhost_with_data if @replace_localhost_with_data do_replace_localhost_with_data_entry end end
#¶ ↑
do_replace_data_entry_with_localhost
¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 170 def do_replace_data_entry_with_localhost @commandline_arguments.map! {|entry| entry = entry.dup if entry.frozen? if entry.include? @data_dir regexp_quoted = Regexp.quote(@data_dir) entry.sub!(regexp_quoted, HTTP_LOCALHOST_STRING) end entry } end
#¶ ↑
do_replace_localhost_with_data_entry
¶ ↑
This method will replace the leading localhost-string with the corresponding data-dir value.
For example, the :
http://localhost/DATA/PC/palemoon/palemoon.cgi
Would become:
/home/x/DATA/PC/palemoon/palemoon.cgi
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 240 def do_replace_localhost_with_data_entry @commandline_arguments.map! {|entry| entry = entry.dup if entry.frozen? if entry.include? HTTP_LOCALHOST_STRING regexp_quoted = Regexp.quote(HTTP_LOCALHOST_STRING) entry.sub!(regexp_quoted, home_dir_of_user_x?) end entry } end
#¶ ↑
generate_tab_completion_for_bash_shell
¶ ↑
This entry point will generate the tab-completion for the BASH shell.
Invocation examples:
purl GENERATE purl --generate
#¶ ↑
# File lib/beautiful_url/class/generate_tab_completion_for_bash_shell.rb, line 26 def generate_tab_completion_for_bash_shell begin require 'collect_first_word_of_case_menu' rescue LoadError; end begin require 'opn' rescue LoadError; end opn; e 'Now generating tab completion for the bash shell.' _ = ''.dup # This is the string to store. _ << 'completion_for_purl() { local cur complete_these_words COMPREPLY=() # Array variable storing the possible completions. cur="${COMP_WORDS[COMP_CWORD]}" complete_these_words="' beautiful_menu = ::BeautifulUrl[:beautiful_menu] if beautiful_menu.is_a? Array beautiful_menu = beautiful_menu.first end results = CollectFirstWordOfCaseMenu.new(beautiful_menu).result results.sort.each {|entry| _ << entry+"\n" } _ << ' " if [[ ${cur} == * ]] ; then COMPREPLY=( $(compgen -W "${complete_these_words}" -- ${cur}) ) return 0 fi } # Next, available auto-completion for the commands "purl" and "url". complete -F completion_for_purl url complete -F completion_for_purl purl ' store_where = "#{BASE_DIR}DATA/PC/OS/LINUX/SHELL/SCRIPTS/completion_for_purl.sh" opn; e 'We will store at `'+store_where+'`.' SaveFile.write_what_into(_, store_where) end
#¶ ↑
handle_additional_actions_after_the_input_arguments_were_already_mapped
¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 161 def handle_additional_actions_after_the_input_arguments_were_already_mapped consider_replacing_localhost_with_data consider_replacing_data_with_localhost end
#¶ ↑
is_a_remote_url?¶ ↑
This method will return true if the URL is remote, false if it is local, and nil if it was not found at all.
The variable @url_was_found will tell us whether the url was found or whether it was not.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 93 def is_a_remote_url? if @is_registered _ = @commandline_arguments.all? {|entry| entry.include? 'http' } else _ = nil end _ # Our return value. end
#¶ ↑
map_the_input_arguments_to_their_corresponding_values
¶ ↑
This method will map the given input to its corresponding value, if possible.
Take note that this method will also change @is_registered if the entry was found.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 32 def map_the_input_arguments_to_their_corresponding_values( i = @commandline_arguments ) i.map! {|entry| if entry.is_a? Symbol entry = entry.to_s.dup end entry = entry.dup if entry.frozen? # ===================================================================== # # Neither ':' or ',' seem to be useful at the end, so we will chop # them away next (since as of October 2011). Trailing whitespace # will also be removed in the line afterwards. # ===================================================================== # if entry and (entry.to_s.end_with?(':') or entry.to_s.end_with?(',')) entry.chop! end if entry and entry.is_a?(String) entry.rstrip! # =================================================================== # # Nor is a leading ':' useful here either. # =================================================================== # entry[0,1] = '' if entry.start_with? ':' end # ===================================================================== # # Handle intralink entries when they contain a '#' character. # ===================================================================== # if entry and entry.is_a?(String) and entry.include?('#') splitted = entry.split('#') set_intralink(splitted.last) entry = splitted.first end old_value = entry.to_s.dup # Use a reference copy here. new_value = ::BeautifulUrl.menu(entry) new_value.flatten! if new_value.is_a? Array if new_value == old_value # =================================================================== # # In this case we did not find anything, so we don't do anything. # =================================================================== # else @is_registered = true entry = new_value end if @intralink and !@intralink.empty? entry = entry.dup if entry.frozen? entry << @intralink # Append in this case. end entry } i.flatten! end
#¶ ↑
replace_localhost_with_data
?¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 268 def replace_localhost_with_data? @replace_localhost_with_data end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
# File lib/beautiful_url/class/reset.rb, line 14 def reset set_intralink set_data_dir(:default) # ======================================================================= # # === @commandline_arguments # ======================================================================= # @commandline_arguments = [] # ======================================================================= # # === @is_registered # # By default the given input is NOT registered. A method can change # this value at a later time. (In the past this variable was called # @url_was_found, but during the rewrite in early 2020 it was # renamed). # ======================================================================= # @is_registered = false # Will become true if the URL was found. # ======================================================================= # # === @replace_localhost_with_data # # This variable must be false by default. If true then we will replace # the localhost string with the DATA/ string. # ======================================================================= # @replace_localhost_with_data = false # ======================================================================= # # === @replace_data_with_localhost # ======================================================================= # @replace_data_with_localhost = false # ======================================================================= # # === @ignore_help # # This variable can be used to specifically NOT make use of --help, # that is to ignore this case even if the user specified it. # ======================================================================= # @ignore_help = false end
#¶ ↑
return_corresponding_entry
¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 327 def return_corresponding_entry @commandline_arguments.first end
#¶ ↑
search_for_this_url
¶ ↑
This will return a string.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 133 def search_for_this_url(i) set_commandline_arguments(i) map_the_input_arguments_to_their_corresponding_values handle_additional_actions_after_the_input_arguments_were_already_mapped return string? end
#¶ ↑
set_data_dir
¶ ↑
We need this method so that the user can define another data-dir that is to be used.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 300 def set_data_dir( i = :default ) case i # ======================================================================= # # === :default # ======================================================================= # when :default, nil i = DATA_DIR end @data_dir = i end
#¶ ↑
set_intralink
¶ ↑
This method sets the @intralink variable. It is the only allowed way to modify this variable. The @intralink variable can be a pointer to a local or to a remote page.
As of August 2014, we will only set @intralink if we provide input.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 281 def set_intralink( i = nil, use_this_token = '#' ) case i when :default i = nil end if i i[0,0] = use_this_token unless i.empty? end @intralink = i # Must always be set, even to nil. end
#¶ ↑
set_shall_we_replace_localhost
¶ ↑
This method simply sets @replace_localhost_with_data to the passed value, which is true by default.
If true, we will replace the “localhost” string with the DATA constant.
Parameters can be like this:
:replace => true # Yes, replace localhost with data. :replace => false # No, do not replace localhost with data. :replace => :reverse # Replace data with localhost, which is the reverse.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 121 def set_shall_we_replace_localhost( i = true ) @replace_localhost_with_data = i end
#¶ ↑
show_help
¶ ↑
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 362 def show_help( shall_we_exit = :then_exit ) unless @ignore_help opn e 'To issue the various instructions, use AUTOGENERATED or GENERATED.' e e 'Alternative, it works as a downcased variant as well, with -- as in:' e e ' --autogenerate' e ' --generate' e exit if shall_we_exit == :then_exit end end
#¶ ↑
url_as_string
¶ ↑
Return the @input in string variant, always. This can be used as content that can be displayed in other programs.
Take note that in the past result? returned an Array, from 02.08.2015 up until 04.01.2020. Past that point it will return a String instead.
#¶ ↑
# File lib/beautiful_url/class/misc.rb, line 387 def url_as_string return_corresponding_entry.to_s end