Skip to content

Commit 919d37c

Browse files
committed
Testing ruby code
1 parent e2a69e3 commit 919d37c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

ruby/browse.rb

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+

0 commit comments

Comments
 (0)