Class: Issue::Webhook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Webhook

Initialize the Issue::Webhook object This method should receive a Hash with the following settings:

secret_token: the GitHub secret token needed to verify the request signature.
accept_events: an Array of valid values for the HTTP_X_GITHUB_EVENT header. If empty any event will be processed.
origin: the respository where the webhook should be sent to be accepted. If empty any request will be processed.
discard_sender: an optional GitHub user handle to discard all events triggered by it.


22
23
24
25
26
27
# File 'lib/issue/webhook.rb', line 22

def initialize(settings={})
  @secret_token = settings[:secret_token]
  @accept_origin = settings[:origin]
  @accept_events = [settings[:accept_events]].flatten.compact.uniq.map(&:to_s)
  @discard_sender = parse_discard_senders(settings[:discard_sender])
end

Instance Attribute Details

#accept_eventsObject

Returns the value of attribute accept_events.



12
13
14
# File 'lib/issue/webhook.rb', line 12

def accept_events
  @accept_events
end

#accept_originObject

Returns the value of attribute accept_origin.



10
11
12
# File 'lib/issue/webhook.rb', line 10

def accept_origin
  @accept_origin
end

#discard_senderObject

Returns the value of attribute discard_sender.



11
12
13
# File 'lib/issue/webhook.rb', line 11

def discard_sender
  @discard_sender
end

#errorObject

Returns the value of attribute error.



13
14
15
# File 'lib/issue/webhook.rb', line 13

def error
  @error
end

#payloadObject

Returns the value of attribute payload.



14
15
16
# File 'lib/issue/webhook.rb', line 14

def payload
  @payload
end

#requestObject

Returns the value of attribute request.



9
10
11
# File 'lib/issue/webhook.rb', line 9

def request
  @request
end

#secret_tokenObject

Returns the value of attribute secret_token.



8
9
10
# File 'lib/issue/webhook.rb', line 8

def secret_token
  @secret_token
end

Instance Method Details

#errored?Boolean

This method returns True if parsing a request has generated an Issue::Error object That object will be available at the #error accessor method.

Returns:

  • (Boolean)


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

def errored?
  !error.nil?
end

#parse_request(request) ⇒ Object

This method will parse the passed request. If the request signature is incorrect or any of the conditions set via the initialization settings are not met an error will be created with the appropiate html status and message. Otherwise a Issue::Payload object will be created with the information contained in the request payload.

This method returns a pair [payload, error] where only one of them will be nil



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/issue/webhook.rb', line 36

def parse_request(request)
  @payload = nil
  @error = nil
  @request = request

  if verify_signature
    parse_payload
  end

  return [payload, error]
end