Class: BeautifulUrl::BeautifulUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/beautiful_url/class/run.rb,
lib/beautiful_url/class/misc.rb,
lib/beautiful_url/class/reset.rb,
lib/beautiful_url/class/constants.rb,
lib/beautiful_url/class/initialize.rb,
lib/beautiful_url/class/generate_tab_completion_for_bash_shell.rb

Overview

It is part of the class.

Constant Summary collapse

RUN_ALREADY =
#

RUN_ALREADY

#
true
DEFAULT_URL =
#

DEFAULT_URL

#
'http://localhost/DATA/PROGRAMMING_LANGUAGES/R/R.cgi'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = RUN_ALREADY, &block) ⇒ BeautifulUrl

#

initialize

Usage examples:

x = BeautifulUrl::BeautifulUrl.new('palemoon', :replace_localhost)
x = BeautifulUrl::BeautifulUrl.new('palemoon' {{ replace_localhost: true }}
#


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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

Class Method Details

.[](i = 'palemoon', &block) ⇒ Object



401
402
403
404
405
# File 'lib/beautiful_url/class/misc.rb', line 401

def self.[](
    i = 'palemoon', &block
  )
  new(i, &block).return_as_string
end

Instance Method Details

#consider_replacing_data_with_localhostObject

#

consider_replacing_data_with_localhost

#


143
144
145
146
147
# File 'lib/beautiful_url/class/misc.rb', line 143

def consider_replacing_data_with_localhost
  if @replace_data_with_localhost
    do_replace_data_with_localhost
  end
end

#consider_replacing_localhost_with_dataObject Also known as: replace_localhost

#

consider_replacing_localhost_with_data

#


152
153
154
155
156
# 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_ignore_helpObject

#

do_ignore_help

#


355
356
357
# File 'lib/beautiful_url/class/misc.rb', line 355

def do_ignore_help
  @ignore_help = true
end

#do_replace_data_entry_with_localhostObject Also known as: do_replace_data_with_localhost

#

do_replace_data_entry_with_localhost

#


170
171
172
173
174
175
176
177
178
179
# 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_entryObject Also known as: get_rid_of_localhost, do_replace_localhost_with_data, do_replace_localhost

#

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
#


240
241
242
243
244
245
246
247
248
249
# 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_shellObject Also known as: generate_tab_completion

#

generate_tab_completion_for_bash_shell

This entry point will generate the tab-completion for the BASH shell.

Invocation examples:

purl GENERATE
purl --generate
#


26
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
55
56
57
58
59
60
61
62
63
64
65
# 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_mappedObject Also known as: consider_replacing_everything, consider_replacing_anything

#

handle_additional_actions_after_the_input_arguments_were_already_mapped

#


161
162
163
164
# 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

#home_dir_of_user_x?Boolean

#

home_dir_of_user_x?

#

Returns:

  • (Boolean)


348
349
350
# File 'lib/beautiful_url/class/misc.rb', line 348

def home_dir_of_user_x?
  BASE_DIR
end

#is_a_remote_url?Boolean Also known as: remote?, is_a_remote_file?

#

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.

#

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
# 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

#is_registered?Boolean Also known as: url_was_found?, url_was_found, is_included?

#

is_registered?

#

Returns:

  • (Boolean)


219
220
221
# File 'lib/beautiful_url/class/misc.rb', line 219

def is_registered?
  @is_registered
end

#map_the_input_arguments_to_their_corresponding_values(i = @commandline_arguments) ⇒ Object Also known as: check_input_against_menu, sanitize_input

#

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.

#


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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
#

menu (menu tag)

#


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/beautiful_url/class/misc.rb', line 184

def menu(
    i = @commandline_arguments
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === rf --show_help
    # ===================================================================== #
    when /^-?-?show(_|-)?help$/i,
         /^-?-?help$/i
      show_help
    # ===================================================================== #
    # === rf --generate-tab
    # or
    # === purl GENERATE
    # ===================================================================== #
    when 'AUTOGENERATED','GENERATE',
         '--generate',
         '--autogenerate',
         /^-?-?generate?(-|_)?completion$/,
         'generate_shell',
         /^GENE/,
         '--tab',
         '--generate-tab' # Could also be 'GENERATE','GENE'
      generate_tab_completion_for_bash_shell # This here is in another file.
      exit
    end
  end
end

#replace_localhost_with_data?Boolean Also known as: replace_localhost_with_data

#

replace_localhost_with_data?

#

Returns:

  • (Boolean)


268
269
270
# File 'lib/beautiful_url/class/misc.rb', line 268

def replace_localhost_with_data?
  @replace_localhost_with_data
end

#resetObject

#

reset (reset tag)

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#results?Boolean

#

results?

#

Returns:

  • (Boolean)


334
335
336
# File 'lib/beautiful_url/class/misc.rb', line 334

def results?
  @commandline_arguments
end

#return_corresponding_entriesObject Also known as: input?, data, array, array?

#

return_corresponding_entries

#


317
318
319
# File 'lib/beautiful_url/class/misc.rb', line 317

def return_corresponding_entries
  @commandline_arguments
end

#return_corresponding_entryObject

#

return_corresponding_entry

#


327
328
329
# File 'lib/beautiful_url/class/misc.rb', line 327

def return_corresponding_entry
  @commandline_arguments.first
end

#runObject

#

run

#


14
15
16
17
18
# File 'lib/beautiful_url/class/run.rb', line 14

def run
  menu
  map_the_input_arguments_to_their_corresponding_values
  handle_additional_actions_after_the_input_arguments_were_already_mapped
end

#search_for_this_url(i) ⇒ Object

#

search_for_this_url

This will return a string.

#


133
134
135
136
137
138
# 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_commandline_arguments(i = :default) ⇒ Object

#

set_commandline_arguments

#


256
257
258
259
260
261
262
263
# File 'lib/beautiful_url/class/misc.rb', line 256

def set_commandline_arguments(i = :default)
  case i
  when nil
    i = DEFAULT_URL
  end
  i = [i].flatten.compact
  @commandline_arguments = i
end

#set_data_dir(i = :default) ⇒ Object

#

set_data_dir

We need this method so that the user can define another data-dir that is to be used.

#


300
301
302
303
304
305
306
307
308
309
310
311
312
# 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
#

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.

#


281
282
283
284
285
286
287
288
289
290
291
292
# 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(i = true) ⇒ Object Also known as: set_replace_localhost, shall_we_replace_localhost=

#

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.
#


121
122
123
124
125
# 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(shall_we_exit = :then_exit) ⇒ Object

#

show_help

#


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# 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

#show_resultObject

#

show_result

#


341
342
343
# File 'lib/beautiful_url/class/misc.rb', line 341

def show_result
  e return_corresponding_entry
end

#url_as_stringObject Also known as: url?, location?, string, string?, result?, result, return_as_string, []

#

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.

#


387
388
389
# File 'lib/beautiful_url/class/misc.rb', line 387

def url_as_string
  return_corresponding_entry.to_s
end