Class: Flowdock::Git::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/flowdock/git/builder.rb

Overview

Class used to build Git payload

Instance Method Summary collapse

Constructor Details

#initialize(repo, ref, before, after) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
11
12
# File 'lib/flowdock/git/builder.rb', line 7

def initialize(repo, ref, before, after)
  @repo = repo
  @ref = ref
  @before = before
  @after = after
end

Instance Method Details

#commitsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flowdock/git/builder.rb', line 14

def commits
  @repo.commits_between(@before, @after).map do |commit|
    {
      :id => commit.sha,
      :message => commit.message,
      :timestamp => commit.authored_date.iso8601,
      :author => {
        :name => commit.author.name,
        :email => commit.author.email
      },
      :removed => filter(commit.diffs) { |d| d.deleted_file },
      :added => filter(commit.diffs) { |d| d.new_file },
      :modified => filter(commit.diffs) { |d| !d.deleted_file && !d.new_file }
    }
  end
end

#ref_nameObject



31
32
33
# File 'lib/flowdock/git/builder.rb', line 31

def ref_name
  @ref.to_s.sub(/\Arefs\/(heads|tags)\//, '')
end

#to_hashObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flowdock/git/builder.rb', line 35

def to_hash
  encode({
    :before   => @before,
    :after    => @after,
    :ref      => @ref,
    :commits  => commits,
    :ref_name => @ref.to_s.sub(/\Arefs\/(heads|tags)\//, ''),
    :repository => {
      :name => File.basename(Dir.pwd).sub(/\.git$/,'')
    }
  }).merge(if @before == "0000000000000000000000000000000000000000"
    {:created => true}
  elsif @after == "0000000000000000000000000000000000000000"
    {:deleted => true}
  else
    {}
  end)
end