Cory O'Daniel – These are just words Software development, thoughts, and randomness

25Feb/102

A ruby wrapper for Google AjaxLibs (jquery, jquery ui, mootools, prototype, swfobject, etc) [Lazy GoogleJsApi Wrapper]

I like using the Google AjaxLibs API. Its cool to not host files when I don't have to, especially ones I know a user has already probably cached from Google anyway (even though Google Page speed busts my balls about too many DNS lookups :P ).

I do on the other hand hate watching in the bottom of my browser "Waiting for google.com" when using the standard Javascript API for loading the Libraries like so:

1
2
3
4
<script type="text/javascript" src="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
<script type="text/javascript">
 google.load("jquery", "1.4.2");
</script>

And you don't have to wait, you can totally do something like:

1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Which is why I wrote this little ruby wrapper. Its nothing special. It just encapsulates all the URLs for the libraries and their versions to allow you to not remember the URL and not have to go looking for the documentation when you start a new app or just need an additional library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Docs: http://code.google.com/apis/ajaxlibs/documentation/index.html
#
class GoogleJsApi
  BaseURL = "http://ajax.googleapis.com/ajax/libs".freeze
 
  Libraries = { 
    "jquery" => {
      :versions => %w(1.2.3 1.2.6 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2),
      :compressed_url => "/jquery/%s/jquery.min.js",
      :uncompressed_url => "/jquery/%s/jquery.js"
    },
    "jqueryui" => {
      :versions => %w(1.5.2 1.5.3 1.6 1.7.0 1.7.1 1.7.2),
      :compressed_url   => "/jqueryui/%s/jquery-ui.min.js",
      :uncompressed_url => "/jqueryui/%s/jquery-ui.js"
    },
    "prototype" => {
      :versions => %w(1.6.0.2 1.6.0.3 1.6.1.0),
      :compressed_url   => "",
      :uncompressed_url => "/prototype/%s/prototype.js"
    },
    "scriptaculous" => {
      :versions => %w(1.8.1 1.8.2 1.8.3),
      :compressed_url   => "",
      :uncompressed_url => "/scriptaculous/%s/scriptaculous.js"
    },
    "mootools" => {
      :versions => %w(1.1.1 1.1.2 1.2.1 1.2.2 1.2.3 1.2.4),
      :compressed_url => "/mootools/%s/mootools-yui-compressed.js",
      :uncompressed_url => "/mootools/%s/mootools.js"
    },
    "dojo" => {
      :versions => %w(1.1.1 1.2.0 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1),
      :compressed_url => "/dojo/%s/dojo/dojo.xd.js",
      :uncompressed_url => "/dojo/%s/dojo/dojo.xd.js.uncompressed.js"
    },
    "swfobject" => {
      :versions => %w(2.1 2.2),
      :compressed_url   => "/swfobject/%s/swfobject.js",
      :uncompressed_url => "/swfobject/%s/swfobject_src.js"
    },
    "yui" => {
      :versions => %w(2.6.0 2.7.0 2.8.0r4),
      :compressed_url   => "/yui/%s/build/yuiloader/yuiloader-min.js",
      :uncompressed_url => "/yui/%s/build/yuiloader/yuiloader.js"
    },
    "ext-core" => {
      :versions => %w(3.0.0 3.1.0),
      :compressed_url   => "/ext-core/%s/ext-core.js",
      :uncompressed_url => "/ext-core/%s/ext-core-debug.js"
    },
    "chrome-frame" => {
      :versions => %w(1.0.0 1.0.1 1.0.2),
      :compressed_url   => "/chrome-frame/%s/CFInstall.min.js",
      :uncompressed_url => "/chrome-frame/%s/CFInstall.js"
    } 
  }.freeze
 
  class << self
    def include(name, version=nil, compressed=true, validate_version=true)
      name = name.to_s
      if lib = GoogleJsApi::Libraries[name]
        version ||= lib[:versions].last
 
        if validate_version && lib[:versions].include?(version)
          GoogleJsApi.url_for(name, version, compressed)
        elsif !validate_version
          GoogleJsApi.url_for(name, version, compressed)
        else
          raise Exception, "Invalid version (#{version}) for #{name}"
        end
      else
        raise Exception, "Unknown Google Javascript Library"
      end
    end
 
    def version_info
      tmp_version = {}
      GoogleJsApi::Libraries.each do |k,v|
        tmp_version[ k ] = v[:versions]
      end
      tmp_version
    end
 
    protected
    def url_for(name, version, compressed)
      GoogleJsApi::BaseURL +
      GoogleJsApi::Libraries[ name ][ compressed ? :compressed_url : :uncompressed_url ] % version
    end
  end
 
end

Here are some tests for it if you are interested in how/if it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'pp'
 
pp GoogleJsApi.version_info
 
# Include the newest version of a library
puts GoogleJsApi.include('jquery')
 
# Include a specific version of a library
puts GoogleJsApi.include "chrome-frame", "1.0.0"
 
# Include a specific version of a library uncompressed
puts GoogleJsApi.include "chrome-frame", "1.0.0", false
 
# Include a specific version of a library w/o validating the version; useful if this goes out of date :P
puts GoogleJsApi.include "jquery", "NOT_A_VERSION", false, false
 
begin
  puts GoogleJsApi.include :jqueryui, "1.0"
rescue Exception => e
  puts "This should blow up..."
  puts e.message
end

If you want to use it in your Rails or Merb app you can do:

1
2
3
4
5
# Rails
require 'google_js_api' #or whatever you named the file when you dropped it in lib
 
# In your application layout or whatever
javascript_include_tag GoogleJsApi.include(:jquery, "1.4.0")
1
2
3
4
5
# Merb
require 'google_js_api' #or whatever you named the file when you dropped it in lib
 
#in your application layout or where ever
require_js GoogleJsApi.include(:swfobject)

Its simple, its lazy. I like it.

Post to Twitter Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon