Class: Lolcommits::Lolsrv

Inherits:
Plugin
  • Object
show all
Defined in:
lib/lolcommits/plugins/lolsrv.rb

Instance Attribute Summary

Attributes inherited from Plugin

#options, #runner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#configuration, #configure_options!, #debug, #enabled?, #execute, #puts, #valid_configuration?

Constructor Details

#initialize(runner) ⇒ Lolsrv

Returns a new instance of Lolsrv.



8
9
10
11
# File 'lib/lolcommits/plugins/lolsrv.rb', line 8

def initialize(runner)
  super
  self.options << 'server'
end

Class Method Details

.nameObject



65
66
67
# File 'lib/lolcommits/plugins/lolsrv.rb', line 65

def self.name
  'lolsrv'
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/lolcommits/plugins/lolsrv.rb', line 18

def configured?
  !configuration['enabled'].nil? && configuration['server']
end

#existing_lolsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/lolcommits/plugins/lolsrv.rb', line 34

def existing_lols
  begin
    lols = JSON.parse(
    RestClient.get(configuration['server'] + '/lols'))
    lols.map { |lol| lol['sha'] }
  rescue => e
    log_error(e, "ERROR: existing lols could not be retrieved #{e.class} - #{e.message}")
    return nil
  end
end

#log_error(e, message) ⇒ Object



60
61
62
63
# File 'lib/lolcommits/plugins/lolsrv.rb', line 60

def log_error(e, message)
  debug message
  debug e.backtrace
end

#runObject



13
14
15
16
# File 'lib/lolcommits/plugins/lolsrv.rb', line 13

def run
  return unless valid_configuration?
  fork { sync }
end

#syncObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lolcommits/plugins/lolsrv.rb', line 22

def sync
  existing = existing_lols
  unless existing.nil?
    Dir[self.runner.config.loldir + '/*.{jpg,gif}'].each do |item|
      sha = File.basename(item, '.*')
      unless existing.include?(sha) || sha == 'tmp_snapshot'
        upload(item, sha)
      end
    end
  end
end

#upload(file, sha) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lolcommits/plugins/lolsrv.rb', line 45

def upload(file, sha)
  begin
    RestClient.post(
      configuration['server'] + '/uplol',
      :lol => File.new(file),
      :url => self.runner.url + sha,
      :repo => self.runner.repo,
      :date => File.ctime(file),
      :sha => sha)
  rescue => e
    log_error(e, "ERROR: Upload of lol #{sha} FAILED #{e.class} - #{e.message}")
    return
  end
end