cdnjs-command 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,13 @@
1
+ v0.0.5 - Jul 24, 2011
2
+ ---------------------
3
+
4
+ Update base URL to the new `cloudflare.com` URL.
5
+
6
+ v0.0.4 - Jul 24, 2011
7
+ ---------------------
8
+
9
+ Calling `cdnjs` actually works now.
10
+
1
11
  v0.0.3 - Apr 16, 2011
2
12
  ---------------------
3
13
 
data/bin/cdnjs CHANGED
@@ -1,194 +1,60 @@
1
1
  #!/usr/bin/env ruby
2
- require 'open-uri'
3
- require 'fileutils'
4
- require 'json'
5
2
 
6
- CDNJS_VERSION = "0.0.3"
3
+ require File.expand_path('../../lib/cdnjs_command', __FILE__)
7
4
 
8
- module Params;
9
- def extract(what) i = index(what) and slice!(i, 2)[1] end;
10
- def first_is(what) shift if what.include?(self.first); end
11
- def self.[](*what) what.extend Params; end
12
- def ===(argv) argv.first_is(self); end
13
- end
5
+ Package = CdnjsCommand::Package
6
+ Fetcher = CdnjsCommand::Fetcher
7
+ Params = CdnjsCommand::Params
14
8
 
9
+ extend CdnjsCommand::Helpers
15
10
  ARGV.extend Params
16
11
 
17
12
  # ============================================================================
13
+ # Controller
18
14
 
19
- module Fetcher
20
- CACHE_PATH = "~/.cache/cdnjs"
21
-
22
- def self.fetch(url)
23
- return cache_for(url) if cached?(url)
24
-
25
- tip "Fetching #{url}..."
26
- output = open(url).read
27
-
28
- FileUtils.mkdir_p File.expand_path(CACHE_PATH)
29
- File.open(cache_path_for(url), 'w') { |f| f.write output }
30
- output
31
- rescue => e
32
- tip "Unable to fetch (#{e.class.name}: #{e.message})."
33
- exit
34
- end
35
-
36
- def self.purge
37
- FileUtils.rm_rf File.expand_path(CACHE_PATH)
38
- end
39
-
40
- def self.cached?(url)
41
- File.file? cache_path_for(url)
42
- end
43
-
44
- def self.cache_path_for(url)
45
- File.expand_path slug(url), CACHE_PATH
46
- end
47
-
48
- def self.cache_for(url)
49
- File.open(cache_path_for(url)) { |f| f.read }
50
- end
51
-
52
- def self.slug(url)
53
- url.gsub(/[\/:]/, '_')
54
- end
55
- end
15
+ case ARGV
16
+ when Params['-h', '--help']
17
+ show_help
56
18
 
57
- class Package
58
- INDEX_URL = 'http://cdnjs.com/packages.json'
59
-
60
- attr_reader :data, :name, :version, :description, :homepage,
61
- :maintainers, :repositories
62
-
63
- def initialize(hash)
64
- @data = hash
65
- @name = hash['name'].gsub(/\.js$/, '')
66
- @version = hash['version']
67
- @description = hash['description']
68
- @homepage = hash['homepage']
69
- @filename = hash['filename']
70
- @maintainers = hash['maintainers']
71
- @repositories = hash['repositories']
72
- end
19
+ when Params['-v', '--version']
20
+ puts "cdnjs-command #{CDNJS_VERSION}"
73
21
 
74
- def basename
75
- "#{name}-#{version}.js"
76
- end
22
+ when Params['list', 'l']
23
+ Package.all.each { |pkg|
24
+ puts "%-20s %s" % [ pkg.name + " (" + pkg.version + ")", ""]
25
+ }
77
26
 
78
- def url
79
- if @filename =~ /^http/
80
- @filename
81
- else
82
- "http://ajax.cdnjs.com/ajax/libs/#{data['name']}/#{version}/#{@filename}"
83
- end
84
- end
27
+ when Params['html', 'h']
28
+ name = ARGV.shift or invalid_usage("cdnjs html NAME")
29
+ pkg = get_package(name)
85
30
 
86
- # @return Array of {Package}s
87
- def self.all
88
- packages = JSON.parse(fetch(INDEX_URL))['packages']
89
- packages.delete(Hash.new)
90
- packages
91
- .map { |hash| Package.new(hash) }
92
- .sort_by { |pkg| pkg.name }
93
- end
31
+ puts "<script src='#{pkg.url}'></script>"
94
32
 
