Class: CdnjsCommand::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/cdnjs_command/package.rb

Constant Summary collapse

INDEX_URL =
'http://cdnjs.com/packages.json'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Package

Returns a new instance of Package.



7
8
9
10
11
12
13
14
15
16
# File 'lib/cdnjs_command/package.rb', line 7

def initialize(hash)
  @data = hash
  @name         = hash['name'].gsub(/\.js$/, '')
  @version      = hash['version']
  @description  = hash['description']
  @homepage     = hash['homepage']
  @filename     = hash['filename']
  @maintainers  = hash['maintainers']
  @repositories = hash['repositories']
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def data
  @data
end

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def description
  @description
end

#homepageObject (readonly)

Returns the value of attribute homepage.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def homepage
  @homepage
end

#maintainersObject (readonly)

Returns the value of attribute maintainers.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def maintainers
  @maintainers
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def name
  @name
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def repositories
  @repositories
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/cdnjs_command/package.rb', line 4

def version
  @version
end

Class Method Details

.[](id) ⇒ Object



43
44
45
46
# File 'lib/cdnjs_command/package.rb', line 43

def self.[](id)
  all.detect { |pkg| pkg.name == id } ||
  all.detect { |pkg| pkg.name[0...id.size] == id }
end

.allObject

Returns Array of CdnjsCommand::Packages.

Returns:



31
32
33
34
35
36
37
# File 'lib/cdnjs_command/package.rb', line 31

def self.all
  packages = JSON.parse(fetch(INDEX_URL))['packages']
  packages.delete(Hash.new)
  packages.
    map { |hash| Package.new(hash) }.
    sort_by { |pkg| pkg.name }
end

.fetch(url) ⇒ Object



39
40
41
# File 'lib/cdnjs_command/package.rb', line 39

def self.fetch(url)
  Fetcher.fetch(INDEX_URL).force_encoding('utf-8')
end

Instance Method Details

#basenameObject



18
19
20
# File 'lib/cdnjs_command/package.rb', line 18

def basename
  "#{name}-#{version}.js"
end

#urlObject



22
23
24
25
26
27
28
# File 'lib/cdnjs_command/package.rb', line 22

def url
  if @filename =~ /^http/
    @filename
  else
    "http://cdnjs.cloudflare.com/ajax/libs/#{data['name']}/#{version}/#{@filename}"
  end
end