Class: ChemistryParadise::Wetter

Inherits:
Base
  • Object
show all
Defined in:
lib/chemistry_paradise/wetter/wetter.rb,
lib/chemistry_paradise/sinatra/wetter/app.rb,
lib/chemistry_paradise/www/wetter/embeddable_interface.rb,
lib/chemistry_paradise/sinatra/wetter/start_sinatra_interface.rb

Overview

ChemistryParadise::Wetter

Defined Under Namespace

Modules: EmbeddableInterface Classes: Sinatra

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
REMOTE_URL =
#

REMOTE_URL

Point to the remote website that holds the information for the local temperature in the city of Vienna.

#
'https://www.zamg.ac.at/cms/de/wetter/wetterwerte-analysen/wien'
TRY_TO_MAKE_USE_OF_THE_UNICODE_WEATHER_SYMBOL =
#

TRY_TO_MAKE_USE_OF_THE_UNICODE_WEATHER_SYMBOL

If the next constant is set to true then class Wetter will try to make use of the unicode symbol for “cloudy weather”.

#
true
USE_THIS_REGEX =
#

USE_THIS_REGEX

This is the regex handling some remote “Temp:” String.

#
/Temp: (-?\d+.\d+)/
USE_DEGREE_REGEX =
#

USE_DEGREE_REGEX

This second regex can be used when we wish to match against &deg values.

#
/\>(\d+.\d+)&deg;\</

Constants inherited from Base

Base::FILE_MOLECULAR_FORMULA_OF_DIFFERENT_MOLECULES

Constants included from Shared

Shared::ARRAY_TEST_THESE_MOLECULES

Constants included from Constants

Constants::ELECTRON_NEGATIVITY_CHART, Constants::FILE_ATOMGEWICHTE, Constants::FILE_ELECTRON_NEGATIVITY_CHART, Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS, Constants::N, Constants::PLANK_CONSTANT, Constants::PROPER_FILLORDER, Constants::SPEED_OF_LIGHT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#be_quiet, #be_verbose?, #cd, #cliner, #commandline_arguments?, #do_use_the_english_language, #do_use_the_german_language, #do_we_use_english?, #esystem, #first_argument?, #gold, #grey, #initialize_the_internal_hash, #internal_hash?, #is_on_roebe?, #mediumpurple, #namespace?, #olivedrab, #opnn, #rev, #royalblue, #set_be_verbose, #set_commandline_arguments, #sfancy, #steelblue, #teal, #tomato, #use_which_language?, #yellow

Methods included from Shared

#convert_parens, #is_number?, #periodic_table?, periodic_table?, #return_range_for_this_period, #square

Methods included from Constants

#electron_negativity_chart?

Constructor Details

#initialize(i = ARGV, run_already = true, &block) ⇒ Wetter

#

initialize

#


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 81

def initialize(
    i           = ARGV, # The commandline arguments are stored here.
    run_already = true,
    &block
  )
  reset
  set_commandline_arguments(i)
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_silent
    # ===================================================================== #
    when :be_silent
      set_be_quiet
    end
  end
  run if run_already
end

Class Method Details

.current_degreesObject

#

ChemistryParadise::Wetter.current_degrees

This method will simply return a Float number, the current temperature in Vienna by default.

Usage examples:

ChemistryParadise::Wetter.current_degrees
ChemistryParadise::Wetter.simplified_current_degrees
#


490
491
492
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 490

def self.current_degrees
  new { :be_silent }.match?.to_f
end

.gtk3_widgetObject

#

ChemistryParadise::Wetter.gtk3_widget

Simpler toplevel-method to obtain a new gtk-widget instance for the wetter project. It will only work if the gtk3 code has been required prior to calling this method.

#


515
516
517
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 515

def self.gtk3_widget
  ::Wetter::GUI::Wetter.gtk3_widget
end

.is_on_roebe?Boolean

#

ChemistryParadise::Wetter.is_on_roebe?

#

Returns:

  • (Boolean)


474
475
476
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 474

def self.is_on_roebe?
  ChemistryParadise.send(__method__)
end
#

ChemistryParadise::Wetter.menu (menu tag)

#


402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 402

def self.menu(i)
  if i.is_a? Array
    if i.empty?
      new(i)
    else
      i.each {|entry| menu(entry) }
    end
  else
    case i
    # ===================================================================== #
    # === wetter --libui
    # ===================================================================== #
    when /^-?-?libui$/i
      puts 'Starting the libui wetter (aka "weather") application next.'
      require 'chemistry_paradise/gui/libui/wetter/wetter.rb'
      ChemistryParadise::GUI::LibUI::Wetter.new
      exit
    # ===================================================================== #
    # === wetter --sinatra
    #
    # This entry point starts the sinatra-interface of the wetter
    # project.
    #
    # Invocation example:
    #
    #   wetter --sinatra
    #
    # ===================================================================== #
    when /^-?-?sinatra$/i,
         /^-?-?www$/i
      require 'chemistry_paradise/sinatra/wetter/start_sinatra_interface.rb'
      ChemistryParadise::Wetter.start_sinatra_interface
    # ===================================================================== #
    # === wetter --gtk3
    # ===================================================================== #
    when /^-?-?gtk3?$/i,
         /^-?-?gui$/i # === wetter --gui
      puts 'Starting the ruby-gtk3 wetter (aka "weather") application next.'
      require 'chemistry_paradise/gui/gtk3/wetter/wetter.rb'
      ChemistryParadise::GUI::Gtk::Wetter.run
      exit
    # ===================================================================== #
    # === wetter --temperature-only
    # ===================================================================== #
    when /^-?-?temperature(-|_)?only$/i,
         /^-?-?temperature\??$/i
      puts Wetter.temperature?
      exit
    # ===================================================================== #
    # === wetter --gtk2
    # ===================================================================== #
    when /^-?-?gtk2$/i
      e 'Support for ruby-gtk2 has been removed in November 2021.'
      e 'Please use the ruby-gtk3 variant instead.'
      exit
    else
      Wetter.new(i)
    end
  end
