Class: Jail::Cdnjs

Inherits:
Object
  • Object
show all
Defined in:
app/models/jail/cdnjs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, lib) ⇒ Cdnjs

Returns a new instance of Cdnjs.



32
33
34
35
36
# File 'app/models/jail/cdnjs.rb', line 32

def initialize(path, lib)
  @github       = self.class.github
  @root_path    = path
  @package_path = lib.find {|h| h.type == "file"}.path
end

Instance Attribute Details

#githubObject (readonly)

Returns the value of attribute github.



3
4
5
# File 'app/models/jail/cdnjs.rb', line 3

def github
  @github
end

#package_pathObject (readonly)

Returns the value of attribute package_path.



3
4
5
# File 'app/models/jail/cdnjs.rb', line 3

def package_path
  @package_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



3
4
5
# File 'app/models/jail/cdnjs.rb', line 3

def root_path
  @root_path
end

Class Method Details

.find(name) ⇒ Object



25
26
27
28
29
30
# File 'app/models/jail/cdnjs.rb', line 25

def self.find(name)
  path = find_path(name)
  lib = lib(path)

  self.new(path, lib)
end

.find_path(name) ⇒ Object



20
21
22
23
# File 'app/models/jail/cdnjs.rb', line 20

def self.find_path(name)
  hash = libs.find { |hashie| hashie.path.split("/").last == name }
  hash and hash.path # raise if not found ?
end

.githubObject



7
8
9
# File 'app/models/jail/cdnjs.rb', line 7

def self.github
  @github ||= Jail::Github.find("cdnjs", "cdnjs", "ajax/libs")
end

.lib(path) ⇒ Object



16
17
18
# File 'app/models/jail/cdnjs.rb', line 16

def self.lib(path)
  github.where(path).contents
end

.libsObject



11
12
13
# File 'app/models/jail/cdnjs.rb', line 11

def self.libs
  @libs ||= github.contents
end

Instance Method Details

#file(path) ⇒ Object



68
69
70
# File 'app/models/jail/cdnjs.rb', line 68

def file(path)
  github.where(path)
end

#install(params) ⇒ Object



76
77
78
79
80
81
82
# File 'app/models/jail/cdnjs.rb', line 76

def install(params)
  files_to_write = params.map {|file, checkbox_val| file if checkbox_val == "1"}.compact

  files_to_write.each do |name|
    file(name).download()
  end
end

#mapped_filesObject



72
73
74
# File 'app/models/jail/cdnjs.rb', line 72

def mapped_files
  files.map(&:name).group_by {|name| Pathname(name).extname }
end

#packageObject



38
39
40
# File 'app/models/jail/cdnjs.rb', line 38

def package
  @package ||= Hashie::Mash.new ActiveSupport::JSON.decode( github.where(package_path).read )
end

#treeObject

build a depth first tree



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/jail/cdnjs.rb', line 52

def tree
  @tree and return @tree 
  @tree = []
  file_set = version_files

  while child = file_set.shift
    tree << child #if child.dir?

    if child.type == "dir"
      file_set.unshift( github.where(child.path).contents ).flatten!
    end
  end

  @tree
end

#version_filesObject



47
48
49
# File 'app/models/jail/cdnjs.rb', line 47

def version_files
  @version_files ||= github.where(version_path).contents
end

#version_pathObject

gets the version from the package.json & returns a path



43
44
45
# File 'app/models/jail/cdnjs.rb', line 43

def version_path
  Pathname(root_path).join(version)
end