Class: Hercules::RequestHandler

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/request_handler.rb

Overview

Class that knows how to handle deploy requests. This implementation will just parse a JSON as defined by github http hooks. In order to use other hook formats this class should be reimplemented.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RequestHandler

We must pass the request data (method, path, query and body).

  • options is a hash containing all the request data described above plus the logger and the config hash.



16
17
18
19
20
21
22
# File 'lib/request_handler.rb', line 16

def initialize(options)
  @method = options[:method]
  @body = options[:body]
  @log = options[:log]
  @path = options[:path]
  @config = options[:config]
end

Instance Method Details

#branchObject

Returns the branch of the repository that fired the request.



74
75
76
# File 'lib/request_handler.rb', line 74

def branch
  payload['ref'].split('/').pop
end

#hdiObject

Returns the assembled HDI



60
61
62
63
64
65
66
# File 'lib/request_handler.rb', line 60

def hdi
  response = ""
  css = ""
  File.open(File.dirname(__FILE__) + "/../hdi/site/index.html", 'r'){|f| response = f.read }
  File.open(File.dirname(__FILE__) + "/../hdi/site/stylesheets/style.css", 'r'){|f| css = f.read }
  response.gsub(/<link href="stylesheets\/style.css" media="all" rel="stylesheet" type="text\/css">/, "<style>#{css}</style>").gsub(/##REQUEST_ADDRESS##/, @path.gsub(/\/hdi/, ""))
end

#messageObject

Returns the message generated as response for the request passed in the initializer. We also store the message for further queries.



31
32
33
34
# File 'lib/request_handler.rb', line 31

def message
  @result ||= process_request
  @result[:message]
end

#repository_nameObject

Returns the repository name that fired the request.



44
45
46
47
# File 'lib/request_handler.rb', line 44

def repository_name
  return payload['repository']['name'] if @method == "POST"
  return @path.split('/')[1] if @method == "GET"
end

#repository_urlObject

Returns the url of the repository that fired the request.



69
70
71
# File 'lib/request_handler.rb', line 69

def repository_url
  payload['repository']['url']
end

#request_hdi?Boolean

Returns true whenever the request made was a HDI request

Returns:

  • (Boolean)


55
56
57
# File 'lib/request_handler.rb', line 55

def request_hdi?
  @path.split('/')[3] == 'hdi' and @method == "GET"
end

#request_tokenObject

Returns the security token that fired the request.



50
51
52
# File 'lib/request_handler.rb', line 50

def request_token 
  @path.split('/')[2]
end

#runObject



24
25
26
27
28
# File 'lib/request_handler.rb', line 24

def run
  status
  message
  set_deferred_status :succeeded
end

#statusObject

Returns the status generated as response for the request passed in the initializer. We also store the status for further queries.



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

def status
  @result ||= process_request
  @result[:status]
end