95
- def self.fetch(url)
96
- Fetcher.fetch(INDEX_URL).force_encoding('utf-8')
97
- end
33
+ when Params['url', 'u']
34
+ name = ARGV.shift or invalid_usage("cdnjs url NAME")
35
+ puts get_package(name).url
98
36
 
99
- def self.[](id)
100
- all.detect { |pkg| pkg.name == id } ||
101
- all.detect { |pkg| pkg.name[0...id.size] == id }
102
- end
103
- end
37
+ when Params['info', 'i']
38
+ name = ARGV.shift or invalid_usage("cdnjs info NAME")
39
+ pkg = get_package(name)
104
40
 
105
- # ============================================================================
106
- # Helpers
41
+ puts "%s (%s)" % [ pkg.name, pkg.version ]
42
+ puts ""
43
+ puts "%-10s %s" % [ "URL:", pkg.url ]
44
+ puts "%-10s %s" % [ "Homepage:", pkg.homepage ]
45
+ puts ""
46
+ puts pkg.description
47
+ puts ""
48
+ puts "<!-- Include this library in your project with: -->"
49
+ puts "<script src='#{pkg.url}'></script>"
107
50
 
108
- def tip(message)
109
- $stderr.write "#{message}\n"
110
- end
111
-
112
- def invalid_usage(usage="cdnjs --help")
113
- puts "Invalid usage."
114
- puts "Try: #{usage.gsub(/^cdnjs/, File.basename($0))}"
115
- exit
116
- end
117
-
118
- def get_package(id)
119
- Package[id] or begin
120
- tip "Package not found."
121
- tip "For a list of all packages, see: cdnjs list"
122
- exit
123
- end
124
- end
125
-
126
- def show_help
127
- tip "Usage:"
128
- tip " cdnjs NAME - Download specific package"
129
- tip ""
130
- tip "Other commands:"
131
- tip " cdnjs list - List available packages"
132
- tip " cdnjs info NAME - Show package info"
133
- tip " cdnjs html NAME - Show HTML snippet to include given package"
134
- tip " cdnjs url NAME - Show the URL for the given package"
135
- tip " cdnjs update - Updates the package cache"
136
- tip ""
137
- tip "You may also use the first letters of each command; e.g., `cdnjs i jquery`."
138
- tip "Here are some common examples:"
139
- tip ""
140
- tip " $ cdnjs jquery"
141
- tip " $ echo `cdnjs h underscore` >> index.html"
142
- tip ""
143
- end
144
-
145
- # ============================================================================
146
- # Le controller
147
-
148
- if __FILE__ == $0
149
- case ARGV
150
- when Params['-h', '--help']
151
- show_help
152
-
153
- when Params['-v', '--version']
154
- puts "cdnjs-command #{CDNJS_VERSION}"
155
-
156
- when Params['list', 'l']
157
- Package.all.each { |pkg|
158
- puts "%-20s %s" % [ pkg.name + " (" + pkg.version + ")", ""]
159
- }
160
-
161
- when Params['html', 'h']
162
- name = ARGV.shift or invalid_usage("cdnjs html NAME")
163
- pkg = get_package(name)
164
-
165
- puts "<script src='#{pkg.url}'></script>"
166
-
167
- when Params['url', 'u']
168
- name = ARGV.shift or invalid_usage("cdnjs url NAME")
169
- puts get_package(name).url
170
-
171
- when Params['info', 'i']
172
- name = ARGV.shift or invalid_usage("cdnjs info NAME")
173
- pkg = get_package(name)
174
-
175
- puts "%s (%s)" % [ pkg.name, pkg.version ]
51
+ if pkg.url =~ /^https/
176
52
  puts ""
177
- puts "%-10s %s" % [ "URL:", pkg.url ]
178
- puts "%-10s %s" % [ "Homepage:", pkg.homepage ]
179
- puts ""
180
- puts pkg.description
181
- puts ""
182
- puts "<!-- Include this library in your project with: -->"
183
- puts "<script src='#{pkg.url}'></script>"
184
-
185
- if pkg.url =~ /^https/
186
- puts ""
187
- puts "<!-- (You may need to replace 'https' with 'http'.) -->"
188
- end
53
+ puts "<!-- (You may need to replace 'https' with 'http'.) -->"
54
+ end
189
55
 
190
56
 
