logfmt_marshalling 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f87ce5ef918a5e1a05d179ffd9243d7995573b9
4
+ data.tar.gz: 2c9d654c9bfb918c77c635ae0de9383a94a1186d
5
+ SHA512:
6
+ metadata.gz: 375fdc64f8bc0ea64869efb2cd5376789b8088fb375d574a287b47f6cc5995c8070f1d15048c2ef9b8a1ed06f729b321b51c67dc225c675f1b273eef39df8851
7
+ data.tar.gz: 070c8f7d81915033f58111b1273cb20b2785e0f9dfeb883cb5308897ef118bb9fefa1e0a97bc8886a57f2d5dc2bcfdf7f0c1af13d69efaedd06273b918c4943d
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .ruby-gemset
5
+ .ruby-version
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
25
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.1
6
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logfmt_marshalling.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 pm
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # LogfmtMarshalling
2
+
3
+ A Ruby gem for emitting [logfmt logging format](https://brandur.org/logfmt).
4
+
5
+ logfmt (as a key-value logging format) is one flavor of [structured logging](http://gregoryszorc.com/blog/category/logging/).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'logfmt_marshalling'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install logfmt_marshalling
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ > require 'logfmt_marshalling'
25
+ => true
26
+ > LogfmtMarshalling.marshal method: 'get', path: '/', status: 200
27
+ => "method=get path=/ status=200"
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/MitinPavel/logfmt_marshalling/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,58 @@
1
+ module LogfmtMarshalling
2
+ class Marshaller
3
+ def marshal(hash)
4
+ hash.inject([]) do |acc, (key, value)|
5
+ case value
6
+ when TrueClass then acc << marshal_key(key)
7
+ else acc << [marshal_key(key), marshal_value(value)].join('=')
8
+ end
9
+ acc
10
+ end.join ' '
11
+ end
12
+
13
+ private
14
+
15
+ def marshal_key(key)
16
+ key.to_s
17
+ end
18
+
19
+ def marshal_value(value)
20
+ case value
21
+ when String then marshal_string_value value
22
+ else value.to_s
23
+ end
24
+ end
25
+
26
+ def marshal_string_value(value)
27
+ handle_special_case(value) || wrap_in_quotes(escapes_double_quotes(value))
28
+ end
29
+
30
+ def escapes_double_quotes(value)
31
+ value.gsub '[^\]"', '\"'
32
+ end
33
+
34
+ def wrap_in_quotes(value)
35
+ if value.match(/\\|\s/)
36
+ %{"#{value}"}
37
+ else
38
+ value
39
+ end
40
+ end
41
+
42
+ def handle_special_case(value)
43
+ case value
44
+ when '' then '""'
45
+ when 'false' then '"false"'
46
+ when 'true' then '"true"'
47
+ when /\d/ then handle_string_with_number(value)
48
+ end
49
+ end
50
+
51
+ def handle_string_with_number(value)
52
+ Kernel.Float value
53
+ %{"#{value}"}
54
+ rescue ArgumentError
55
+ # Not a number. Will be handled later.
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module LogfmtMarshalling
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "logfmt_marshalling/version"
2
+ require "logfmt_marshalling/marshaller"
3
+
4
+ module LogfmtMarshalling
5
+ def self.marshal(hash)
6
+ marshaller = ::LogfmtMarshalling::Marshaller.new
7
+
8
+ marshaller.marshal hash
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logfmt_marshalling/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'logfmt_marshalling'
8
+ spec.version = LogfmtMarshalling::VERSION
9
+ spec.authors = ['Pavel Mitin']
10
+ spec.email = ['mitin.pavel@gmail.com']
11
+ spec.summary = %q{A Ruby gem for emitting strings in logfmt logging format.}
12
+ spec.description = %q{A Ruby gem for emitting strings in logfmt logging format. Check out https://brandur.org/logfmt to get more information about logfmt key-value structured logging.}
13
+ spec.homepage = 'https://github.com/MitinPavel/logfmt_marshalling'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,149 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::LogfmtMarshalling::Marshaller do
4
+ it 'serializes an empty hash' do
5
+ actual = marshal({})
6
+ expect(actual).to eq('')
7
+ end
8
+
9
+ it 'serializes an empty string' do
10
+ actual = marshal 'key' => ''
11
+ expect(actual).to eq('key=""')
12
+ end
13
+
14
+ it 'serializes a whitespace string' do
15
+ actual = marshal 'key' => ' '
16
+ expect(actual).to eq('key=" "')
17
+ end
18
+
19
+ it 'drops the TrueClass instance value' do
20
+ actual = marshal 'key' => true
21
+ expect(actual).to eq('key')
22
+ end
23
+
24
+ it "serializes the FalseClass instance value" do
25
+ actual = marshal 'key' => false
26
+ expect(actual).to eq('key=false')
27
+ end
28
+
29
+ it 'serializes a quoted true as a string' do
30
+ actual = marshal 'key' => 'true'
31
+ expect(actual).to eq('key="true"')
32
+ end
33
+
34
+ it 'serializes a quoted false as a string' do
35
+ actual = marshal 'key' => 'false'
36
+ expect(actual).to eq('key="false"')
37
+ end
38
+
39
+ it 'serializes a single key-value pair' do
40
+ actual = marshal 'key' => 'value'
41
+ expect(actual).to eq('key=value')
42
+ end
43
+
44
+ it 'serializes multiple key-value pairs' do
45
+ actual = marshal 'key1' => true, 'key2' => true
46
+ expect(actual).to eq('key1 key2')
47
+ end
48
+
49
+ it 'preserves order of serialized pairs' do
50
+ actual = marshal 'key1' => 'value1', 'key2' => 'value2'
51
+ expect(actual).to eq('key1=value1 key2=value2')
52
+ end
53
+
54
+ it 'serializes mixed single/non-single pairs' do
55
+ actual = marshal 'key1' => 'value1', 'key2' => true
56
+ expect(actual).to eq('key1=value1 key2')
57
+ end
58
+
59
+ it 'preserves order of mixed single/non-single pairs' do
60
+ actual = marshal 'key1' => true, 'key2' => 'value2'
61
+ expect(actual).to eq('key1 key2=value2')
62
+ end
63
+
64
+ it 'quotes values with whitespaces' do
65
+ actual = marshal 'key' => 'a value'
66
+ expect(actual).to eq('key="a value"')
67
+ end
68
+
69
+ it 'serializes escaped quote value ' do
70
+ actual = marshal 'key' => 'quoted \" value', 'r' => 'esc\\t'
71
+ expect(actual).to eq('key="quoted \" value" r="esc\t"')
72
+ end
73
+
74
+ it 'serializes mixed pairs' do
75
+ actual = marshal 'key1' => 'quoted \" value', 'key2' => true, 'key3' => 'value3'
76
+ expect(actual).to eq('key1="quoted \" value" key2 key3=value3')
77
+ end
78
+
79
+ it 'serializes mixed characters pairs' do
80
+ actual = marshal 'foo' => 'bar',
81
+ 'a' => 14,
82
+ 'baz' => 'hello kitty',
83
+ 'ƒ' => '2h3s',
84
+ 'cool%story' => 'bro',
85
+ 'f' => true,
86
+ '%^asdf' => true
87
+ expect(actual).to eq('foo=bar a=14 baz="hello kitty" ƒ=2h3s cool%story=bro f %^asdf')
88
+ end
89
+
90
+ it 'serializes Ruby Symbol objects as Strings' do
91
+ actual = marshal key: :value
92
+ expect(actual).to eq('key=value')
93
+ end
94
+
95
+ it 'serializes a positive integer' do
96
+ actual = marshal 'key' => 234
97
+ expect(actual).to eq('key=234')
98
+ end
99
+
100
+ it 'serializes a negative integer' do
101
+ actual = marshal 'key' => -3428
102
+ expect(actual).to eq('key=-3428')
103
+ end
104
+
105
+ it 'serializes a bignum' do
106
+ bignum = 9999999999999999999
107
+ expect(bignum).to be_a(Bignum)
108
+
109
+ actual = marshal 'key' => bignum
110
+
111
+ expect(actual).to eq('key=9999999999999999999')
112
+ end
113
+
114
+ it 'serializes a positive float' do
115
+ actual = marshal 'key' => 3.14
116
+ expect(actual).to eq('key=3.14')
117
+ end
118
+
119
+ it 'serializes a negative float' do
120
+ actual = marshal 'key' => -0.9934
121
+ expect(actual).to eq('key=-0.9934')
122
+ end
123
+
124
+ it 'serializes an exponential float' do
125
+ actual = marshal 'key' => 2.342342342342344e+18
126
+ expect(actual).to eq('key=2.342342342342344e+18')
127
+ end
128
+
129
+ %w(
130
+ 0
131
+ 1
132
+ -1
133
+ 123.4
134
+ -3.14
135
+ 1.0e6
136
+ 1.2e-3
137
+ 4E20
138
+ 4e+20
139
+ ).each do |quoted_float|
140
+ it %{serializes a quoted number ("#{quoted_float}") as a string} do
141
+ actual = marshal 'key' => quoted_float
142
+ expect(actual).to eq(%{key="#{quoted_float}"})
143
+ end
144
+ end
145
+
146
+ def marshal(data)
147
+ described_class.new.marshal data
148
+ end
149
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::LogfmtMarshalling do
4
+ describe '.marshal' do
5
+ it 'delegates work to LogfmtMarshalling::Marshaller' do
6
+ expect_any_instance_of(::LogfmtMarshalling::Marshaller).to receive(:marshal).with 'k' => 'v'
7
+
8
+ described_class.marshal 'k' => 'v'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require 'logfmt_marshalling'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logfmt_marshalling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Mitin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Ruby gem for emitting strings in logfmt logging format. Check out https://brandur.org/logfmt
56
+ to get more information about logfmt key-value structured logging.
57
+ email:
58
+ - mitin.pavel@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/logfmt_marshalling.rb
70
+ - lib/logfmt_marshalling/marshaller.rb
71
+ - lib/logfmt_marshalling/version.rb
72
+ - logfmt_marshalling.gemspec
73
+ - spec/logfmt_marshalling/marshaller_spec.rb
74
+ - spec/logfmt_marshalling_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/MitinPavel/logfmt_marshalling
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A Ruby gem for emitting strings in logfmt logging format.
100
+ test_files:
101
+ - spec/logfmt_marshalling/marshaller_spec.rb
102
+ - spec/logfmt_marshalling_spec.rb
103
+ - spec/spec_helper.rb