Class: OpenInBrowser

Inherits:
Object
  • Object
show all
Defined in:
lib/open_in_browser/constants.rb,
lib/open_in_browser/open_in_browser.rb,
lib/open_in_browser/version/version.rb

Overview

#

require 'open_in_browser/version/version.rb'

#

Constant Summary collapse

DATA_DIRECTORY_AT_HOME =
#

DATA_DIRECTORY_AT_HOME

#
'/home/x/DATA/'
LOCATION_OF_BROWSER_YAML_FILE =
#

LOCATION_OF_BROWSER_YAML_FILE

Here we must defined where we store the location for our browser.

That file will tell us which browser to use.

cat /Users/x/DATA/PC/yaml/browser.yml
#
"#{DATA_DIRECTORY_AT_HOME}PC/yaml/browser.yml"
VERSION =
#

OpenInBrowser::VERSION

#
'1.0.32'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_url = '', run_already = true) ⇒ OpenInBrowser

#

initialize

#

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/open_in_browser/open_in_browser.rb', line 35

def initialize(
    target_url  = '',
    run_already = true
  )
  register_sigint
  reset
  set_target_url(
    target_url
  )
  if block_given?
    yielded = yield
    case yielded
    when :be_silent,
         :be_quiet
      @show_the_sys_command_that_is_used = false
    end
  end
  run if run_already
end

Class Method Details

.open_in_browser(i) ⇒ Object

#

OpenInBrowser.open_in_browser

#

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/open_in_browser/open_in_browser.rb', line 214

def self.open_in_browser(i)
  if block_given?
    yielded = yield
    if yielded.is_a? Symbol
      case yielded
      when :be_silent,
           :be_quiet
        OpenInBrowser.new(i) { yielded }
        return
      end
    elsif yielded.is_a? Hash      # =================================================================== #
      # === Handle Hashes past this point
      # =================================================================== #

      if yielded.has_key? :delay
        _ = yielded.delete(:delay)
        if _.is_a? String
          _ = _.sub(/ seconds/,'')
          _ = _.sub(/ second/,'').to_f if _.include? ' second'
          _ = _.to_f # Need a Float.
        end
        sleep(_)
      end
    end
  end
  OpenInBrowser.new(i)
end

Instance Method Details

#

The second optional argument specifies the token to use for replacing empty spaces, if we have passed an Array as argument.

#

111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/open_in_browser/open_in_browser.rb', line 111

def check_for_intralink(
    i                                      = nil,
    use_this_token_to_replace_empty_spaces = '_'
  )
  if i
    if i.is_a? Array
      i = i.join(' ').tr(' ', use_this_token_to_replace_empty_spaces)
    end
    i = i.to_s # Ensure that we have a String at this point.
    @append_this_string << i
  end
end

#esystem(i, do_show_the_command_that_will_be_used = true) ⇒ Object

#

esystem

#

73
74
75
76
77
78
# File 'lib/open_in_browser/open_in_browser.rb', line 73

def esystem(
    i, do_show_the_command_that_will_be_used = true
  )
  e i if do_show_the_command_that_will_be_used
  system(i)
end

#is_on_roebe?Boolean

#

is_on_roebe?

#

Returns:

  • (Boolean)

127
128
129
# File 'lib/open_in_browser/open_in_browser.rb', line 127

def is_on_roebe?
  ENV['IS_ROEBE'].to_s == '1'
end

#open_in_browser(optional_append_this_string = @append_this_string) ⇒ Object

#

open_in_browser

This is the part that does the actual file-opening via the browser.

#

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/open_in_browser/open_in_browser.rb', line 136

def open_in_browser(
    optional_append_this_string = @append_this_string
  )
  _ = LOCATION_OF_BROWSER_YAML_FILE  # ======================================================================= #
  # We must check first whether the file exists or not.
  # ======================================================================= #

  if File.exist? _
    browser = YAML.load_file(_) # This variable holds which browser to use.
    # ===================================================================== #
    # Different browsers have different ways to open a new tab.
    # ===================================================================== #
    if browser.include? 'opera' # Perform some sanitizing in this case.
      browser << ' -newtab'      # =================================================================== #
      # Or alternatively:
      #   opera --remote 'openURL(<url>, new-page)'
      # =================================================================== #

    elsif browser.include? 'firefox'
      browser << ' -new-tab'
    end
    @target_url.each {|entry|
      entry = entry.to_s.dup # .dup to avoid that it may be frozen.
      if optional_append_this_string.size > 0        # ================================================================= #
        # We prepend the '#' character, unless it starts with the
        # '?' character.
        # ================================================================= #

        unless optional_append_this_string.start_with? '?'
          optional_append_this_string.prepend '#'
        end
        entry << optional_append_this_string
      end      # =================================================================== #
      # Next, specifically handle the situation on my home system, when
      # we are in a directory that includes a substring such as
      # '/Users/x/DATA/' and if the target file name ends with '.cgi'.
      # =================================================================== #

      if is_on_roebe? and
         Dir.pwd.include?(DATA_DIRECTORY_AT_HOME) and
         entry.end_with?('.cgi') and
        !entry.start_with?('http://localhost/')        # ================================================================= #
        # We need a target URL such as:
        #   http://localhost/DATA/GAMES/AITD/AITD.cgi
        # in this case.
        # ================================================================= #

        unless entry.include? DATA_DIRECTORY_AT_HOME
          entry.prepend((Dir.pwd+'/'))
        end
        entry.squeeze!('/')
        quoted_regexp = Regexp.quote(DATA_DIRECTORY_AT_HOME)
        entry.sub!(quoted_regexp, 'http://localhost/DATA/')
      end
      if entry.include? '&'
        entry = '"'+entry+'"' unless entry.start_with? '"'
      end
      this_command = "#{browser} #{entry}"
      if @show_the_sys_command_that_is_used
        e this_command
      end
      system(this_command) # Here we always output.
    } unless @target_url.nil?
  else
    browser = 'firefox' # Else, default to this here as a generic fallback.
  end
end

#register_sigintObject

#

register_sigint

#

58
59
60
# File 'lib/open_in_browser/open_in_browser.rb', line 58

def register_sigint
  Signal.trap('SIGINT') { exit }
end

#resetObject

#

reset (reset tag)

#

65
66
67
68
# File 'lib/open_in_browser/open_in_browser.rb', line 65

def reset
  @append_this_string = ''.dup
  @show_the_sys_command_that_is_used = true
end

#runObject

#

run (run tag)

#

207
208
209
# File 'lib/open_in_browser/open_in_browser.rb', line 207

def run 
  open_in_browser
end

#set_target_url(i = '') ⇒ Object

#

set_target_url

We prefer this to be an Array.

#

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/open_in_browser/open_in_browser.rb', line 85

def set_target_url(i = '')
  i = [i] unless i.is_a? Array
  i.flatten! if i.is_a? Array
  i.map! {|entry|
    case entry
    when :default_sinatra_port
      entry = 'http://localhost:4567/'
    end    # ===================================================================== #
    # Perform some sanitize-operations next.
    # ===================================================================== #

    if (entry.include?("'") and !entry.include?('"')) or
        entry.include?(' ')
      entry = '"'+entry+'"'
    end
    entry
  }
  @target_url = i
end