rack-traffic-logger 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b956c1f090a00f5a1a2a9312a2d75d553110a1a5
4
- data.tar.gz: 61e0c4abd72b7273951b6b6a3bcca482b0debea1
3
+ metadata.gz: 1120835c0da5e0f0c12db85aabed34df53cbb863
4
+ data.tar.gz: 252fa5e46838aeeb1062bf5bbaba6895a9232c42
5
5
  SHA512:
6
- metadata.gz: 1a1f32d6b739d39af2375383f659aa3f9efa37a69d47e0c8f6ddb22b2bca5dc355074f2e879590109fd2f035c2aaa7d0e84f35734535e4346514bbfb1dff7d89
7
- data.tar.gz: 81552abbaa6f568e6af21cb2e88fb0c98c831b602b54cae47883981d4d6eb2b568977e975ea31dd2af132af1c73a4e84eb9dcd8a1e5bf537e91afd3b4c63110d
6
+ metadata.gz: 37ec6e8bba2e9da12241eb85234f42a2cf16f7e0f2c84bd6a90097178aa178acd754d2e690895d9b67505cd77862b3df544a9b6bdd56e25cb5a0bde9a29ad899
7
+ data.tar.gz: 1c9d85c8cc6489cc3924a69f2ff9a4eb7406326d331155b052e9fb1f6fdeb01a76ab272f9849c11f4cbb429d2b63f8cf49042821da08f2198cdd1d3310ce302d
data/README.md CHANGED
@@ -134,6 +134,25 @@ use Rack::TrafficLogger, 'file.log', :request_headers, 401 => false, 500...600 =
134
134
  use Rack::TrafficLogger, 'file.log', [:get, :head] => 200..204, post: {only: {201 => :request_bodies}}, [:put, :patch] => :all
135
135
  ```
136
136
 
137
+ #### Shorthand Syntax
138
+
139
+ Use shorthand syntax if you want to configure logging through a string-based configuration medium. The previous examples could also be written as:
140
+
141
+ ```ruby
142
+ use Rack::TrafficLogger, 'file.log', 'ih,401:f,5**:a,2**:{po:ib,de:f}'
143
+ use Rack::TrafficLogger, 'file.log', '[ge,he]:200-204,po:{o:{201:ib}},[pu,pa]:a'
144
+ ```
145
+
146
+ It's ruby, plus these rules:
147
+
148
+ - Omit colons from Symbols. All strings of letters are converted to symbols (except `false`).
149
+ - Use colons in place of hash rockets.
150
+ - Use hyphens for ranges, i.e. `200-204` instead of `200..204`.
151
+ - Use splats in place of large ranges, i.e. `40*` instead of `400..409`.
152
+ - Write only the first two letters of HTTP verbs, e.g. `po` for `post`.
153
+ - Use `a` for `all`, `h` for `headers`, `b` for `bodies`, `ih` for `request_headers`, `ib` for `request_bodies`, `oh` for `response_headers`, and `ob` for `response_bodies` (think of `i` for *input*, and `o` for *output*).
154
+ - Use `o` for `only` and `f` for false.
155
+
137
156
  ### Tailing a JSON log
138
157
 
139
158
  Tailing a JSON log can induce migraines. There are a couple of solutions:
@@ -0,0 +1,74 @@
1
+ module Rack
2
+ class TrafficLogger
3
+ class OptionInterpreter
4
+ class Shorthand
5
+
6
+ ABBREVIATONS = {
7
+ ge: :get,
8
+ po: :post,
9
+ pu: :put,
10
+ pa: :patch,
11
+ de: :delete,
12
+ op: :options,
13
+ he: :head,
14
+ tr: :trace,
15
+ h: :headers,
16
+ b: :bodies,
17
+ a: :all,
18
+ ih: :request_headers,
19
+ ib: :request_bodies,
20
+ oh: :response_headers,
21
+ ob: :response_bodies,
22
+ o: :only,
23
+ f: :false
24
+
25
+ }.map { |k, v| [k.to_s, v.to_s] }.to_h
26
+
27
+ def self.transform(input)
28
+ raise ArgumentError, 'Input must be a string' unless String === input
29
+ new(input).transform
30
+ end
31
+
32
+ def initialize(input)
33
+ @input = input
34
+ end
35
+
36
+ def transform
37
+ @string = @input.dup
38
+ expand_abbreviations
39
+ ranges
40
+ hash_rockets_and_symbols
41
+ wrappers
42
+ instance_eval @string
43
+ end
44
+
45
+ private
46
+
47
+ attr_accessor :string
48
+
49
+ def expand_abbreviations
50
+ @string.gsub!(/[_a-z]+/) { |m| ABBREVIATONS[m] || m }
51
+ end
52
+
53
+ def ranges
54
+ @string.gsub!(/(\d+)-(\d+)/) { "(#{$1}..#{$2})" }
55
+ @string.gsub!(/[1-5][\d*]\*/) { |m| '(%s..%s)' % [m.gsub('*', '0'), m.gsub('*', '9')] }
56
+ end
57
+
58
+ def hash_rockets_and_symbols
59
+ @string.gsub! ':', '=>'
60
+ @string.gsub!(/[_a-z]+/) { |m| m == 'false' ? m : ":#{m}" }
61
+ end
62
+
63
+ def wrappers
64
+ @string.gsub!(/[\[{]/) { |m| "(#{m}" }
65
+ @string.gsub!(/[\]}]/) { |m| "#{m})" }
66
+ @string.gsub! '!', ' _false!'
67
+ @string.gsub! '+', ' _only!'
68
+ @string = "[#{@string}]"
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,5 @@
1
+ require_relative 'option_interpreter/shorthand'
2
+
1
3
  module Rack
2
4
  class TrafficLogger
3
5
  class OptionInterpreter
@@ -52,6 +54,7 @@ module Rack
52
54
 
53
55
  def add_rules(input, **filter)
54
56
  input = [input] unless Array === input
57
+ input = Shorthand.new(input.shift).transform + input while String === input.first
55
58
  verb = filter[:verb]
56
59
  code = filter[:code]
57
60
  input.each do |token|
@@ -13,8 +13,8 @@ module Rack
13
13
  @input = input
14
14
  @output = output
15
15
  @formatter = Formatter::Stream.new(**options)
16
- Signal.trap('INT') { exit 0 }
17
- loop { readline }
16
+ Signal.trap('INT') { done }
17
+ readline until @done
18
18
  end
19
19
 
20
20
  # Reads a line from input, formats it, and sends it to output
@@ -27,7 +27,7 @@ module Rack
27
27
  rescue IO::EAGAINWaitReadable
28
28
  sleep 0.1
29
29
  rescue EOFError
30
- exit 0
30
+ return done
31
31
  end
32
32
  end
33
33
  end
@@ -44,6 +44,10 @@ module Rack
44
44
  end
45
45
  end
46
46
 
47
+ def done
48
+ @done = true
49
+ end
50
+
47
51
  end
48
52
  end
49
53
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class TrafficLogger
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-traffic-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -40,6 +40,7 @@ files:
40
40
  - lib/rack/traffic_logger/formatter/stream.rb
41
41
  - lib/rack/traffic_logger/header_hash.rb
42
42
  - lib/rack/traffic_logger/option_interpreter.rb
43
+ - lib/rack/traffic_logger/option_interpreter/shorthand.rb
43
44
  - lib/rack/traffic_logger/reader.rb
44
45
  - lib/rack/traffic_logger/stream_simulator.rb
45
46
  - lib/rack/traffic_logger/version.rb