Class: Localhook::WebhookForwarder
- Inherits:
-
Object
- Object
- Localhook::WebhookForwarder
- 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
-
#http_options ⇒ Object
readonly
Returns the value of attribute http_options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, http_options = {}) ⇒ WebhookForwarder
constructor
Create a WebhookForwarder url - the base url of the destination webhook http_options - the http options passed to em.
-
#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.
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, ={}) 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] = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#http_options ⇒ Object (readonly)
Returns the value of attribute http_options.
8 9 10 |
# File 'lib/localhook/webhook_forwarder.rb', line 8 def end |
#url ⇒ Object (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, ).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 |