logfmt 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: ebbeb9a0b99c4d279a682953bd40f6585acfc8ff
4
- data.tar.gz: 0c24353022ea70cf672b07cc9126de375b0400d2
3
+ metadata.gz: 3ee31ce93089f77a986eeaf421bc81109629096a
4
+ data.tar.gz: cc396ac537f202d1b5a1422b068c19abdef6cf67
5
5
  SHA512:
6
- metadata.gz: a14c02145816f5ec84487ef96b4fff690c6d1692bd3209c59eb58db7bf1af5b20148e19c6d56d4b99eb6ba8175e540b61852ddac2748441552fcc9d4faf7f4b8
7
- data.tar.gz: 5db326288526225bd1084acaf9ef2fb57a785de7b0f6f7086c92b0e0a4eae58252f052632897f49d60dca488b03f5bd68c4cebe8d263e086cb5a94ada3774c6d
6
+ metadata.gz: f5846e36d0e21b576156ee0c8ef0ef94c503b50279807e734f5aaf6d4351f9f61946d0e51e99f8a545a85f81f5f7ac93f7971bfa069e99dc9a4acd70da5372ed
7
+ data.tar.gz: ef739d7f6a37e95ac60ab94ca1356f90e5e6133682603dd0ae7d87bddfdb08b4bfe5b3ab9bfb6eb91d4447a1569ad42499f4fc12ee3f258a7c1a46759529e1cc
data/lib/logfmt/parser.rb CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  module Logfmt
2
3
  GARBAGE = 0
3
4
  KEY = 1
@@ -5,6 +6,14 @@ module Logfmt
5
6
  IVALUE = 3
6
7
  QVALUE = 4
7
8
 
9
+ def self.numeric?(s)
10
+ return s.match(/\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/)
11
+ end
12
+
13
+ def self.integer?(s)
14
+ return s.match(/\A[-+]?[0-9]+\Z/)
15
+ end
16
+
8
17
  def self.parse(line)
9
18
  output = {}
10
19
  key, value = "", ""
@@ -54,12 +63,22 @@ module Logfmt
54
63
  end
55
64
  if state == IVALUE
56
65
  if not (c > ' ' && c != '"' && c != '=')
66
+ if integer?(value)
67
+ value = Integer(value)
68
+ elsif numeric?(value)
69
+ value = Float(value)
70
+ end
57
71
  output[key.strip()] = value
58
72
  state = GARBAGE
59
73
  else
60
74
  value << c
61
75
  end
62
76
  if i >= line.length
77
+ if integer?(value)
78
+ value = Integer(value)
79
+ elsif numeric?(value)
80
+ value = Float(value)
81
+ end
63
82
  output[key.strip()] = value
64
83
  end
65
84
  next
@@ -1,3 +1,3 @@
1
1
  module Logfmt
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -65,11 +65,65 @@ describe Logfmt do
65
65
 
66
66
  it 'parse mixed characters pairs' do
67
67
  data = Logfmt.parse('foo=bar a=14 baz="hello kitty" ƒ=2h3s cool%story=bro f %^asdf')
68
- expect(data).to eq({"foo" => "bar", "a" => "14", "baz" => "hello kitty", "ƒ" => "2h3s", "cool%story" => "bro", "f" => true, "%^asdf" => true})
68
+ expect(data).to eq({"foo" => "bar", "a" => 14, "baz" => "hello kitty", "ƒ" => "2h3s", "cool%story" => "bro", "f" => true, "%^asdf" => true})
69
69
  end
70
70
 
71
71
  it 'parse pair with empty quote' do
72
72
  data = Logfmt.parse('key=""')
73
73
  expect(data).to eq({"key" => ""})
74
74
  end
75
+
76
+ # Currently, the value comes back as "true", which could mess up stats
77
+ # Really, only "true" should come back as "true"
78
+ # it 'parse 1 as integer type' do
79
+ # data = Logfmt.parse('key=1')
80
+ # expect(data["key"].class).to eq(Fixnum)
81
+ # end
82
+
83
+ it 'parse positive integer as integer type' do
84
+ data = Logfmt.parse('key=234')
85
+ expect(data["key"]).to eq(234)
86
+ expect(data["key"].class).to eq(Fixnum)
87
+ end
88
+
89
+ it 'parse negative integer as integer type' do
90
+ data = Logfmt.parse('key=-3428')
91
+ expect(data["key"]).to eq(-3428)
92
+ expect(data["key"].class).to eq(Fixnum)
93
+ end
94
+
95
+
96
+ it 'parse positive float as float type' do
97
+ data = Logfmt.parse('key=3.342')
98
+ expect(data["key"]).to eq(3.342)
99
+ expect(data["key"].class).to eq(Float)
100
+ end
101
+
102
+ it 'parse negative float as float type' do
103
+ data = Logfmt.parse('key=-0.9934')
104
+ expect(data["key"]).to eq(-0.9934)
105
+ expect(data["key"].class).to eq(Float)
106
+ end
107
+
108
+ it 'parse exponential float as float type' do
109
+ data = Logfmt.parse('key=2.342342342342344e+18')
110
+ expect(data["key"]).to eq(2.342342342342344e+18)
111
+ expect(data["key"].class).to eq(Float)
112
+ end
113
+
114
+ it 'parse quoted integer as string type' do
115
+ data = Logfmt.parse('key="234"')
116
+ expect(data["key"].class).to eq(String)
117
+ end
118
+
119
+ it 'parse quoted float as string type' do
120
+ data = Logfmt.parse('key="3.14"')
121
+ expect(data["key"].class).to eq(String)
122
+ end
123
+
124
+ it 'parse IP address as string type' do
125
+ data = Logfmt.parse('key=10.10.10.1')
126
+ expect(data["key"].class).to eq(String)
127
+ end
128
+
75
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logfmt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timothée Peignier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-24 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet