Sha256: 361528aef84c32b4145256029e52d9401c0c901dfb6022ae43920459d01fc991

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

# -*- encoding : utf-8 -*-
require 'rest_client'
require 'pp'
require 'json'

module Lolcommits
  class Lolsrv < Plugin
    def initialize(runner)
      super
      options << 'server'
    end

    def run_postcapture
      return unless valid_configuration?
      fork { sync }
    end

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

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

    def existing_lols
      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

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

    def self.name
      'lolsrv'
    end

    def self.runner_order
      :postcapture
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
snapgit-0.6.5 lib/lolcommits/plugins/lolsrv.rb
snapgit-0.6.4 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.4 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.3 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.3.pre1 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.2 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.2.pre1 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.1 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.6.0 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.5.9 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.5.9.pre1 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.5.8 lib/lolcommits/plugins/lolsrv.rb
lolcommits-0.5.8.pre1 lib/lolcommits/plugins/lolsrv.rb