Class: Localhook::EventSource
- Inherits:
-
EventMachine::EventSource
- Object
- EventMachine::EventSource
- Localhook::EventSource
- Defined in:
- lib/localhook/event_source.rb
Constant Summary collapse
- VALID_URL =
%r{^(https?://[^/]+)/?$}
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#forwarder ⇒ Object
readonly
Returns the value of attribute forwarder.
Instance Method Summary collapse
- #data_parsed(data) ⇒ Object
-
#initialize(base_url, forwarder, token) ⇒ EventSource
constructor
A new instance of EventSource.
Constructor Details
#initialize(base_url, forwarder, token) ⇒ EventSource
Returns a new instance of EventSource.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/localhook/event_source.rb', line 11 def initialize(base_url, forwarder, token) unless base_url =~ VALID_URL raise ArgumentError, "Invalid base_url \"#{base_url}\", it should be in format: (https|http)://<host>(:<port>)?/?" end base_url = base_url.match(VALID_URL)[1] super("#{base_url}/_localhook?token=#{token}") @forwarder = forwarder @parser = Yajl::Parser.new(:symbolize_keys => true) @parser.on_parse_complete = method(:data_parsed) self.on "webhook" do || @parser.parse() end self.on "endpoint" do || @endpoint = puts "Connected to endpoint: #{message}" end self.on "error" do |error| puts "Error: #{error}" self.close raise "Server response with an error: #{error}" end # never timeout self.inactivity_timeout = 0 end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/localhook/event_source.rb', line 7 def endpoint @endpoint end |
#forwarder ⇒ Object (readonly)
Returns the value of attribute forwarder.
6 7 8 |
# File 'lib/localhook/event_source.rb', line 6 def forwarder @forwarder end |
Instance Method Details
#data_parsed(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/localhook/event_source.rb', line 40 def data_parsed(data) # binding.pry case data[:action] when "post" # convert headers array to Hash, if needed headers = data[:headers] headers = headers.inject({}){|map, v| map[v[0]] = v[1]; map } if headers.is_a?(Array) path = data[:path] query = data[:query_string] body = data[:body] forwarder.post(path, query, headers, body) else raise "unknown action '#{data[:action]}' (#{data}" end end |