Class: GithubHook

Inherits:
Object
  • Object
show all
Defined in:
lib/github_hook.rb

Constant Summary collapse

VERSION =
'0.6.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ GithubHook



9
10
11
12
13
14
15
# File 'lib/github_hook.rb', line 9

def initialize(json)
  payload = JSON.parse(json)
  @before, @after, @ref = payload["before"], payload["after"], payload["ref"]
  @repository = OpenStruct.new(payload["repository"])
  @owner = OpenStruct.new(payload["repository"]["owner"])
  @commits = create_commits(payload)
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



7
8
9
# File 'lib/github_hook.rb', line 7

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



7
8
9
# File 'lib/github_hook.rb', line 7

def before
  @before
end

#commitsObject (readonly)

Returns the value of attribute commits.



7
8
9
# File 'lib/github_hook.rb', line 7

def commits
  @commits
end

#ownerObject (readonly)

Returns the value of attribute owner.



7
8
9
# File 'lib/github_hook.rb', line 7

def owner
  @owner
end

#refObject (readonly)

Returns the value of attribute ref.



7
8
9
# File 'lib/github_hook.rb', line 7

def ref
  @ref
end

#repositoryObject (readonly)

Returns the value of attribute repository.



7
8
9
# File 'lib/github_hook.rb', line 7

def repository
  @repository
end

Instance Method Details

#create_commits(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/github_hook.rb', line 17

def create_commits(payload)
  payload['commits'].map do |hash|
    commit_ostruct = OpenStruct.new(hash)
    commit_ostruct.sha = hash["id"]
    commit_ostruct.author = OpenStruct.new(hash["author"])
    commit_ostruct
  end
rescue NoMethodError
  payload['commits'].map do |sha, hash|
    commit_ostruct = OpenStruct.new(hash)
    commit_ostruct.sha = sha
    commit_ostruct.author = OpenStruct.new(hash["author"])
    commit_ostruct
  end
end

#last_commitObject



33
34
35
# File 'lib/github_hook.rb', line 33

def last_commit
  @commits.sort_by { |commit| commit.timestamp }.last
end