File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/bin/env ruby
2
+ #
3
+ # Open the default browser to a URL constructed from the root
4
+ # and the args
5
+ #
6
+
7
+ require 'cgi'
8
+
9
+ # Use Launchy for anything but jruby
10
+
11
+ unless defined? JRUBY_VERSION
12
+ require 'launchy'
13
+ end
14
+
15
+ class Browse
16
+
17
+ def initialize root
18
+ @root = root
19
+ @ready_for_args = false
20
+ if @root =~ /\? /
21
+ @ready_for_args = true
22
+ end
23
+
24
+ if defined? JRUBY_VERSION
25
+ singleton_class = class << self ; self ; end
26
+ singleton_class . send ( :define_method , :browse , method ( :jruby_browse ) )
27
+ @@desktop = Java ::java . awt . Desktop . desktop
28
+ end
29
+ end
30
+
31
+ def urlify args = { }
32
+ return root if args . size == 0
33
+ unless @ready_for_args
34
+ raise "There's no '?' in your root url '#{ @root } '; can't add args"
35
+ end
36
+ return @root + args . map { |k , v | "#{ k } =#{ CGI ::escape ( v ) } " } . join ( '&' )
37
+ end
38
+
39
+ def jruby_browse args = { }
40
+ @@desktop . browse ( Java ::java . net . URI . new ( urlify args ) )
41
+ end
42
+
43
+ def browse args = { }
44
+ Launchy . open urlify ( args )
45
+ end
46
+ end
47
+
48
+
49
+ if __FILE__ == $0
50
+ b = Browse . new ( 'http://mirlyn.lib.umich.edu/Search/Home?' )
51
+ if defined? JRUBY_VERSION
52
+ b . browse ( { 'lookfor' => 'jruby' } )
53
+ else
54
+ b . browse ( { 'lookfor' => 'ruby' } )
55
+ end
56
+ end
57
+
58
+
You can’t perform that action at this time.
0 commit comments