forget_that 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4c089033e63cc13c348be8e0fc634dc9270d835b1427faffcfce82f744fce33
4
- data.tar.gz: ccf7e04335ecc2af7e89742d0e3046c1d2fa04f063f1a45d74de3dade8526d71
3
+ metadata.gz: a51c257b09c9d456322846f42227c152fbac8890217b62fa1404667398dc907d
4
+ data.tar.gz: 2f768756f8589783a0af8a51a06fc9d2c2e416277016274d046ddf0a604c01ae
5
5
  SHA512:
6
- metadata.gz: 84525558e0101333d1c38b59a79b48230043dafac018c729f4340e34f1f4b7063b2936b72f82d738abcc1db3e054cfbfadae35312a2e0d733b9505768272ef14
7
- data.tar.gz: 0c5e4546c6f65dcbc70d02a9179258e4d1cb0f200e8f4b54cb0787d6b2cddfb3d1b4e0d8c7d499de903d2b6c968f056001fded5c8599882d5b590b5894b07cba
6
+ metadata.gz: 18a2d9593ff168ce6454c613935cf4b47ae73212a8e4d8173b51c0c40098e03478b38e73640b69230cc37cf603cfda15c1cf90c397496c6b40b3f86d6ca195f4
7
+ data.tar.gz: cf142d7d6073491ca57e654420edd39b9e1049152340b34f955ac83e2d3f2bca98ca42715b5e69bd8cacbaca8633031475306d560dd084c09f00bbc55627c080
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .gem
data/.rubocop.yml CHANGED
@@ -1,10 +1,12 @@
1
1
  Style/Documentation:
2
2
  Enabled: false
3
+
3
4
  Style/MixinUsage:
4
5
  Exclude:
5
6
  - 'bin/setup'
6
7
  - 'bin/update'
7
- Metrics/LineLength:
8
+
9
+ Layout/LineLength:
8
10
  Max: 120
9
11
  IgnoredPatterns: ['(\A|\s)#']
10
12
 
@@ -74,10 +76,10 @@ Documentation:
74
76
  Layout/MultilineHashBraceLayout:
75
77
  EnforcedStyle: symmetrical
76
78
 
77
- Layout/AlignHash:
79
+ Layout/HashAlignment:
78
80
  Enabled: false
79
81
 
80
- Layout/AlignParameters:
82
+ Layout/ParameterAlignment:
81
83
  Enabled: false
82
84
 
83
85
  Metrics/BlockLength:
@@ -103,7 +105,7 @@ Lint/AmbiguousOperator:
103
105
 
104
106
  AllCops:
105
107
  DisplayCopNames: true
106
- TargetRubyVersion: 2.5.0
108
+ TargetRubyVersion: 2.7.0
107
109
  CacheRootDirectory: tmp/test-results
108
110
  Exclude:
109
111
  - db/**/*
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- forget_that (0.1.0)
4
+ forget_that (0.1.1)
5
5
  activerecord (>= 5)
6
6
  activesupport
7
7
  pg
@@ -34,13 +34,16 @@ GEM
34
34
  minitest (~> 5.1)
35
35
  tzinfo (~> 1.1)
36
36
  zeitwerk (~> 2.2)
37
+ ast (2.4.0)
37
38
  builder (3.2.4)
39
+ coderay (1.1.2)
38
40
  concurrent-ruby (1.1.5)
39
41
  crass (1.0.5)
40
42
  diff-lcs (1.3)
41
43
  erubi (1.9.0)
42
- i18n (1.7.0)
44
+ i18n (1.7.1)
43
45
  concurrent-ruby (~> 1.0)
46
+ jaro_winkler (1.5.4)
44
47
  loofah (2.4.0)
45
48
  crass (~> 1.0.2)
46
49
  nokogiri (>= 1.5.9)
@@ -49,7 +52,13 @@ GEM
49
52
  minitest (5.13.0)
50
53
  nokogiri (1.10.7)
51
54
  mini_portile2 (~> 2.4.0)
55
+ parallel (1.19.1)
56
+ parser (2.7.0.1)
57
+ ast (~> 2.4.0)
52
58
  pg (1.2.1)
59
+ pry (0.12.2)
60
+ coderay (~> 1.1.0)
61
+ method_source (~> 0.9.0)
53
62
  rack (2.0.8)
54
63
  rack-test (1.1.0)
55
64
  rack (>= 1.0, < 3)
@@ -64,6 +73,7 @@ GEM
64
73
  method_source
65
74
  rake (>= 0.8.7)
66
75
  thor (>= 0.20.3, < 2.0)
76
+ rainbow (3.0.0)
67
77
  rake (10.5.0)
68
78
  rspec (3.9.0)
69
79
  rspec-core (~> 3.9.0)
@@ -78,10 +88,20 @@ GEM
78
88
  diff-lcs (>= 1.2.0, < 2.0)
79
89
  rspec-support (~> 3.9.0)
80
90
  rspec-support (3.9.2)
91
+ rubocop (0.79.0)
92
+ jaro_winkler (~> 1.5.1)
93
+ parallel (~> 1.10)
94
+ parser (>= 2.7.0.1)
95
+ rainbow (>= 2.2.2, < 4.0)
96
+ ruby-progressbar (~> 1.7)
97
+ unicode-display_width (>= 1.4.0, < 1.7)
98
+ ruby-progressbar (1.10.1)
99
+ sqlite3 (1.4.1)
81
100
  thor (1.0.1)
82
101
  thread_safe (0.3.6)
83
102
  tzinfo (1.2.6)
84
103
  thread_safe (~> 0.1)
104
+ unicode-display_width (1.6.0)
85
105
  zeitwerk (2.2.2)
86
106
 
