rails-gdpr-export 0.1.1 → 1.0.0

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: 1019761a9443a0a21381bdf2276b0186c198c45d1225f4c751863fc6ac004622
4
- data.tar.gz: 1b5136e360723d7c113aea6699d740c828513e06a24f032ced92e39a64ca5a27
3
+ metadata.gz: 7071e26a9662f67d80d9c59a8ee5dcb3d896682b63094b8fd37c288f73ebb052
4
+ data.tar.gz: 0f10a671e075a4fc691b88a99d905434e93ce189b81adc90a7e89a59fac5c1c0
5
5
  SHA512:
6
- metadata.gz: 8943787b8e4ee6eb4e24fcf58d01ff8df090245a7e955c1ccb1f17af2bd3805999ed6de29b9eaf64fdd70a2176504ec1fd2e90910e0cfba687b1abf0b0da99c2
7
- data.tar.gz: 6b3818a619645b863ee8ee294ac41417620c8b2daa295dac0e7b22b7a5fedd7cc5479fd710f113478fd6848ba4b6fd2a0728c28fa934764fab03bb54102f1da5
6
+ metadata.gz: 96fa84ddc1af458eb2e6a545644159c5fae81667cbc032182b70ac6f8fc99d6071f996be7cf3a1f9ad67086e12254849e2c2eb8324d8a54ee15e631d01696cda
7
+ data.tar.gz: efb60c1bf1fc6e1503d856b5e57ef591d6c9f7e52a1f169a861395469ebcc53ec0c1b171cb0c0498bdc132a7536e5ffe22b90317f99abd271640db3550b15e06
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-gdpr-export (0.1.0)
4
+ rails-gdpr-export (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -20,17 +20,30 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- - Add code similar to the above in a initializer file (e.g. initializers/gdpr.rb) to specify the fields you want to retrieve:
23
+ This gem allows you to specify fields that you want to retrieve from your models and to export them in a csv format.
24
+
25
+ ### Initialization
26
+
27
+ To initialize the gem usage, loads the GdprExporter module into activerecord classes. Do this at initialization time through an initializer. E.g. create a initializers/gdpr.rb file and add the following:
24
28
 
25
29
  ```ruby
26
- require 'exts/gdpr'
30
+ ActiveRecord::Base.send :include, GdprExporter
31
+ ```
27
32
 
28
- # Loads the Gdpr module into activerecord classes.
29
- ActiveRecord::Base.send :include, Exts::Gdpr
33
+ ### Data collection
30
34
 
35
+ In order to specify the fields that you want to return to the user you need to call `ruby gdpr_collect`.
36
+ The call target is a rails model and its arguments are as follows:
37
+ * set of simple fields: i.e. fields that will be output as is
38
+ * a hash of params:
39
+ {user_id: <the field in the model used as alias for the user id>
40
+ renamed_fields: {<field_from_db> => <field_name_in_output>}
41
+ table_name: <the new table name in output>
42
+ description: <a comment>
43
+ join: <an association>}
31
44
 
32
- # Defines gdpr data collection throughout the model classes
33
- #
45
+
46
+ ```ruby
34
47
  User.gdpr_collect :email, :last_sign_in_at, :stripe_customer_id,
35
48
  :type, :forward_mailbox,
36
49
  {user_id: :id,
@@ -39,25 +52,13 @@ User.gdpr_collect :email, :last_sign_in_at, :stripe_customer_id,
39
52
  chosen_program_id: "chosen program",
40
53
  current_sign_in_ip: "current IP address",
41
54
  last_sign_in_ip: "previously used IP address"}}
42
-
43
-
44
- Subscription.gdpr_collect :stripe_id, :stripe_plan_id,
45
- :exmatriculated_at, :current_period_end, :status,
46
- :notified_at, :pause_reason, :pause_error,
47
- :start_at, :initial_start_at, :completed_at,
48
- :amount, :paused_at, :rules_accepted_at,
49
- {user_id: :user_id,
50
- join: :program,
51
- renamed_fields: {title: "program title"}}
52
55
  ```
53
56
 
54
- - call Exts::Gdpr.export(<user_id>) and it will return a csv formatted output
55
-
56
- ## Development
57
+ ```
57
58
 
58
- 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.
59
+ ### Data export
60
+ Call `ruby GdprExporter.export(<user_id>)` and it will return a csv formatted output.
59
61
 
60
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
62
 
62
63
  ## Contributing
63
64
 
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "gdpr_exporter/version"
4
+ require 'csv'
5
+
6
+ module GdprExporter
7
+ # Stores all the classes that have been tagged for gdpr collection
8
+ @@klasses = []
9
+
10
+ def self.get_klasses
11
+ @@klasses
12
+ end
13
+
14
+ def self.add_klass(klass)
15
+ @@klasses << klass
16
+ end
17
+
18
+ # Collects data through all the tagged models and generates a csv
19
+ # formatted output
20
+ def self.export(user_id)
21
+ CSV.generate do |csv|
22
+ get_klasses.each do |klass|
23
+ rows = klass.gdpr_query(user_id)
24
+ klass.gdpr_export(rows, csv)
25
+ end
26
+ end
27
+ end
28
+
29
+ # Instruments the classes implementing this module with instance and class
30
+ # methods.
31
+ def self.included base
32
+ base.send :include, InstanceMethods
33
+ base.extend ClassMethods
34
+ end
35
+
36
+ module InstanceMethods
37
+ end
38
+
39
+ module ClassMethods
40
+ # Declared in each model class with interest in collecting gdpr data.
41
+ # Instruments the singleton of those classes so that gdpr data can be
42
+ # collected and exported to csv.
43
+ #
44
+ # Arguments are:
45
+ # - set of simple fields: i.e. fields that will be output as is
46
+ # - a hash of params:
47
+ # {renamed_fields: {<field_from_db> => <field_name_in_output>}
48
+ # table_name: <the new table name in output>
49
+ # description: <a comment>
50
+ # join: <an association>}
51
+ def gdpr_collect(*args)
52
+ # Params handling
53
+ if args.class == Hash # when user provides the hash_params only
54
+ simple_fields, hash_params = [[], args]
55
+ else
56
+ simple_fields, hash_params = [args[0..-2], args.last]
57
+ end
58
+
59
+ unless hash_params.class == Hash
60
+ raise "Gdpr fields collection error: last argument must be a hash!"
61
+ end
62
+
63
+ unless hash_params.key?(:user_id)
64
+ raise "Gdpr fields collection error: the field aliasing user_id is not declared for '#{self}'!"
65
+ end
66
+
67
+ # Adds the eigen class to the set of classes eligible for gdpr data collection.
68
+ GdprExporter.add_klass(self)
69
+
70
+ # Adds instance fields to the eigenclass. They store
71
+ # all the fields and info we are interested in.
72
+ @gdpr_simple_fields = simple_fields
73
+ @gdpr_hash_params = hash_params
74
+ # Add readers for the instance vars declared above (for testing reasons)
75
+ self.class.send :attr_reader, :gdpr_simple_fields
76
+ self.class.send :attr_reader, :gdpr_hash_params
77
+
78
+ # Build the csv header and prepare the fields used for querying
79
+ user_id_field = hash_params[:user_id]
80
+ csv_headers = [:user_id].concat @gdpr_simple_fields
81
+ query_fields = [user_id_field].concat @gdpr_simple_fields
82
+
83
+ if hash_params[:renamed_fields]
84
+ csv_headers.concat hash_params[:renamed_fields].values
85
+ query_fields.concat hash_params[:renamed_fields].keys
86
+ end
87
+
88
+ # Adds the class method 'gdpr_query' to the eigenclass.
89
+ # It will execute the query.
90
+ self.define_singleton_method(:gdpr_query) do |_user_id|
91
+ if hash_params[:join]
92
+ self.select(query_fields).where(user_id_field => _user_id).
93
+ joins(hash_params[:join])
94
+ else
95
+ self.select(query_fields).where(user_id_field => _user_id)
96
+ end
97
+ end
98
+
99
+ # Adds a method to export to csv to the eigenclass.
100
+ self.define_singleton_method(:gdpr_export) do |rows, csv|
101
+ csv << (hash_params[:table_name] ? [hash_params[:table_name]] :
102
+ [self.to_s])
103
+ csv << csv_headers
104
+ rows.each do |r|
105
+ csv << query_fields.map{ |f| r.send(f) }
106
+ end
107
+ csv << ['Comment:', hash_params[:desc]] if hash_params[:desc]
108
+ csv << []
109
+ end
110
+ end
111
+ end
112
+ end
@@ -1,5 +1,5 @@
1
1
  module Exts
2
2
  module Gdpr
3
- VERSION = "0.1.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "exts/gdpr/version"
4
+ require "gdpr_exporter/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "rails-gdpr-export"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-gdpr-export
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chrislain Razafimahefa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-10 00:00:00.000000000 Z
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,8 +69,8 @@ files:
69
69
  - Rakefile
70
70
  - bin/console
71
71
  - bin/setup
72
- - lib/exts/gdpr.rb
73
- - lib/exts/gdpr/version.rb
72
+ - lib/gdpr_exporter.rb
73
+ - lib/gdpr_exporter/version.rb
74
74
  - rails-gdpr-export.gemspec
75
75
  homepage: https://github.com/epfl-exts/rails-gdpr-export
76
76
  licenses:
data/lib/exts/gdpr.rb DELETED
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "exts/gdpr/version"
4
- require 'csv'
5
-
6
- module Exts
7
- module Gdpr
8
- # Stores all the classes that have been tagged for gdpr collection
9
- @@klasses = []
10
-
11
- def self.get_klasses
12
- @@klasses
13
- end
14
-
15
- def self.add_klass(klass)
16
- @@klasses << klass
17
- end
18
-
19
- # Collects data through all the tagged models and generates a csv
20
- # formatted output
21
- def self.export(user_id)
22
- CSV.generate do |csv|
23
- get_klasses.each do |klass|
24
- rows = klass.gdpr_query(user_id)
25
- klass.gdpr_export(rows, csv)
26
- end
27
- end
28
- end
29
-
30
- # Instruments the classes implementing this module with instance and class
31
- # methods.
32
- def self.included base
33
- base.send :include, InstanceMethods
34
- base.extend ClassMethods
35
- end
36
-
37
- module InstanceMethods
38
- end
39
-
40
- module ClassMethods
41
- # Declared in each model class with interest in collecting gdpr data.
42
- # Instruments the singleton of those classes so that gdpr data can be
43
- # collected and exported to csv.
44
- #
45
- # Arguments are:
46
- # - set of simple fields: i.e. fields that will be output as is
47
- # - a hash of params:
48
- # {renamed_fields: {<field_from_db> => <field_name_in_output>}
49
- # table_name: <the new table name in output>
50
- # description: <a comment>
51
- # join: <an association>}
52
- def gdpr_collect(*args)
53
- # Params handling
54
- if args.class == Hash # when user provides the hash_params only
55
- simple_fields, hash_params = [[], args]
56
- else
57
- simple_fields, hash_params = [args[0..-2], args.last]
58
- end
59
-
60
- unless hash_params.class == Hash
61
- raise "Gdpr fields collection error: last argument must be a hash!"
62
- end
63
-
64
- unless hash_params.key?(:user_id)
65
- raise "Gdpr fields collection error: the field aliasing user_id is not declared for '#{self}'!"
66
- end
67
-
68
- # Adds the eigen class to the set of classes eligible for gdpr data collection.
69
- Gdpr.add_klass(self)
70
-
71
- # Adds instance fields to the eigenclass. They store
72
- # all the fields and info we are interested in.
73
- @gdpr_simple_fields = simple_fields
74
- @gdpr_hash_params = hash_params
75
- # Add readers for the instance vars declared above (for testing reasons)
76
- self.class.send :attr_reader, :gdpr_simple_fields
77
- self.class.send :attr_reader, :gdpr_hash_params
78
-
79
- # Build the csv header and prepare the fields used for querying
80
- user_id_field = hash_params[:user_id]
81
- csv_headers = [:user_id].concat @gdpr_simple_fields
82
- query_fields = [user_id_field].concat @gdpr_simple_fields
83
-
84
- if hash_params[:renamed_fields]
85
- csv_headers.concat hash_params[:renamed_fields].values
86
- query_fields.concat hash_params[:renamed_fields].keys
87
- end
88
-
89
- # Adds the class method 'gdpr_query' to the eigenclass.
90
- # It will execute the query.
91
- self.define_singleton_method(:gdpr_query) do |_user_id|
92
- if hash_params[:join]
93
- self.select(query_fields).where(user_id_field => _user_id).
94
- joins(hash_params[:join])
95
- else
96
- self.select(query_fields).where(user_id_field => _user_id)
97
- end
98
- end
99
-
100
- # Adds a method to export to csv to the eigenclass.
101
- self.define_singleton_method(:gdpr_export) do |rows, csv|
102
- csv << (hash_params[:table_name] ? [hash_params[:table_name]] :
103
- [self.to_s])
104
- csv << csv_headers
105
- rows.each do |r|
106
- csv << query_fields.map{ |f| r.send(f) }
107
- end
108
- csv << ['Comment:', hash_params[:desc]] if hash_params[:desc]
109
- csv << []
110
- end
111
- end
112
- end
113
- end
114
- end