end

.project_base_directory?Boolean

#

ChemistryParadise::Wetter.project_base_directory?

#

Returns:

  • (Boolean)


466
467
468
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 466

def self.project_base_directory?
  "#{ChemistryParadise.project_base_directory?}wetter/"
end

.run(i = ARGV) ⇒ Object

#

ChemistryParadise::Wetter.run

#


502
503
504
505
506
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 502

def self.run(
    i = ARGV
  )
  new(i)
end

.start_sinatra_interfaceObject

#

ChemistryParadise::Wetter.start_sinatra_interface

This method can be used to start the sinatra interface.

#


22
23
24
25
# File 'lib/chemistry_paradise/sinatra/wetter/start_sinatra_interface.rb', line 22

def self.start_sinatra_interface
  puts 'Trying to start the sinatra-interface for class Wetter next.'
  ::ChemistryParadise::Wetter::Sinatra.run!
end

Instance Method Details

#data?Boolean

#

data?

#

Returns:

  • (Boolean)


219
220
221
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 219

def data?
  @internal_hash[:data]
end

#debug?Boolean

#

debug?

#

Returns:

  • (Boolean)


181
182
183
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 181

def debug?
  @internal_hash[:debug]
end

#enable_debug_modeObject

#

enable_debug_mode

#


174
175
176
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 174

def enable_debug_mode
  @internal_hash[:debug] = true
end

#find_the_appropriate_matchObject

#

find_the_appropriate_match

This will find the current degree, in Celsius.

#


350
351
352
353
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 350

def find_the_appropriate_match
  @internal_hash[:data] =~ USE_THIS_REGEX # Apply the appropriate regex here.
  @internal_hash[:match] = $1.to_s.dup
end

#local_file?Boolean

#

local_file?

#

Returns:

  • (Boolean)


233
234
235
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 233

def local_file?
  File.absolute_path(File.basename(remote_URL?)+'.html')
end

#match?Boolean

#

match?

#

Returns:

  • (Boolean)


167
168
169
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 167

def match?
  @internal_hash[:match]
end
#

menu (menu tag)

#


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 136

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === help
    # ===================================================================== #
    when /^-?-?help$/i
      show_help
      exit
    # ===================================================================== #
    # === --URL
    # ===================================================================== #
    when /^-?-?URL$/i
      show_the_remote_url
      exit
    # ===================================================================== #
    # === debug
    # ===================================================================== #
    when /debug/
      enable_debug_mode
    end
  end
end

#read_in_the_dataset_from_the_remote_webpageObject

#

read_in_the_dataset_from_the_remote_webpage

This method has to catch OpenSSL errors.

#


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 288

def read_in_the_dataset_from_the_remote_webpage
  begin
    @internal_hash[:data] = URI.open(remote_URL?).read
  rescue OpenSSL::SSL::SSLError => _error
    # ===================================================================== #
    # Here we know that an OpenSSL connection error occurred.
    # We will notify the user about this but only one sentence.
    # ===================================================================== #
    _ = local_file?
    if File.exist? _
      if be_verbose and shall_we_report_openssl_errors?
        opn; e 'An OpenSSL-error occurred. Trying to read from the'
        opn; e 'local file at '+sfile(_)+' next.'
      end
      internal_hash[:data] = File.read(_)
    else
      e 'An OpenSSL-error occurred. Can not continue.' if be_verbose
    end
  end
  if debug?
    opn; e 'Now showing @internal_hash[:data], as we run in debug-mode.'
    e cliner
    e data?
    e cliner
  end
  return @internal_hash[:data]
end

#remote_URL?Boolean

#

remote_URL?

#

Returns:

  • (Boolean)


226
227
228
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 226

def remote_URL?
  REMOTE_URL
end

#report_the_resultObject Also known as: report

#

report_the_result

#


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 358

