waithook 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b32b080839c1c70aad64787e2c4ac0f34fe1dbda59282224a033017ae653621d
4
- data.tar.gz: 2adcded9aec179705c2e623aae4a78751ca3c3c057b083744382f7e45934c3fc
3
+ metadata.gz: 587f43be470d0045276f76af9a9518cde2c4c0fda3a48894a09bc8a4c053b78d
4
+ data.tar.gz: bb935b3ba35498e9250f4df4f7f750939defd5c08eb6bbf6501bbfdec97dca74
5
5
  SHA512:
6
- metadata.gz: d9ed5c757660fcaef33a48243d7695d7b7a2af1c007043c38898cdba459e49aa1ecade64071a5faf15217148269a6e40ab9aea1bd2b8fe4e583bb5270b64fe2d
7
- data.tar.gz: 5dee2730756b927ba2ac01757de9add47a28fd8c53c4f1f7c60fa53f6453445dd33cb4f6e2cc8cacf7a2af9572b5b2d57fb059b561ba63d1a1599fedd3aeee24
6
+ metadata.gz: a8e43d129c6ba5043ce3b5aa1f279f3c16e104392f4d54ae6f3cb3a21b788037705933bbd037315477c7f29e919d0233b83d7c7fc2ced9de1dcbf670c20e06f2
7
+ data.tar.gz: 62a00aa0dd68c73d4e1ed57cc941e1797f8c6e43df03ccb8d5d84001728925ff043100dbb72c98790cf1a61dcbe20e32b62c0141755a0113e1f84668070a7cca
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  *.gem
2
- doc
2
+ doc
3
+ .idea/
data/Gemfile.lock CHANGED
@@ -1,27 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- waithook (0.3.3)
4
+ waithook (0.4.0)
5
5
  websocket (~> 1.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- addressable (2.5.2)
11
- public_suffix (>= 2.0.2, < 4.0)
10
+ addressable (2.8.7)
11
+ public_suffix (>= 2.0.2, < 7.0)
12
12
  ansi (1.5.0)
13
- builder (3.2.3)
14
- excon (0.60.0)
15
- minitest (5.11.3)
16
- minitest-reporters (1.1.19)
13
+ builder (3.3.0)
14
+ excon (0.112.0)
15
+ minitest (5.25.1)
16
+ minitest-reporters (1.7.1)
17
17
  ansi
18
18
  builder
19
19
  minitest (>= 5.0)
20
20
  ruby-progressbar
21
- public_suffix (3.0.2)
22
- rake (12.3.0)
23
- ruby-progressbar (1.9.0)
24
- websocket (1.2.5)
21
+ public_suffix (6.0.1)
22
+ rake (13.2.1)
23
+ ruby-progressbar (1.13.0)
24
+ websocket (1.2.11)
25
25
 
26
26
  PLATFORMS
27
27
  ruby
@@ -34,4 +34,4 @@ DEPENDENCIES
34
34
  waithook!
35
35
 
36
36
  BUNDLED WITH
37
- 1.16.1
37
+ 2.5.11
data/bin/waithook CHANGED
@@ -23,6 +23,10 @@ begin
23
23
  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
24
24
  options[:verbose] = v
25
25
  end
26
+ opts.on("--[no-]debug", "Run verbosely") do |v|
27
+ options[:verbose] = v
28
+ end
29
+
26
30
  opts.on("-f URL", "--forward URL", "Forward to HTTP server") do |value|
27
31
  options[:forward] = value
28
32
  end
data/lib/waithook/cli.rb CHANGED
@@ -39,7 +39,7 @@ class Waithook
39
39
 
40
40
  while true
41
41
  message = waithook.wait_message
42
- puts message.message
42
+ puts message.pretty_print
43
43
  if options[:forward]
44
44
  Thread.new do
45
45
  begin
@@ -1,3 +1,3 @@
1
1
  class Waithook
2
- VERSION = "0.3.4"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -199,6 +199,10 @@ class Waithook
199
199
  def _process_frame(message)
200
200
  logger.trace "Received :#{message.type} #{message.data ? "DATA: #{message.data}" : "(no data)"}"
201
201
 
202
+ if message.type == :pong
203
+ logger.debug "Got pong"
204
+ end
205
+
202
206
  if message.type == :ping
203
207
  send_pong!
204
208
  end
@@ -208,6 +212,13 @@ class Waithook
208
212
  end
209
213
  end
210
214
 
215
+ def ping_sender(interval = 60)
216
+ while @is_open
217
+ send_ping!
218
+ sleep interval
219
+ end
220
+ end
221
+
211
222
  def _wait_frames!
212
223
  while char = @socket.getc
213
224
  @frame_parser << char
data/lib/waithook.rb CHANGED
@@ -47,7 +47,7 @@ class Waithook
47
47
 
48
48
  # Filter (Proc), can be used to to filter incoming messages
49
49
  attr_accessor :filter
50
- # Array of all recieved messages
50
+ # Array of all received messages
51
51
  attr_accessor :messages
52
52
  # Websocket client, instance of Waithook::WebsocketClient
53
53
  attr_reader :client
@@ -106,9 +106,23 @@ class Waithook
106
106
  raise "Waithook connection already started" if @started
107
107
  @started = true
108
108
  @client.connect!.wait_handshake!
109
+ start_pinger_thread!
110
+
109
111
  self
110
112
  end
111
113
 
114
+ def start_pinger_thread!
115
+ Thread.new do
116
+ begin
117
+ logger.debug("Sending ping every 60 seconds")
118
+ client.ping_sender
119
+ logger.debug("Exit ping sender thread")
120
+ rescue => error
121
+ logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
122
+ end
123
+ end
124
+ end
125
+
112
126
  # Whenever connected to server or not
113
127
  def started?
114
128
  !!@started
@@ -116,7 +130,7 @@ class Waithook
116
130
 
117
131
  # Send all incoming webhook requests to running HTTP server
118
132
  #
119
- # webhook = Waithook.subscribe(path: 'my-webhoooks').forward_to('http://localhost:3000/notification')
133
+ # webhook = Waithook.subscribe(path: 'my-webhooks').forward_to('http://localhost:3000/notification')
120
134
  #
121
135
  def forward_to(url, options = {})
122
136
  webhook = wait_message(options)
@@ -132,7 +146,7 @@ class Waithook
132
146
  Timeout.timeout(timeout) do
133
147
  while true
134
148
  _, data = @client.wait_message
135
- webhook = Webhook.new(data)
149
+ webhook = Webhook.new(data, logger)
136
150
  if @filter && @filter.call(webhook) || !@filter
137
151
  @messages << webhook
138
152
  return webhook
@@ -140,7 +154,7 @@ class Waithook
140
154
  end
141
155
  end
142
156
  rescue Timeout::Error => error
143
- time_diff = (Time.now.to_f - start_time).round(3)
157
+ time_diff = (Time.now.to_f - start_time.to_i).round(3)
144
158
  logger.error "#{error.class}: #{error.message} (after #{time_diff} seconds)"
145
159
  raise error if raise_timeout_error
146
160
  return nil
@@ -166,13 +180,37 @@ class Waithook
166
180
  # Raw message from waithook server
167
181
  attr_reader :message
168
182
 
169
- def initialize(payload)
183
+ def initialize(payload, logger = nil)
170
184
  @message = payload
171
185
  data = JSON.parse(@message)
172
186
  @url = data['url']
173
187
  @headers = data['headers']
174
188
  @body = data['body']
175
189
  @method = data['method']
190
+ @logger = logger
191
+ end
192
+
193
+ def pretty_print
194
+ if @body.start_with?('{') && @body.end_with?('}') || @body.start_with?('[') && @body.end_with?(']')
195
+ begin
196
+ body_data = JSON.parse(body)
197
+ pretty_body = JSON.pretty_generate(body_data)
198
+ data_without_body = JSON.parse(@message)
199
+ data_without_body.delete('body')
200
+
201
+ begin
202
+ require 'coderay'
203
+ pretty_body = CodeRay.scan(pretty_body, :json).term
204
+ rescue => error
205
+ logger&.debug("Error while trying to use CodeRay: #{error.message}")
206
+ end
207
+ return "#{JSON.pretty_generate(data_without_body)}\nBody:\n#{pretty_body}"
208
+ rescue JSON::ParserError => error
209
+ logger&.debug("Error while parsing json body: #{error.message}")
210
+ end
211
+ end
212
+
213
+ return message
176
214
  end
177
215
 
178
216
  # Returns Hash.
@@ -185,7 +223,7 @@ class Waithook
185
223
 
186
224
  # Send webhook information to other HTTP server
187
225
  #
188
- # webhook = Waithook.subscribe(path: 'my-webhoooks').wait_message
226
+ # webhook = Waithook.subscribe(path: 'my-webhooks').wait_message
189
227
  # webhook.send_to('http://localhost:3000/notification')
190
228
  #
191
229
  def send_to(url)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waithook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Evstigneev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-19 00:00:00.000000000 Z
11
+ date: 2024-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket