onelinejson 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in onelinejson.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem "rspec"
8
+ gem "lograge", git: "git@github.com:i0rek/lograge.git", branch: "before_format"
9
+ end
data/Rakefile CHANGED
@@ -1 +1,28 @@
1
- require "bundler/gem_tasks"
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ require "rake"
5
+ require "rspec/core/rake_task"
6
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
7
+ require "onelinejson/version"
8
+
9
+ task :gem => :build
10
+ task :build do
11
+ system "gem build onelinejson.gemspec"
12
+ end
13
+
14
+ task :install => :build do
15
+ system "gem install #{FILE}"
16
+ end
17
+
18
+ task :release => :build do
19
+ system "git tag -a v#{Onelinejson::VERSION} -m 'Tagging #{Onelinejson::VERSION}'"
20
+ system "git push --tags"
21
+ system "gem push onelinejson-#{Onelinejson::VERSION}.gem"
22
+ end
23
+
24
+ RSpec::Core::RakeTask.new(:spec) do |t|
25
+ t.verbose = false
26
+ t.ruby_opts = "-W -I./spec -rspec_helper"
27
+ end
28
+ task :default => :spec
data/lib/onelinejson.rb CHANGED
@@ -20,22 +20,23 @@ module Onelinejson
20
20
  ]
21
21
  ELIP = "\xe2\x80\xa6"
22
22
  LOG_MAX_LENGTH = 1900
23
-
24
- def self.trim_values(hash, trim_to)
25
- Hash[hash.map do |k, v|
26
- if v.is_a? String
27
- trimmed = if v.size > trim_to
28
- v[0, trim_to] + ELIP
29
- else
30
- v
31
- end
32
- [k, trimmed]
33
- else
34
- [k, v]
35
- end
36
- end]
23
+ ENTRY_MAX_LENGTH = 128
24
+ BEFORE_HOOK = lambda do |data, payload|
25
+ request = data.select{ |k,_|
26
+ [:method, :path, :format].include?(k)
27
+ }.merge(payload[:request])
28
+ response = data.select{ |k,_|
29
+ [:status, :duration, :view, :view_runtime].include?(k)
30
+ }
31
+ Onelinejson.enforce_max_json_length(
32
+ {
33
+ debug_info: payload[:debug_info] || {},
34
+ request: request,
35
+ response: response,
36
+ })
37
37
  end
38
38
 
39
+
39
40
  def self.enforce_max_json_length(hash)
40
41
  return hash if JSON.dump(hash).size <= LOG_MAX_LENGTH
41
42
 
@@ -48,24 +49,50 @@ module Onelinejson
48
49
  end
49
50
 
50
51
  module AppControllerMethods
51
- def append_info_to_payload(payload)
52
- super
53
- headers = if request.headers.respond_to?(:env)
54
- request.headers.env
55
- elsif request.headers.respond_to?(:to_hash)
56
- request.headers.to_hash
52
+ extend self # for testing
53
+
54
+ def trim_values(hash)
55
+ Hash[hash.map do |k, v|
56
+ if v.is_a? String
57
+ trimmed = if v.size > ENTRY_MAX_LENGTH
58
+ v[0, ENTRY_MAX_LENGTH-1] + ELIP
59
+ else
60
+ v
61
+ end
62
+ [k, trimmed]
63
+ else
64
+ [k, v]
65
+ end
66
+ end]
67
+ end
68
+
69
+ def extract_headers(headers)
70
+ if headers.respond_to?(:env)
71
+ headers.env
72
+ elsif headers.respond_to?(:to_hash)
73
+ headers.to_hash
57
74
  end.select do |k, v|
58
75
  k =~ /^HTTP_/ && !REJECTED_HEADERS.any? {|regex| k =~ regex}
59
76
  end
60
- parameters = params.reject do |k,v|
77
+ end
78
+
79
+ def extract_params(params)
80
+ params.reject do |k,v|
61
81
  k == 'controller' ||
62
82
  k == 'action' ||
63
83
  v.is_a?(ActionDispatch::Http::UploadedFile) ||
64
84
  v.is_a?(Hash)
65
85
  end
86
+ end
87
+
88
+ def append_info_to_payload(payload)
89
+ super
66
90
 
91
+ parameters = extract_params(params)
92
+ parameters = trim_values(parameters)
93
+ headers = extract_headers(request.headers)
67
94
  payload[:request] = {
68
- params: Onelinejson.trim_values(parameters, 128),
95
+ params: parameters,
69
96
  headers: headers,
70
97
  ip: request.ip,
71
98
  uuid: request.env['action_dispatch.request_id'],
@@ -81,25 +108,12 @@ module Onelinejson
81
108
  end
82
109
 
83
110
  class Railtie < Rails::Railtie
111
+ puts 'FUUUU'
84
112
  config.log_tags = nil
85
113
  config.lograge = ActiveSupport::OrderedOptions.new
86
114
  config.lograge.formatter = ::Lograge::Formatters::Json.new
87
115
  config.lograge.enabled = true
88
- config.lograge.before_format = lambda do |data, payload|
89
- request = data.select{ |k,_|
90
- [:method, :path, :format].include?(k)
91
- }.merge(payload[:request])
92
- response = data.select{ |k,_|
93
- [:status, :duration, :view, :view_runtime].include?(k)
94
- }
95
- Onelinejson.enforce_max_json_length(
96
- {
97
- debug_info: payload[:debug_info] || {},
98
- request: request,
99
- response: response,
100
- })
101
- end
102
-
116
+ config.lograge.before_format = Onelinejson::BEFORE_HOOK
103
117
  ActiveSupport.on_load(:action_controller) do
104
118
  include AppControllerMethods
105
119
  end
@@ -1,3 +1,3 @@
1
1
  module Onelinejson
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
data/onelinejson.gemspec CHANGED
@@ -19,6 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "lograge"
22
- spec.add_development_dependency "bundler", "~> 1.4"
23
- spec.add_development_dependency "rake"
24
22
  end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Onelinejson::AppControllerMethods do
4
+ describe '.trim_values' do
5
+ let(:trimmed) { Onelinejson::AppControllerMethods.trim_values(hash) }
6
+ context 'when all good' do
7
+ let(:hash) { {a: 1, b: 2} }
8
+ it 'returns eq hash' do
9
+ expect(trimmed).to eq(hash)
10
+ end
11
+ end
12
+
13
+ context 'when entry too long' do
14
+ let(:hash) { {a: "aa"*Onelinejson::ENTRY_MAX_LENGTH} }
15
+ it 'trims entry' do
16
+ expect(trimmed[:a]).to have(Onelinejson::ENTRY_MAX_LENGTH).chars
17
+ end
18
+ end
19
+ end
20
+
21
+ describe '.extract_headers' do
22
+ context 'when rails 3' do
23
+ it 'extracts'
24
+ end
25
+
26
+ context 'when rails 4' do
27
+ it 'extracts'
28
+ end
29
+ end
30
+
31
+ describe '.extract_params' do
32
+ it 'returns sanetized params'
33
+ it 'rejects controller'
34
+ it 'rejects action'
35
+ context 'when value is a file' do
36
+ it 'rejects whenfiles'
37
+ end
38
+ context 'when value is a hash' do
39
+ it 'rejects'
40
+ end
41
+ end
42
+ end
43
+
44
+ describe Onelinejson do
45
+ describe Onelinejson::BEFORE_HOOK do
46
+ it 'works'
47
+ end
48
+
49
+ describe '.enforce_max_json_length' do
50
+ let(:enforced) { Onelinejson.enforce_max_json_length(hash) }
51
+ context 'when all good' do
52
+ let(:hash) { {a: 1, b: 2} }
53
+ it 'returns hash' do
54
+ expect(enforced).to be(hash)
55
+ end
56
+ end
57
+
58
+ context 'when hash too long' do
59
+ context 'when removing params fixes it' do
60
+ let(:hash) { {a: 1, request: {
61
+ headers: 'a', params: 'b'*Onelinejson::LOG_MAX_LENGTH
62
+ }} }
63
+ it 'returns hash without params' do
64
+ expect(enforced[:request].keys).to eq([:headers])
65
+ end
66
+ end
67
+
68
+ context 'when removing params and headers fixes it' do
69
+ let(:hash) { {a: 1, request: {
70
+ headers: 'a'*Onelinejson::LOG_MAX_LENGTH, params: 'b'*Onelinejson::LOG_MAX_LENGTH
71
+ }} }
72
+ it 'returns hash without params and headers' do
73
+ expect(enforced[:request].keys).to be_empty
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require "bundler"
5
+ Bundler.setup
6
+
7
+ module Rails
8
+ end
9
+
10
+ require "lograge"
11
+ require "onelinejson"
12
+ require "rspec"
13
+
14
+ # This file was generated by the `rspec --init` command. Conventionally, all
15
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
16
+ # Require this file using `require "spec_helper"` to ensure that it is only
17
+ # loaded once.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ config.treat_symbols_as_metadata_keys_with_true_values = true
22
+
23
+ config.order = 'random'
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onelinejson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-10 00:00:00.000000000 Z
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lograge
@@ -27,38 +27,6 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: bundler
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: '1.4'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: '1.4'
46
- - !ruby/object:Gem::Dependency
47
- name: rake
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
30
  description: Everything you need to log json oneliners
63
31
  email:
64
32
  - me@hans.io
@@ -67,6 +35,7 @@ extensions: []
67
35
  extra_rdoc_files: []
68
36
  files:
69
37
  - .gitignore
38
+ - .rspec
70
39
  - Gemfile
71
40
  - LICENSE.txt
72
41
  - README.md
@@ -74,6 +43,8 @@ files:
74
43
  - lib/onelinejson.rb
75
44
  - lib/onelinejson/version.rb
76
45
  - onelinejson.gemspec
46
+ - spec/lib/onelinejson_spec.rb
47
+ - spec/spec_helper.rb
77
48
  homepage: ''
78
49
  licenses:
79
50
  - MIT
@@ -87,16 +58,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
58
  - - ! '>='
88
59
  - !ruby/object:Gem::Version
89
60
  version: '0'
61
+ segments:
62
+ - 0
63
+ hash: 3268962913112188005
90
64
  required_rubygems_version: !ruby/object:Gem::Requirement
91
65
  none: false
92
66
  requirements:
93
67
  - - ! '>='
94
68
  - !ruby/object:Gem::Version
95
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 3268962913112188005
96
73
  requirements: []
97
74
  rubyforge_project:
98
75
  rubygems_version: 1.8.23
99
76
  signing_key:
100
77
  specification_version: 3
101
78
  summary: Everything you need to log json oneliners
102
- test_files: []
79
+ test_files:
80
+ - spec/lib/onelinejson_spec.rb
81
+ - spec/spec_helper.rb