def report_the_result
  if be_verbose?
    n_degrees_celsius = match?.to_f
    result = "#{olivedrab('The current temperature in Vienna is: ')}"\
             "#{sfancy(n_degrees_celsius)} #{gold('°C')}".dup
    # ===================================================================== #
    # Next we have to check if we use a fancy unicode weather symbol:
    # ===================================================================== #
    if try_to_make_use_of_the_unicode_weather_symbol?
      begin
        require 'roebe/toplevel_methods/unicode/popular_unicode_symbols.rb'
        case n_degrees_celsius
        when  16 .. 50 # Should be clear weather most of the time.
          result << " #{tomato(Roebe.clear_weather)}"
        when   5 .. 15 # Medium-temperature; usually cloudy.
          result << " #{royalblue(Roebe.cloudy_weather_symbol)}"
        when -35 .. 4 # It's really quite cold.
          result << " #{royalblue(Roebe.snowman)}"
        end
      rescue LoadError
      end
    end
    # ===================================================================== #
    # Next display which class reports the output as well:
    # ===================================================================== #
    opn; e result
  end
end

#resetObject

#

reset (reset tag)

#


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 107

def reset
  super()
  initialize_the_internal_hash
  # ======================================================================= #
  # === :debug
  # ======================================================================= #
  @internal_hash[:debug] = false
  # ======================================================================= #
  # === :data
  # ======================================================================= #
  @internal_hash[:data] = nil
  # ======================================================================= #
  # === :match
  # ======================================================================= #
  @internal_hash[:match] = nil
  # ======================================================================= #
  # === :shall_we_report_openssl_errors
  # ======================================================================= #
  @internal_hash[:shall_we_report_openssl_errors] = false
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  be_verbose
end

#runObject

#

run (run tag)

#


390
391
392
393
394
395
396
397
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 390

def run
  menu
  try_to_download_the_remote_webpage
  read_in_the_dataset_from_the_remote_webpage
  sanitize_the_dataset
  find_the_appropriate_match
  report
end

#sanitize_the_datasetObject

#

sanitize_the_dataset

#


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 326

def sanitize_the_dataset
  splitted = data?.split(N)
  splitted.reject! {|line|
    ! (line.include?('title="Wien Innere Stadt') and line.include?('Temp: '))
  }
  splitted = splitted.first if splitted.is_a? Array
  if debug?
    opn; e 'Now showing the sanitized dataset, as we run in debug-mode.'
    pp splitted
  end
  @internal_hash[:data] = splitted # Re-assign it here.
  if data?.nil?
    @internal_hash[:data] =~ USE_DEGREE_REGEX
    unless $1.to_s.dup.empty?
      @internal_hash[:data] = $1.to_s.dup
    end
  end
end

#shall_we_report_openssl_errors?Boolean

#

shall_we_report_openssl_errors?

#

Returns:

  • (Boolean)


319
320
321
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 319

def shall_we_report_openssl_errors?
  @internal_hash[:shall_we_report_openssl_errors]
end

#show_helpObject

#

show_help

#


188
189
190
191
192
193
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 188

def show_help
  e
  e '  --URL     # show the remote URL'
  e '  --sinatra # start the sinatra-interface of this gem'
  e
end

#show_the_remote_urlObject

#

show_the_remote_url

To invoke this method, do:

wetter --URL
#


203
204
205
206
207
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 203

def show_the_remote_url
  e
  e sfancy("  #{REMOTE_URL}")
  e
end

#try_to_download_the_remote_webpage(remote_URL = remote_URL?, , be_verbose = be_verbose? ) ⇒ Object

#

try_to_download_the_remote_webpage

This method was specifically added in February 2023, to bypass any issue we may have had when openssl is not available. It will rely on “wget” to download the webpage.

#


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 244

def try_to_download_the_remote_webpage(
    remote_URL = remote_URL?,
    be_verbose = be_verbose?
  )
  base_dir = '/home/x/Temp/wetter/'
  if is_on_roebe? and !File.directory?(base_dir)
    require 'fileutils'
    FileUtils.mkdir_p(base_dir)
  end
  cd(base_dir) if File.directory?(base_dir)
  local_file = File.absolute_path(File.basename(remote_URL)+'.html')
  if File.exist?(local_file)
    File.delete(local_file) # Remove any old file first.
  end
  if File.exist?(local_file.delete_suffix('.html'))
    File.delete(local_file.delete_suffix('.html')) # Remove any old file first.
  end
  # ======================================================================= #
  # Next, download the remote file.
  # ======================================================================= #
  esystem(
    "wget #{remote_URL}", :silence_output_from_system
  ) { :be_quiet }
  unless File.exist?(local_file)
    require 'fileutils'
    if File.exist? local_file.delete_suffix('.html')
      FileUtils.mv(
        local_file.delete_suffix('.html'),
        local_file
      )
    end
  end
  if File.exist? local_file and
     be_verbose and
     shall_we_report_openssl_errors?
    opn; e "The remote file was downloaded into `#{sfile(local_file)}`."
  end
end

#try_to_make_use_of_the_unicode_weather_symbol?Boolean

#

try_to_make_use_of_the_unicode_weather_symbol?

#

Returns:

  • (Boolean)


212
213
214
# File 'lib/chemistry_paradise/wetter/wetter.rb', line 212

def try_to_make_use_of_the_unicode_weather_symbol?
  TRY_TO_MAKE_USE_OF_THE_UNICODE_WEATHER_SYMBOL
end