Class: GithubHook

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

Constant Summary collapse

VERSION =
'0.6.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ GithubHook

Returns a new instance of GithubHook.



9
10
11
12
13
14
15
16
# 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"])
  @repository.private = parse_private_flag(@repository.private)
  @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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github_hook.rb', line 22

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



38
39
40
# File 'lib/github_hook.rb', line 38

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

#parse_private_flag(private_flag) ⇒ Object



18
19
20
# File 'lib/github_hook.rb', line 18

def parse_private_flag(private_flag)
  private_flag == "true" ? true : false
end