191
- p pkg if ARGV.delete('-v')
57
+ p pkg if ARGV.delete('-v')
192
58
 
193
59
  when Params['update']
194
60
  Fetcher.purge
@@ -198,19 +64,18 @@ if __FILE__ == $0
198
64
  else
199
65
  name = ARGV.shift or (show_help; exit)
200
66
  pkg = Package[name] or begin
201
- if ARGV.empty?
202
- puts "Invalid package name."
203
- else
204
- puts "Invalid usage."
205
- end
206
- puts "For a list of all packages: cdnjs list"
207
- puts "For more help: cdnjs --help"
208
- exit
67
+ if ARGV.empty?
68
+ puts "Invalid package name."
69
+ else
70
+ puts "Invalid usage."
209
71
  end
72
+ puts "For a list of all packages: cdnjs list"
73
+ puts "For more help: cdnjs --help"
74
+ exit
75
+ end
210
76
 
211
- data = Fetcher.fetch(pkg.url)
212
- File.open(pkg.basename, 'w') { |f| f.write data }
77
+ data = Fetcher.fetch(pkg.url)
78
+ File.open(pkg.basename, 'w') { |f| f.write data }
213
79
 
214
- tip "Created #{pkg.basename}."
215
- end
80
+ tip "Created #{pkg.basename}."
216
81
  end
@@ -0,0 +1,12 @@
1
+ require 'open-uri'
2
+ require 'fileutils'
3
+ require 'json'
4
+
5
+ CDNJS_VERSION = "0.0.5"
6
+
7
+ module CdnjsCommand
8
+ autoload :Params, File.expand_path('../cdnjs_command/params', __FILE__)
9
+ autoload :Fetcher, File.expand_path('../cdnjs_command/fetcher', __FILE__)
10
+ autoload :Package, File.expand_path('../cdnjs_command/package', __FILE__)
11
+ autoload :Helpers, File.expand_path('../cdnjs_command/helpers', __FILE__)
12
+ end
@@ -0,0 +1,39 @@
1
+ module CdnjsCommand::Fetcher
2
+ CACHE_PATH = "~/.cache/cdnjs"
3
+
4
+ extend CdnjsCommand::Helpers
5
+
6
+ def self.fetch(url)
7
+ return cache_for(url) if cached?(url)
8
+
9
+ tip "Fetching #{url}..."
10
+ output = open(url).read
11
+
12
+ FileUtils.mkdir_p File.expand_path(CACHE_PATH)
13
+ File.open(cache_path_for(url), 'w') { |f| f.write output }
14
+ output
15
+ rescue => e
16
+ tip "Unable to fetch (#{e.class.name}: #{e.message})."
17
+ exit
18
+ end
19
+
20
+ def self.purge
21
+ FileUtils.rm_rf File.expand_path(CACHE_PATH)
22
+ end
23
+
24
+ def self.cached?(url)
25
+ File.file? cache_path_for(url)
26
+ end
27
+
28
+ def self.cache_path_for(url)
29
+ File.expand_path slug(url), CACHE_PATH
30
+ end
31
+
32
+ def self.cache_for(url)
33
+ File.open(cache_path_for(url)) { |f| f.read }
34
+ end
35
+
36
+ def self.slug(url)
37
+ url.gsub(/[\/:]/, '_')
38
+ end
39
+ end
@@ -0,0 +1,38 @@
1
+ module CdnjsCommand::Helpers
2
+ def tip(message)
3
+ $stderr.write "#{message}\n"
4
+ end
5
+
6
+ def invalid_usage(usage="cdnjs --help")
7
+ puts "Invalid usage."
8
+ puts "Try: #{usage.gsub(/^cdnjs/, File.basename($0))}"
9
+ exit
10
+ end
11
+
12
+ def get_package(id)
13
+ Package[id] or begin
14
+ tip "Package not found."
15
+ tip "For a list of all packages, see: cdnjs list"
16
+ exit
17
+ end
18
+ end
19
+
20
+ def show_help
21
+ tip "Usage:"
22
+ tip " cdnjs NAME - Download specific package"
23
+ tip ""
24
+ tip "Other commands:"
25
+ tip " cdnjs list - List available packages"
26
+ tip " cdnjs info NAME - Show package info"
27
+ tip " cdnjs html NAME - Show HTML snippet to include given package"
28
+ tip " cdnjs url NAME - Show the URL for the given package"
29
+ tip " cdnjs update - Updates the package cache"
30
+ tip ""
31
+ tip "You may also use the first letters of each command; e.g., `cdnjs i jquery`."
32
+ tip "Here are some common examples:"
33
+ tip ""
34
+ tip " $ cdnjs jquery"
35
+ tip " $ echo `cdnjs h underscore` >> index.html"
36
+ tip ""
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ class CdnjsCommand::Package
2
+ INDEX_URL = 'http://cdnjs.com/packages.json'
3
+
4
+ attr_reader :data, :name, :version, :description, :homepage,
5
+ :maintainers, :repositories
6
+
7
+ def initialize(hash)
8
+ @data = hash
9
+ @name = hash['name'].gsub(/\.js$/, '')
10
+ @version = hash['version']
11
+ @description = hash['description']
12
+ @homepage = hash['homepage']
13
+ @filename = hash['filename']
14
+ @maintainers = hash['maintainers']
15
+ @repositories = hash['repositories']
16
+ end
17
+
18
+ def basename
19
+ "#{name}-#{version}.js"
20
+ end
21
+
22
+ def url
23
+ if @filename =~ /^http/
24
+ @filename
25
+ else
26
+ "http://cdnjs.cloudflare.com/ajax/libs/#{data['name']}/#{version}/#{@filename}"
27
+ end
28
+ end
29
+
30
+ # @return Array of {Package}s
31
+ def self.all
32
+ packages = JSON.parse(fetch(INDEX_URL))['packages']
33
+ packages.delete(Hash.new)
34
+ packages
35
+ .map { |hash| Package.new(hash) }
36
+ .sort_by { |pkg| pkg.name }
37
+ end
38
+
39
+ def self.fetch(url)
40
+ Fetcher.fetch(INDEX_URL).force_encoding('utf-8')
41
+ end
42
+
43
+ def self.[](id)
44
+ all.detect { |pkg| pkg.name == id } ||
45
+ all.detect { |pkg| pkg.name[0...id.size] == id }
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module CdnjsCommand::Params
2
+ def extract(what) i = index(what) and slice!(i, 2)[1] end;
3
+ def first_is(what) shift if what.include?(self.first); end
4
+ def self.[](*what) what.extend Params; end
5
+ def ===(argv) argv.first_is(self); end
6
+ end
7
+
metadata CHANGED
@@ -1,79 +1,68 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cdnjs-command
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Rico Sta. Cruz
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-04-16 00:00:00 +08:00
12
+ date: 2011-07-24 00:00:00.000000000 +08:00
18
13
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
21
16
  name: json
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2165491960 !ruby/object:Gem::Requirement
24
18
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
31
23
  type: :runtime