87
107
  PLATFORMS
@@ -90,8 +110,11 @@ PLATFORMS
90
110
  DEPENDENCIES
91
111
  bundler (~> 2.0)
92
112
  forget_that!
113
+ pry
93
114
  rake (~> 10.0)
94
115
  rspec (~> 3.0)
116
+ rubocop (~> 0.79.0)
117
+ sqlite3
95
118
 
96
119
  BUNDLED WITH
97
- 2.0.2
120
+ 2.1.2
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Vehiculum, Stanislav Pankov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -23,8 +23,8 @@ And then execute:
23
23
  Before gem could be used, a config in `config/anonymization_config.yml` in must be created:
24
24
 
25
25
  ```YAML
26
- config:
27
- retention_time:
26
+ config: # config part is only valid for the `call` method which will write in db
27
+ retention_time: # defines the newest record to be anonymized when `call` is used
28
28
  value: 90
29
29
  unit: 'days'
30
30
 
@@ -100,6 +100,20 @@ schema:
100
100
 
101
101
  Then gem can be invoked from the rake-task. It is your responsibility to ensure that it never runs on production.
102
102
 
103
+ ### Non-destructive use
104
+
105
+ Anonymizers might be used with collection `ActiveRecord::Relation` supplied, not affecting any database.
106
+
107
+ For example:
108
+
109
+ ```ruby
110
+ anonymizer = ForgetThat::Service.new
111
+ collection = Address.where(created_at: Time.current - 40.days)
112
+ anonymizer.sanitize_collection(collection)
113
+ ```
114
+
115
+ This will return an array of Hashes, corresponding to the records in `collection`. All fields configured to be anonymized, will be anonymized, ids will be stripped, the rest will be provided as is. This method ignores `retention_time`.
116
+
103
117
  ### Custom placeholders
104
118
 
105
119
  The default placeholders are `random_date`, `hex_string`, `random_phone`, `fake_personal_id_number`, `random_amount`. In some cases this might not be enough or behaviour might not be desireable. In that case you can supply `anonymizers` hash.
@@ -126,6 +140,7 @@ schema:
126
140
  name: 'Peter %{foobar}' #results in the "name" column of table "users" filled with "Peter FooBar"
127
141
  ```
128
142
 
143
+
129
144
  ## Development
130
145
 
131
146
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/forget_that.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.metadata['homepage_uri'] = spec.homepage
20
20
  spec.metadata['source_code_uri'] = 'https://github.com/vehiculum-berlin/forget_that'
21
- spec.metadata["changelog_uri"] = 'https://github.com/vehiculum-berlin/forget_that'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/vehiculum-berlin/forget_that'
22
22
 
23
23
  # Specify which files should be added to the gem when it is released.
24
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -32,6 +32,8 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'bundler', '~> 2.0'
33
33
  spec.add_development_dependency 'rake', '~> 10.0'
34
34
  spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_development_dependency 'rubocop', '~> 0.79.0'
36
+ spec.add_development_dependency 'sqlite3'
35
37
 
36
38
  spec.add_runtime_dependency 'activerecord', '>= 5'
37
39
  spec.add_runtime_dependency 'activesupport'
data/lib/forget_that.rb CHANGED
@@ -6,13 +6,14 @@ require 'forget_that/record'
6
6
 
7
7
  module ForgetThat
8
8
  class InvalidConfigError < StandardError; end
9
+ class InvalidCollectionError < StandardError; end
9
10
 
10
11
  class << self
11
12
  attr_writer :logger
12
13
 
13
14
  def logger
14
15
  @logger ||= Logger.new($stdout).tap do |log|
15
- log.progname = self.name
16
+ log.progname = name
16
17
  end
17
18
  end
18
19
  end
@@ -20,6 +20,29 @@ module ForgetThat
20
20
  end
21
21
  end
22
22
 
23
+ def sanitize_collection(collection)
24
+ raise InvalidConfigError unless valid_anonymizer_set?
25
+ raise InvalidCollectionError unless valid_records? collection
26
+
27
+ table = collection.klass.table_name
28
+ unsafe_columns = config[table].keys
29
+ safe_columns = (collection.klass.columns.map(&:name) - unsafe_columns).reject { |e| e == 'id' }
30
+ safe_data = collection.pluck(*safe_columns).map { |r| r.is_a?(Array) ? r : [r] }
31
+ safe_data.map do |record|
32
+ makeshift = {}
33
+ unsafe_columns.each do |column|
34
+ makeshift[column] = config[table][column]
35
+ end
36
+ makeshift = makeshift
37
+ .map { |key, value| [key, value.to_s % generate_anonymized_values] }
38
+ .to_h
39
+ safe_columns
40
+ .map.with_index { |value, index| [value, record[index]] }
41
+ .to_h
42
+ .merge(makeshift)
43
+ end
44
+ end
45
+
23
46
  def anonymizers
24
47
  {
25
48
  random_date: -> { Time.now - [*1..10**4].sample.days },
@@ -63,6 +86,12 @@ module ForgetThat
63
86
  end
64
87
  end
65
88
 
89
+ def valid_records?(collection)
90
+ return true if collection.is_a?(ActiveRecord::Relation)
91
+
92
+ false
93
+ end
94
+
66
95
  private
67
96
 
68
97
  def populate_records_hash(columns)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForgetThat
2
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forget_that
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stan Pankov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-08 00:00:00.000000000 Z
11
+ date: 2020-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.79.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.79.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
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'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: activerecord
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +150,7 @@ files:
122
150
  - ".travis.yml"
123
151
  - Gemfile
124
152
  - Gemfile.lock
153
+ - LICENSE.md
125
154
  - README.md
126
155
  - Rakefile
127
156
  - bin/console