Class: Localhook::WebhookForwarder

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

Overview

Forward webhook to destination. Note: this should be used inside eventmachine

Constant Summary collapse

VALID_URL =
%r{^(https?://[^/]+)/?$}
DEFAULT_OPTIONS =
{:connect_timeout => 5}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, http_options = {}) ⇒ WebhookForwarder

Create a WebhookForwarder url - the base url of the destination webhook http_options - the http options passed to em



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

def initialize(url, http_options={})
  unless url =~ VALID_URL
    raise ArgumentError, "Invalid url \"#{url}\", it should be in format: (https|http)://<host>(:<port>)?/?"
  end

  @url = url.match(VALID_URL)[1]
  @http_options = DEFAULT_OPTIONS.merge(http_options)
end

Instance Attribute Details

#http_optionsObject (readonly)

Returns the value of attribute http_options.



8
9
10
# File 'lib/localhook/webhook_forwarder.rb', line 8

def http_options
  @http_options
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#post(path, query, headers, body) ⇒ Object

path - string, the path of the request query - string, the query string headers - Hash, the header of the request body - String, the body of the request



29
30
31
32
33
# File 'lib/localhook/webhook_forwarder.rb', line 29

def post(path, query, headers, body)
  http = EventMachine::HttpRequest.new(url, http_options).post :head => headers, :path => path, :query => query, :body => body
  http.callback { puts "forward url -> #{url}" }
  http.errback { puts "ERROR: failed forward to url #{url}" }
end