32
- version_requirements: *id001
33
- description: This package lets you download the popular JavaScript packages with one command, thanks to www.cdnjs.com.
34
- email:
24
+ prerelease: false
25
+ version_requirements: *2165491960
26
+ description: This package lets you download the popular JavaScript packages with one
27
+ command, thanks to www.cdnjs.com.
28
+ email:
35
29
  - rico@sinefunc.com
36
- executables:
30
+ executables:
37
31
  - cdnjs
38
32
  extensions: []
39
-
40
33
  extra_rdoc_files: []
41
-
42
- files:
34
+ files:
43
35
  - bin/cdnjs
36
+ - lib/cdnjs_command/fetcher.rb
37
+ - lib/cdnjs_command/helpers.rb
38
+ - lib/cdnjs_command/package.rb
39
+ - lib/cdnjs_command/params.rb
40
+ - lib/cdnjs_command.rb
44
41
  - HISTORY.md
45
42
  - README.md
46
43
  has_rdoc: true
47
44
  homepage: http://github.com/rstacruz/cdnjs-command
48
45
  licenses: []
49
-
50
46
  post_install_message:
51
47
  rdoc_options: []
52
-
53
- require_paths:
48
+ require_paths:
54
49
  - lib
55
- required_ruby_version: !ruby/object:Gem::Requirement
50
+ required_ruby_version: !ruby/object:Gem::Requirement
56
51
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- segments:
61
- - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
57
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- segments:
69
- - 0
70
- version: "0"
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
71
62
  requirements: []
72
-
73
63
  rubyforge_project:
74
- rubygems_version: 1.3.7
64
+ rubygems_version: 1.6.2
75
65
  signing_key:
76
66
  specification_version: 3
77
67
  summary: Command line helper for cdnjs.com.
78
68
  test_files: []
79
-