Class: Jail::Github

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

Overview

This is a wrapper around ::Github

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, repo) ⇒ Github

Returns a new instance of Github.



28
29
30
31
32
33
34
35
# File 'app/models/jail/github.rb', line 28

def initialize(name, repo)
  @name= name
  @repo= repo
  @spec= self.class.githublist["#{@name}/#{@repo}"]
  @github = defined?(Jail::LOGIN) ?
              ::Github.new({ :basic_auth => Jail::LOGIN }) :
              ::Github.new
end

Instance Attribute Details

#githubObject (readonly)

Returns the value of attribute github.



4
5
6
# File 'app/models/jail/github.rb', line 4

def github
  @github
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'app/models/jail/github.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'app/models/jail/github.rb', line 5

def path
  @path
end

#repoObject (readonly)

Returns the value of attribute repo.



4
5
6
# File 'app/models/jail/github.rb', line 4

def repo
  @repo
end

#specObject (readonly)

Returns the value of attribute spec.



4
5
6
# File 'app/models/jail/github.rb', line 4

def spec
  @spec
end

Class Method Details

.add_githublist(yaml_path) ⇒ Object



8
9
10
11
# File 'app/models/jail/github.rb', line 8

def self.add_githublist(yaml_path)
  hash = YAML.load_file(yaml_path).to_hash
  self.githublist= self.githublist.merge(hash)
end

.allObject

TODO : raise if missing descr:



17
18
19
# File 'app/models/jail/github.rb', line 17

def self.all
  githublist.group_by {|k, v| v[:descr].match(/\w+:/).to_s if v[:descr]}
end

.find(name, repo, path = nil) ⇒ Object

Uhh useless ? Or AR Sexy ?



22
23
24
25
26
# File 'app/models/jail/github.rb', line 22

def self.find(name, repo, path = nil)
  new(name, repo).tap do |yo|
    yo.path= path
  end
end

Instance Method Details

#contentsObject



48
49
50
51
# File 'app/models/jail/github.rb', line 48

def contents
  self.path ||= "/"
  github.repos.contents.get(name, repo, path)
end

#delete(type = nil) ⇒ Object



69
70
71
# File 'app/models/jail/github.rb', line 69

def delete(type = nil)
  t = target(type) and t.exist? and t.delete
end

#delete_allObject Also known as: remove



102
103
104
105
106
# File 'app/models/jail/github.rb', line 102

def delete_all
  [:js, :css, :img].each do |type|
    where(spec[type]).delete(type) if spec[type]
  end
end

#download(type = nil) ⇒ Object



64
65
66
67
# File 'app/models/jail/github.rb', line 64

def download(type = nil)
  t = target(type) and t.parent.mkpath
  t.open('w') {|f| f.write( read ) }
end

#extract_type(filename) ⇒ Object



87
88
89
90
# File 'app/models/jail/github.rb', line 87

def extract_type(filename)
  type = Pathname(filename).extname
  type = type.gsub(/\./, "").to_sym
end

#readObject



59
60
61
62
# File 'app/models/jail/github.rb', line 59

def read
  #raise "blah" unless contents.type == "file"
  text = Base64.decode64 contents.content
end

#readmeObject

TODO : split in 2 methods for consistency



54
55
56
57
# File 'app/models/jail/github.rb', line 54

def readme
  text = Base64.decode64 github.repos.contents.readme(name, repo).content
  github.markdown.render :text => text
end

#reposObject

do I need this method ?



44
45
46
# File 'app/models/jail/github.rb', line 44

def repos
  @repos ||= github.repos.get(name, repo)
end

#target(type = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/jail/github.rb', line 73

def target(type=nil)
  filename= Pathname(path).basename
  type = type || extract_type(filename)
  
  case type
  when :js, :coffee
    Rails.root.join("vendor/assets/javascripts/#{filename}")
  when :css, :scss, :less
    Rails.root.join("vendor/assets/stylesheets/#{filename}")
  when :img, :png, :gif, :jpeg
    Rails.root.join("vendor/assets/images/#{filename}")
  end
end

#where(path) ⇒ Object

another AR alike for chaining



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

def where(path)
  self.path= path
  self
end

#write_allObject Also known as: install

Depends on yaml files: dont use elsewhere



95
96
97
98
99
# File 'app/models/jail/github.rb', line 95

def write_all
  [:js, :css, :img].each do |type|
    where(spec[type]).download(type) if spec[type]
  end
end