Module: Util

Included in:
CommandRouter, Create, Init, List, Refresh, Remove, Rename, Start, Sync
Defined in:
lib/util.rb

Defined Under Namespace

Classes: Config, Token

Instance Method Summary collapse

Instance Method Details

#add_to_gitObject



81
82
83
84
85
86
87
88
89
# File 'lib/util.rb', line 81

def add_to_git
  g = Git.open content_root
  g.add(:all=>true)
  g.commit('autocommit')
  g.push(g.remote('origin'),opts = {force: true}) if has_remote?(g)

rescue Git::GitExecuteError => error
  puts error.message.split(":")[1]
end

#alter_index_htmlObject



53
54
55
56
57
58
59
60
61
# File 'lib/util.rb', line 53

def alter_index_html
  index = "#{content_root}/client/index.html"
  doc = nil
  File.open index,"r+" do |file|
    doc = Nokogiri::HTML(file)
    yield doc
  end
  File.open(index,'w+') { |file| file.write(doc.to_html)} if doc
end


63
64
65
66
67
68
# File 'lib/util.rb', line 63

def alter_link(name)
  alter_index_html do | doc |
    link   = doc.search(%Q(a[href="slides/#{name}/"])).first
    yield link
  end
end

#content_rootObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/util.rb', line 10

def content_root
  return @root_dir if @root_dir
  search_dir = Dir.pwd
  while search_dir && !File.exist?("#{search_dir}/config.yml")
    parent = File.dirname(search_dir)
    # project_root wird entweder der Root-pfad oder false. Wenn es false
    # wird, bricht die Schleife ab. Vgl. Rails
    search_dir = (parent != search_dir) && parent
  end
  project_root = search_dir if File.exist? "#{search_dir}/config.yml"
  raise 'you are not within a presentation-project.' unless project_root
  @root_dir = Pathname.new(File.realpath project_root)
end

#each_presentation(&block) ⇒ Object



38
39
40
41
42
43
# File 'lib/util.rb', line 38

def each_presentation(&block)
  path = "#{content_root}/master/slides"
  Dir.entries(path)
      .select { |entry| !entry.start_with? '.'}
      .each{ | entry | presentation(entry,&block) }
end

#git_repository?Boolean

git stuff

Returns:

  • (Boolean)


72
73
74
# File 'lib/util.rb', line 72

def git_repository?
  File.exist? "#{content_root}/.git"
end

#has_remote?(git) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/util.rb', line 91

def has_remote?(git)
  git.remotes.count > 0
end

#init_git(project_path, remote = nil) ⇒ Object



76
77
78
79
# File 'lib/util.rb', line 76

def init_git(project_path,remote=nil)
  g = Git.init project_path
  g.add_remote 'origin',remote if remote
end

#parse_configObject



25
26
27
28
# File 'lib/util.rb', line 25

def parse_config
  config = YAML.load_file("#{content_root}/config.yml")
  Config.new(config['host'],config['port'],config['project_name'])
end

#presentation(name, &block) ⇒ Object



46
47
48
49
50
# File 'lib/util.rb', line 46

def presentation(name,&block)
  master_path = "#{content_root}/master/slides/#{name}"
  client_path = "#{content_root}/client/slides/#{name}"
  block.call master_path,client_path
end

#pull_from_git(path) ⇒ Object



95
96
97
98
# File 'lib/util.rb', line 95

def pull_from_git(path)
  g = Git.open(path)
  g.pull('origin','master')
end

#request_tokenObject



30
31
32
33
34
35
# File 'lib/util.rb', line 30

def request_token
  host = parse_config.host
  puts "request token from: http://#{host}"
  hash = JSON.parse(Net::HTTP.get(host, '/token'))
  Token.new(hash['socketId'],hash['secret'])
end