humr 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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +10 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/humr +4 -0
- data/humr.gemspec +30 -0
- data/lib/humr.rb +2 -0
- data/lib/humr/config.rb +42 -0
- data/lib/humr/handler.rb +18 -0
- data/lib/humr/handler/si_prefix.rb +34 -0
- data/lib/humr/handler/time.rb +40 -0
- data/lib/humr/handler/url_escaped.rb +16 -0
- data/lib/humr/handler/user_agent.rb +36 -0
- data/lib/humr/runner.rb +67 -0
- data/lib/humr/splitter.rb +9 -0
- data/lib/humr/splitter/default.rb +34 -0
- data/lib/humr/splitter/ltsv.rb +15 -0
- data/lib/humr/splitter/pattern.rb +35 -0
- data/lib/humr/version.rb +3 -0
- data/spec/humr_spec.rb +2 -0
- data/spec/lib/humr/config_spec.rb +18 -0
- data/spec/lib/humr/handler/si_prefix_spec.rb +20 -0
- data/spec/lib/humr/handler/time_spec.rb +27 -0
- data/spec/lib/humr/handler/url_escaped_spec.rb +20 -0
- data/spec/lib/humr/handler/user_agent_spec.rb +55 -0
- data/spec/lib/humr/runner_spec.rb +16 -0
- data/spec/lib/humr/splitter/default_spec.rb +28 -0
- data/spec/lib/humr/splitter/ltsv_spec.rb +28 -0
- data/spec/lib/humr/splitter/pattern_spec.rb +25 -0
- data/spec/spec_helper.rb +23 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5ffeb104e487991400f9708f9266f2c8d40de2fc
|
4
|
+
data.tar.gz: 5faf4747dee4b9c72770cc75af5df80d40598d04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47c21b4db7fb80fa714fbf205e220893b11f785d710b8e7dd2f0aedf6458013575a35541719c3a021d175c857f0ff0245b729bb21e31d74546b7b082f32f62a5
|
7
|
+
data.tar.gz: 63a96785709ef7217c60b0767d13637f53c42a7729dd872dea69e0cde48543e80f5795bab149a4ad9007cdbe660fc984b366790154061685547bb6d59c47ee16
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
notification 'terminal_notifier'
|
5
|
+
|
6
|
+
guard :rspec do
|
7
|
+
watch(%r{^spec/.+_spec\.rb$})
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
9
|
+
watch('spec/spec_helper.rb') { "spec" }
|
10
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 motemen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 motemen
|
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,29 @@
|
|
1
|
+
# Humr
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'humr'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install humr
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/humr
ADDED
data/humr.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'humr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "humr"
|
8
|
+
spec.version = Humr::VERSION
|
9
|
+
spec.authors = ["motemen"]
|
10
|
+
spec.email = ["motemen@gmail.com"]
|
11
|
+
spec.summary = 'A CLI tool that make input human-readable'
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency 'term-ansicolor', '~> 1'
|
21
|
+
spec.add_runtime_dependency 'useragent', '~> 0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'guard'
|
27
|
+
spec.add_development_dependency 'guard-rspec'
|
28
|
+
spec.add_development_dependency 'terminal-notifier-guard'
|
29
|
+
spec.add_development_dependency 'simplecov'
|
30
|
+
end
|
data/lib/humr.rb
ADDED
data/lib/humr/config.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Humr
|
2
|
+
class Config
|
3
|
+
DEFAULT = {
|
4
|
+
:handlers => [
|
5
|
+
:url, :si, :time, :ua
|
6
|
+
],
|
7
|
+
:handler => {
|
8
|
+
:url => { :color => :green },
|
9
|
+
:si => { :color => :cyan },
|
10
|
+
:time => { :color => :yellow },
|
11
|
+
:ua => { :color => :magenta },
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
COLORS = [
|
16
|
+
:red,
|
17
|
+
:green,
|
18
|
+
:yellow,
|
19
|
+
:blue,
|
20
|
+
:magenta,
|
21
|
+
:cyan
|
22
|
+
]
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@config = DEFAULT.dup
|
26
|
+
end
|
27
|
+
|
28
|
+
def handlers
|
29
|
+
@config[:handlers].map(&:to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
def color(handler)
|
33
|
+
name = if handler.kind_of?(Symbol)
|
34
|
+
handler
|
35
|
+
else
|
36
|
+
handler.name
|
37
|
+
end
|
38
|
+
|
39
|
+
@config[:handler][name][:color]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/humr/handler.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'humr/handler'
|
2
|
+
|
3
|
+
module Humr
|
4
|
+
class Handler::SIPrefix < Handler
|
5
|
+
register :si
|
6
|
+
|
7
|
+
PREFIXES = [ '' ] + %w(k M G T P E Z Y)
|
8
|
+
|
9
|
+
def base
|
10
|
+
1000
|
11
|
+
end
|
12
|
+
|
13
|
+
def replace(s, &block)
|
14
|
+
if /^\d{4,}$/ === s
|
15
|
+
s.gsub(/(\d{4,})/) do |size|
|
16
|
+
n = size.to_f
|
17
|
+
i = 0
|
18
|
+
while n >= base and i < PREFIXES.length - 1
|
19
|
+
n = n / base
|
20
|
+
i = i + 1
|
21
|
+
end
|
22
|
+
|
23
|
+
format = if n < 10
|
24
|
+
'%.1f%s'
|
25
|
+
else
|
26
|
+
'%d%s'
|
27
|
+
end
|
28
|
+
|
29
|
+
yield format % [ n, PREFIXES[i] ]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'humr/handler'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module Humr
|
5
|
+
class Handler::Time < Handler
|
6
|
+
register :time
|
7
|
+
|
8
|
+
def parsers
|
9
|
+
@parsers ||= [
|
10
|
+
method(:_apache_common_log_time),
|
11
|
+
method(:_ctime),
|
12
|
+
::Time.method(:iso8601),
|
13
|
+
::Time.method(:httpdate),
|
14
|
+
::Time.method(:rfc822)
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
def _apache_common_log_time(s)
|
19
|
+
::Time.strptime(s, '%d/%b/%Y:%H:%M:%S %Z')
|
20
|
+
end
|
21
|
+
|
22
|
+
def _ctime(s)
|
23
|
+
::Time.strptime(s, '%c')
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse(s)
|
27
|
+
for parser in parsers
|
28
|
+
parsed = parser[s] rescue nil
|
29
|
+
return parsed if parsed
|
30
|
+
end
|
31
|
+
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def replace(s, &block)
|
36
|
+
parsed = parse(s)
|
37
|
+
parsed.to_s.sub(/.*/, &block) if parsed
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'humr/handler'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Humr
|
5
|
+
class Handler::URLEscaped < Handler
|
6
|
+
register :url
|
7
|
+
|
8
|
+
def replace(s, &block)
|
9
|
+
if /%[A-Fa-f0-9]{2}/ === s
|
10
|
+
s.gsub(/((?:%[A-Fa-f0-9]{2})+)/) do |url_escaped|
|
11
|
+
yield URI.unescape(url_escaped)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'humr/handler'
|
2
|
+
require 'useragent'
|
3
|
+
|
4
|
+
module Humr
|
5
|
+
class Handler::UserAgent < Handler
|
6
|
+
register :ua
|
7
|
+
|
8
|
+
def rough_version(s)
|
9
|
+
s.sub(/(\d+\.\d+)(?:\.\d+)*/, '\1').sub(/\.0$/, '')
|
10
|
+
end
|
11
|
+
|
12
|
+
UA_LIKE_PATTERN = %r<^(?:[\w-]+(?:/[\w.-]+)?(?:\s*\([^\)]+\))?\s*)+$>
|
13
|
+
|
14
|
+
def replace(s, &block)
|
15
|
+
return unless UA_LIKE_PATTERN.match(s)
|
16
|
+
|
17
|
+
ua = ::UserAgent.parse(s)
|
18
|
+
|
19
|
+
return unless ua.version
|
20
|
+
|
21
|
+
readable = if ua.bot?
|
22
|
+
ua.os
|
23
|
+
else
|
24
|
+
'%s %s%s' % [
|
25
|
+
ua.browser,
|
26
|
+
rough_version(ua.version.to_s),
|
27
|
+
if ua.os and not ua.os.empty?
|
28
|
+
" (#{rough_version(ua.os)})"
|
29
|
+
end
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
readable.sub(/.*/, &block) if readable
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/humr/runner.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'humr/handler/url_escaped'
|
2
|
+
require 'humr/handler/si_prefix'
|
3
|
+
require 'humr/handler/time'
|
4
|
+
require 'humr/handler/user_agent'
|
5
|
+
require 'humr/splitter'
|
6
|
+
|
7
|
+
require 'optparse'
|
8
|
+
require 'term/ansicolor'
|
9
|
+
|
10
|
+
module Humr
|
11
|
+
class Runner
|
12
|
+
attr_reader :config
|
13
|
+
|
14
|
+
def initialize(args)
|
15
|
+
@args = args
|
16
|
+
@config = Config.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.bootstrap(args)
|
20
|
+
new(args).run
|
21
|
+
end
|
22
|
+
|
23
|
+
def splitter
|
24
|
+
@splitter ||= Splitter::Default.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def handlers
|
28
|
+
@handlers ||= config.handlers.map do |name|
|
29
|
+
Handler[name].new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def run
|
34
|
+
OptionParser.new do |opts|
|
35
|
+
opts.on('-s', '--splitter SPLITTER[:ARGS]', 'Specify ield splitter (default, pattern:re, ltsv)') do |splitter|
|
36
|
+
impl, *args = splitter.split(/:/, 2)
|
37
|
+
@splitter = Splitter::Impl[impl.to_sym].new(*args)
|
38
|
+
end
|
39
|
+
end.parse!(@args)
|
40
|
+
|
41
|
+
STDIN.each_line do |line|
|
42
|
+
puts readable_line(line.chomp)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def readable_line(line)
|
47
|
+
splitter.sub_each_field(line) do |field|
|
48
|
+
readable_field field
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def readable_field(field)
|
53
|
+
handlers.each do |handler|
|
54
|
+
readable = handler.replace(field) do |chunk|
|
55
|
+
colorize(chunk, handler.name)
|
56
|
+
end
|
57
|
+
return readable if readable
|
58
|
+
end
|
59
|
+
|
60
|
+
field
|
61
|
+
end
|
62
|
+
|
63
|
+
def colorize(chunk, handler)
|
64
|
+
Term::ANSIColor.send(config.color(handler), chunk)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module Humr
|
4
|
+
module Splitter
|
5
|
+
class Default
|
6
|
+
Impl[:default] = self
|
7
|
+
|
8
|
+
def sub_each_field(line, &block)
|
9
|
+
scanner = StringScanner.new(line)
|
10
|
+
|
11
|
+
result = ''
|
12
|
+
index = 1
|
13
|
+
|
14
|
+
loop do
|
15
|
+
if s = scanner.scan(/".*?"|\[.*?\]/)
|
16
|
+
result << s[0]
|
17
|
+
result << yield(s[1..-2], index)
|
18
|
+
result << s[-1]
|
19
|
+
index += 1
|
20
|
+
elsif s = scanner.scan(/\S+/)
|
21
|
+
result << yield(s, index)
|
22
|
+
index += 1
|
23
|
+
elsif not scanner.eos?
|
24
|
+
result << scanner.scan(/\s*/)
|
25
|
+
else
|
26
|
+
break
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
result
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
module Humr
|
4
|
+
module Splitter
|
5
|
+
class Pattern
|
6
|
+
Impl[:pattern] = self
|
7
|
+
|
8
|
+
def initialize(pattern)
|
9
|
+
@pattern = Regexp.new(pattern)
|
10
|
+
@pattern_la = Regexp.new("(?=#{pattern})")
|
11
|
+
end
|
12
|
+
|
13
|
+
def sub_each_field(line, &block)
|
14
|
+
scanner = StringScanner.new(line)
|
15
|
+
|
16
|
+
result = ''
|
17
|
+
index = 1
|
18
|
+
|
19
|
+
loop do
|
20
|
+
field = scanner.scan_until(@pattern_la)
|
21
|
+
if field
|
22
|
+
result << yield(field, index)
|
23
|
+
result << scanner.scan(@pattern)
|
24
|
+
index += 1
|
25
|
+
else
|
26
|
+
result << yield(scanner.rest, index)
|
27
|
+
break
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/humr/version.rb
ADDED
data/spec/humr_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/config'
|
3
|
+
|
4
|
+
describe Humr::Config do
|
5
|
+
subject(:config) { Humr::Config.new }
|
6
|
+
|
7
|
+
describe '#handlers' do
|
8
|
+
it 'has initial value' do
|
9
|
+
expect(config.handlers).to eq([ :url, :si, :time, :ua ])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#color' do
|
14
|
+
it 'returns color tag for a handler' do
|
15
|
+
expect(config.color(:url)).to eq(:green)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/handler/si_prefix'
|
3
|
+
|
4
|
+
describe Humr::Handler::SIPrefix do
|
5
|
+
subject(:handler) { Humr::Handler::SIPrefix.new }
|
6
|
+
|
7
|
+
include_examples 'humr_handler', :si, Humr::Handler::SIPrefix
|
8
|
+
|
9
|
+
it 'handles kilo' do
|
10
|
+
expect { |b| handler.replace('1000', &b) }.to yield_with_args('1.0k')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'handles mega' do
|
14
|
+
expect { |b| handler.replace((20 * 1000 * 1000).to_s, &b) }.to yield_with_args('20M')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'ignores non-numbers' do
|
18
|
+
expect { |b| handler.replace('foobarbaz', &b) }.not_to yield_control
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/handler/time'
|
3
|
+
|
4
|
+
describe Humr::Handler::Time do
|
5
|
+
subject(:handler) { Humr::Handler::Time.new }
|
6
|
+
|
7
|
+
include_examples 'humr_handler', :time, Humr::Handler::Time
|
8
|
+
|
9
|
+
describe '#parse' do
|
10
|
+
it 'parses Apache common log format' do
|
11
|
+
expect(handler.parse('17/Jul/2013:00:19:52 +0900')).to be_kind_of(Time)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'parses HTTP date format' do
|
15
|
+
expect(handler.parse('Tue, 16 Jul 2013 15:33:33 GMT')).to be_kind_of(Time)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'parses ctime format' do
|
19
|
+
expect(handler.parse('Tue Jul 23 13:04:11 2013 +0900')).to be_kind_of(Time)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'does not parse non-time string' do
|
23
|
+
expect(handler.parse('foobarbaz')).to be(nil)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/handler/url_escaped'
|
3
|
+
|
4
|
+
describe Humr::Handler::URLEscaped do
|
5
|
+
subject(:handler) { Humr::Handler::URLEscaped.new }
|
6
|
+
|
7
|
+
include_examples 'humr_handler', :url, Humr::Handler::URLEscaped
|
8
|
+
|
9
|
+
id = lambda { |x| x }
|
10
|
+
|
11
|
+
it 'handles URL-escaped string' do
|
12
|
+
expect { |b| handler.replace('/search?q=%E7%BE%8E%E9%A1%94%E5%99%A8', &b) }.to yield_with_args('美顔器')
|
13
|
+
expect(handler.replace('/search?q=%E7%BE%8E%E9%A1%94%E5%99%A8', &id)).to eq('/search?q=美顔器')
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'ignores non URL-escaped string' do
|
17
|
+
expect { |b| handler.replace('/foo/bar/baz', &b) }.not_to yield_control
|
18
|
+
expect(handler.replace('/foo/bar/baz', &id)).to be(nil)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/handler/user_agent'
|
3
|
+
|
4
|
+
describe Humr::Handler::UserAgent do
|
5
|
+
subject(:handler) { Humr::Handler::UserAgent.new }
|
6
|
+
|
7
|
+
include_examples 'humr_handler', :ua, Humr::Handler::UserAgent
|
8
|
+
|
9
|
+
GOOD_CASES = [
|
10
|
+
[
|
11
|
+
'Chrome',
|
12
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1612.0 Safari/537.36',
|
13
|
+
'Chrome 31 (OS X 10.8)'
|
14
|
+
],
|
15
|
+
[
|
16
|
+
'IE',
|
17
|
+
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)',
|
18
|
+
'Internet Explorer 10 (Windows 7)'
|
19
|
+
],
|
20
|
+
[
|
21
|
+
'Googlebot',
|
22
|
+
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
|
23
|
+
'Googlebot/2.1'
|
24
|
+
]
|
25
|
+
]
|
26
|
+
|
27
|
+
GOOD_CASES.each do |tuple|
|
28
|
+
name, full_ua, readable_ua = *tuple
|
29
|
+
it "handles UA string (#{name})" do
|
30
|
+
expect { |b| handler.replace(full_ua, &b) }.to yield_with_args(readable_ua)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'ignores non-ua string' do
|
35
|
+
expect { |b| handler.replace('foo bar baz', &b) }.not_to yield_control
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'ignores non-ua string (HTTP request line)' do
|
39
|
+
expect { |b| handler.replace('GET /foo/bar/1234 HTTP/1.0', &b) }.not_to yield_control
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'handles unrecognized ua string' do
|
43
|
+
expect { |b| handler.replace('Yeti/1.0 (NHN Corp.; http://help.naver.com/robots/)', &b) }.not_to yield_control
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#rough_version' do
|
47
|
+
it 'rounds to first decimal point' do
|
48
|
+
expect(handler.rough_version('7.2.3')).to eq('7.2')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'cuts off trailing zeros' do
|
52
|
+
expect(handler.rough_version('7.0.0')).to eq('7')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr'
|
3
|
+
|
4
|
+
describe Humr::Runner do
|
5
|
+
subject { Humr::Runner.new(nil) }
|
6
|
+
|
7
|
+
describe '#readable_line' do
|
8
|
+
it 'handles accesslog' do
|
9
|
+
expect(subject.readable_line(<<-INPUT)).to eq(<<-READABLE)
|
10
|
+
66.249.66.33 - - [08/Sep/2013:04:33:45 +0900] "GET /randompoku/?%E3%82%AF%E3%83%83%E3%82%AF%E3%83%91%E3%83%83%E3%83%89 HTTP/1.1" 200 4710 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
11
|
+
INPUT
|
12
|
+
66.249.66.33 - - [#{'2013-09-08 04:33:45 +0900'.color(:yellow)}] "GET /randompoku/?#{'クックパッド'.color(:green)} HTTP/1.1" 200 #{'4.7k'.color(:cyan)} "-" "#{'Googlebot/2.1'.color(:magenta)}"
|
13
|
+
READABLE
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/splitter'
|
3
|
+
require 'humr/splitter/default'
|
4
|
+
|
5
|
+
describe Humr::Splitter::Default do
|
6
|
+
subject(:splitter) { Humr::Splitter::Default.new }
|
7
|
+
|
8
|
+
it 'calls block with field value and index' do
|
9
|
+
expect { |b|
|
10
|
+
splitter.sub_each_field %q<66.249.66.33 - - [08/Sep/2013:04:33:45 +0900] "GET /randompoku/?%E3%82%AF%E3%83%83%E3%82%AF%E3%83%91%E3%83%83%E3%83%89 HTTP/1.1" 200 4710 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"> do |*args|
|
11
|
+
b.to_proc.call(*args)
|
12
|
+
""
|
13
|
+
end
|
14
|
+
}.to yield_successive_args(
|
15
|
+
*[
|
16
|
+
'66.249.66.33',
|
17
|
+
'-',
|
18
|
+
'-',
|
19
|
+
'08/Sep/2013:04:33:45 +0900',
|
20
|
+
'GET /randompoku/?%E3%82%AF%E3%83%83%E3%82%AF%E3%83%91%E3%83%83%E3%83%89 HTTP/1.1',
|
21
|
+
'200',
|
22
|
+
'4710',
|
23
|
+
'-',
|
24
|
+
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
|
25
|
+
].unshift(nil).to_enum.each_with_index.to_a[1..-1]
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/splitter/ltsv'
|
3
|
+
|
4
|
+
describe Humr::Splitter::LTSV do
|
5
|
+
subject(:splitter) { Humr::Splitter::LTSV.new }
|
6
|
+
|
7
|
+
it 'calls block with field value and index' do
|
8
|
+
expect { |b|
|
9
|
+
splitter.sub_each_field %Q<host:127.0.0.1\tident:-\tuser:frank\ttime:[10/Oct/2000:13:55:36 -0700]\treq:GET /apache_pb.gif HTTP/1.0\tstatus:200\tsize:2326\treferer:http://www.example.com/start.html\tua:Mozilla/4.08 [en] (Win98; I ;Nav)> do |*args|
|
10
|
+
b.to_proc.call(*args)
|
11
|
+
""
|
12
|
+
end
|
13
|
+
}.to yield_successive_args(
|
14
|
+
*[
|
15
|
+
'127.0.0.1',
|
16
|
+
'-',
|
17
|
+
'frank',
|
18
|
+
'[10/Oct/2000:13:55:36 -0700]',
|
19
|
+
'GET /apache_pb.gif HTTP/1.0',
|
20
|
+
'200',
|
21
|
+
'2326',
|
22
|
+
'http://www.example.com/start.html',
|
23
|
+
'Mozilla/4.08 [en] (Win98; I ;Nav)'
|
24
|
+
].unshift(nil).to_enum.each_with_index.to_a[1..-1]
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'humr/splitter/pattern'
|
3
|
+
|
4
|
+
describe Humr::Splitter::Pattern do
|
5
|
+
subject(:splitter) { Humr::Splitter::Pattern.new('\t+') }
|
6
|
+
|
7
|
+
it 'splits by specified pattern' do
|
8
|
+
expect(
|
9
|
+
splitter.sub_each_field("a\tbc\t\txyz") { |f,i| f.upcase }
|
10
|
+
).to eq("A\tBC\t\tXYZ")
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'calls block with field value and index' do
|
14
|
+
expect { |b|
|
15
|
+
splitter.sub_each_field "a\tbc\t\txyz" do |*args|
|
16
|
+
b.to_proc.call(*args)
|
17
|
+
""
|
18
|
+
end
|
19
|
+
}.to yield_successive_args(
|
20
|
+
[ "a", 1 ],
|
21
|
+
[ "bc", 2 ],
|
22
|
+
[ "xyz", 3 ]
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter 'spec'
|
5
|
+
end
|
6
|
+
|
7
|
+
shared_examples 'humr_handler' do |name,handler_class|
|
8
|
+
it "is registered as :#{name}" do
|
9
|
+
expect(Humr::Handler[name]).to be(handler_class)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#name' do
|
13
|
+
it "is :#{name}" do
|
14
|
+
expect(subject.name).to be(name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'term/ansicolor'
|
20
|
+
|
21
|
+
class String
|
22
|
+
include Term::ANSIColor
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: humr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- motemen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: term-ansicolor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: useragent
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: terminal-notifier-guard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- motemen@gmail.com
|
142
|
+
executables:
|
143
|
+
- humr
|
144
|
+
extensions: []
|
145
|
+
extra_rdoc_files: []
|
146
|
+
files:
|
147
|
+
- .gitignore
|
148
|
+
- Gemfile
|
149
|
+
- Gemfile.lock
|
150
|
+
- Guardfile
|
151
|
+
- LICENSE
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- bin/humr
|
156
|
+
- humr.gemspec
|
157
|
+
- lib/humr.rb
|
158
|
+
- lib/humr/config.rb
|
159
|
+
- lib/humr/handler.rb
|
160
|
+
- lib/humr/handler/si_prefix.rb
|
161
|
+
- lib/humr/handler/time.rb
|
162
|
+
- lib/humr/handler/url_escaped.rb
|
163
|
+
- lib/humr/handler/user_agent.rb
|
164
|
+
- lib/humr/runner.rb
|
165
|
+
- lib/humr/splitter.rb
|
166
|
+
- lib/humr/splitter/default.rb
|
167
|
+
- lib/humr/splitter/ltsv.rb
|
168
|
+
- lib/humr/splitter/pattern.rb
|
169
|
+
- lib/humr/version.rb
|
170
|
+
- spec/humr_spec.rb
|
171
|
+
- spec/lib/humr/config_spec.rb
|
172
|
+
- spec/lib/humr/handler/si_prefix_spec.rb
|
173
|
+
- spec/lib/humr/handler/time_spec.rb
|
174
|
+
- spec/lib/humr/handler/url_escaped_spec.rb
|
175
|
+
- spec/lib/humr/handler/user_agent_spec.rb
|
176
|
+
- spec/lib/humr/runner_spec.rb
|
177
|
+
- spec/lib/humr/splitter/default_spec.rb
|
178
|
+
- spec/lib/humr/splitter/ltsv_spec.rb
|
179
|
+
- spec/lib/humr/splitter/pattern_spec.rb
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
homepage: ''
|
182
|
+
licenses:
|
183
|
+
- MIT
|
184
|
+
metadata: {}
|
185
|
+
post_install_message:
|
186
|
+
rdoc_options: []
|
187
|
+
require_paths:
|
188
|
+
- lib
|
189
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - '>='
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - '>='
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
199
|
+
requirements: []
|
200
|
+
rubyforge_project:
|
201
|
+
rubygems_version: 2.0.3
|
202
|
+
signing_key:
|
203
|
+
specification_version: 4
|
204
|
+
summary: A CLI tool that make input human-readable
|
205
|
+
test_files:
|
206
|
+
- spec/humr_spec.rb
|
207
|
+
- spec/lib/humr/config_spec.rb
|
208
|
+
- spec/lib/humr/handler/si_prefix_spec.rb
|
209
|
+
- spec/lib/humr/handler/time_spec.rb
|
210
|
+
- spec/lib/humr/handler/url_escaped_spec.rb
|
211
|
+
- spec/lib/humr/handler/user_agent_spec.rb
|
212
|
+
- spec/lib/humr/runner_spec.rb
|
213
|
+
- spec/lib/humr/splitter/default_spec.rb
|
214
|
+
- spec/lib/humr/splitter/ltsv_spec.rb
|
215
|
+
- spec/lib/humr/splitter/pattern_spec.rb
|
216
|
+
- spec/spec_helper.rb
|
217
|
+
has_rdoc:
|