Class: TxghServer::Webhooks::Github::PushAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/txgh-server/webhooks/github/push_attributes.rb

Constant Summary collapse

ATTRIBUTES =
[
  :event, :repo_name, :ref, :before, :after,
  :added_files, :modified_files, :author
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PushAttributes

Returns a new instance of PushAttributes.



67
68
69
70
71
72
73
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 67

def initialize(options = {})
  ATTRIBUTES.each do |attr|
    instance_variable_set(
      "@#{attr}", options.fetch(attr) { options.fetch(attr.to_s) }
    )
  end
end

Class Method Details

.added_files(payload) ⇒ Object



39
40
41
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 39

def added_files(payload)
  extract_files(payload, 'added')
end

.after(payload) ⇒ Object



35
36
37
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 35

def after(payload)
  payload.fetch('after')
end

.author(payload) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 47

def author(payload)
  if head_commit = payload.fetch('head_commit')
    head_commit.fetch('committer').fetch('name')
  else
    # fall back to pusher if no head commit
    payload.fetch('pusher').fetch('name')
  end
end

.before(payload) ⇒ Object



31
32
33
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 31

def before(payload)
  payload.fetch('before')
end

.event(payload) ⇒ Object



19
20
21
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 19

def event(payload)
  'push'
end

.extract_files(payload, state) ⇒ Object



56
57
58
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 56

def extract_files(payload, state)
  payload.fetch('commits').flat_map { |c| c[state] }.uniq
end

.from_webhook_payload(payload) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 11

def from_webhook_payload(payload)
  new(
    ATTRIBUTES.each_with_object({}) do |attr, ret|
      ret[attr] = public_send(attr, payload)
    end
  )
end

.modified_files(payload) ⇒ Object



43
44
45
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 43

def modified_files(payload)
  extract_files(payload, 'modified')
end

.ref(payload) ⇒ Object



27
28
29
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 27

def ref(payload)
  payload.fetch('ref')
end

.repo_name(payload) ⇒ Object



23
24
25
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 23

def repo_name(payload)
  payload.fetch('repository').fetch('full_name')
end

Instance Method Details

#filesObject



61
62
63
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 61

def files
  @files ||= added_files + modified_files
end

#to_hObject



75
76
77
78
79
# File 'lib/txgh-server/webhooks/github/push_attributes.rb', line 75

def to_h
  ATTRIBUTES.each_with_object({}) do |attr, ret|
    ret[attr] = public_send(attr)
  end
end