Class: Eymiha::UrlOpener
- Inherits:
-
Object
- Object
- Eymiha::UrlOpener
- Defined in:
- lib/eymiha/url/url_opener.rb
Overview
UrlOpener opens a URL in a user’s URL viewer of choice. URLs are normalized prior to being opened.
Instances use the Kernel::system method to open the resource in its own Thread to avoid blocking. Any problems are raised through a caller-supplied exception handler’s handle(Exception) method.
For Apple and Windows, the viewer-of-choice is defaulted to the user’s designated viewer in the environment. For other platforms, the caller must assign the viewer prior to opening the URL.
Constant Summary collapse
- @@url_viewer =
nil
- @@relative_base =
nil
- @@exception_handler =
nil
Instance Attribute Summary collapse
-
#exception_handler ⇒ Object
Returns the value of attribute exception_handler.
-
#relative_base ⇒ Object
Returns the value of attribute relative_base.
-
#url_viewer ⇒ Object
Returns the value of attribute url_viewer.
Class Method Summary collapse
-
.exception_handler ⇒ Object
Returns the default exception handler.
-
.exception_handler=(exception_handler) ⇒ Object
Sets and returns the default exception handler.
-
.relative_base ⇒ Object
Returns the default directory for opening relative file resources.
-
.relative_base=(relative_base) ⇒ Object
Sets and returns the default directory for opening relative file resources.
-
.url_viewer ⇒ Object
Returns the user’s default URL viewer-of-choice.
-
.url_viewer=(url_viewer) ⇒ Object
Sets and returns the user’s default URL viewer-or-choice.
Instance Method Summary collapse
-
#initialize(exception_handler = @@exception_handler, relative_base = @@relative_base, url_viewer = @@url_viewer) ⇒ UrlOpener
constructor
Constructs and returns a new instance, using the default exception handler, directory for opening relative file resources, and viewer of choice.
- #method_missing(method, *args) ⇒ Object
-
#open(url) ⇒ Object
opens the given URL in the user’s viewer of choice.
Constructor Details
#initialize(exception_handler = @@exception_handler, relative_base = @@relative_base, url_viewer = @@url_viewer) ⇒ UrlOpener
Constructs and returns a new instance, using the default exception handler, directory for opening relative file resources, and viewer of choice.
71 72 73 74 75 76 77 78 |
# File 'lib/eymiha/url/url_opener.rb', line 71 def initialize(exception_handler=@@exception_handler, relative_base=@@relative_base, url_viewer=@@url_viewer) @exception_handler = exception_handler @relative_base = relative_base @url_viewer = url_viewer @url_normalizer = UrlNormalizer.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
107 108 109 |
# File 'lib/eymiha/url/url_opener.rb', line 107 def method_missing(method,*args) open_other(*args) if method.to_s =~ /^open_/ end |
Instance Attribute Details
#exception_handler ⇒ Object
Returns the value of attribute exception_handler.
65 66 67 |
# File 'lib/eymiha/url/url_opener.rb', line 65 def exception_handler @exception_handler end |
#relative_base ⇒ Object
Returns the value of attribute relative_base.
66 67 68 |
# File 'lib/eymiha/url/url_opener.rb', line 66 def relative_base @relative_base end |
#url_viewer ⇒ Object
Returns the value of attribute url_viewer.
65 66 67 |
# File 'lib/eymiha/url/url_opener.rb', line 65 def url_viewer @url_viewer end |
Class Method Details
.exception_handler ⇒ Object
Returns the default exception handler.
61 62 63 |
# File 'lib/eymiha/url/url_opener.rb', line 61 def self.exception_handler @@exception_handler end |
.exception_handler=(exception_handler) ⇒ Object
Sets and returns the default exception handler. Problems are reported through its handle method, so it should either have the method, or construct it or delegate at runtime.
56 57 58 |
# File 'lib/eymiha/url/url_opener.rb', line 56 def self.exception_handler=(exception_handler) @@exceptionHandler = exception_handler end |
.relative_base ⇒ Object
Returns the default directory for opening relative file resources.
47 48 49 |
# File 'lib/eymiha/url/url_opener.rb', line 47 def self.relative_base @@relative_base end |
.relative_base=(relative_base) ⇒ Object
Sets and returns the default directory for opening relative file resources. The directory is validated prior to assignment - if invalid a UrlException will be raised.
38 39 40 41 42 43 44 |
# File 'lib/eymiha/url/url_opener.rb', line 38 def self.relative_base=(relative_base) if UrlNormalizer.valid_relative_base? relative_base @@relative_base = relative_base else raise UrlException, "Invalid relative file base '#{relative_base}'" end end |
.url_viewer ⇒ Object
Returns the user’s default URL viewer-of-choice.
29 30 31 |
# File 'lib/eymiha/url/url_opener.rb', line 29 def self.url_viewer @@url_viewer end |
.url_viewer=(url_viewer) ⇒ Object
Sets and returns the user’s default URL viewer-or-choice.
24 25 26 |
# File 'lib/eymiha/url/url_opener.rb', line 24 def self.url_viewer=(url_viewer) @@url_viewer = url_viewer end |
Instance Method Details
#open(url) ⇒ Object
opens the given URL in the user’s viewer of choice. The method uses a separate Thread to avoid blocking. Local file resources are opened relative to the value of the current relative base directory, and any problems are reported through the exception handler.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/eymiha/url/url_opener.rb', line 95 def open url Thread.new { begin send "open_#{Config::CONFIG['target_vendor']}".to_sym, @url_normalizer.normalize(url.to_s,@relative_base) rescue Exception => exception @exception_handler.handle exception end terminate } end |