ruby_yappl 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 548e9ea2f6becf27d913698c0c8dd271c7497910
4
- data.tar.gz: 70459291c60cd6ee0924fffd30b481904c54cc06
2
+ SHA256:
3
+ metadata.gz: 303f8d9e0482d59481537014c99ddb6d7c47156ce30979f0609814150ea16c56
4
+ data.tar.gz: '00498f3f6264ccf674e5b87914269cbd37e1bbf602ae8665dfc35b44e6166b29'
5
5
  SHA512:
6
- metadata.gz: 613317c795f74ff0f3b2ab80761b6e11a2e2c2f6bd92755cb5bc7a548aa92f91955942bba9b3ee97f39fd406132a8dc6696fcbda756e1c3facd3da138b153016
7
- data.tar.gz: bada8ddaa7785d2548e434cb8576b7e00c441f6d7184167a639cd3289a49b139cb56e75a9342ecbae017aa4443c0c95dceb0881445368410c2366be4a2c46199
6
+ metadata.gz: d88a43fe6c88cb1fa49194ff860add062e930d6892544351b8a67a6225b689de8708cd1ec3b0af33095f700be6f50a493f03077d12b1b123ce366f82753a7cbf
7
+ data.tar.gz: fe97b78e2e52c3fbcd37fcdf110e533f04441a453100c16f55507da4d04925b3cd030929a9d9e1e776dcdf571075ba60aa13e5c687435cf53d7b2e14492fb32c
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.1
4
+
5
+ script:
6
+ - bundle exec rspec
7
+
8
+ notifications:
9
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test, :development do
4
+ gem 'rspec'
5
+ end
6
+
7
+ gem 'json-schema'
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.5.2)
5
+ public_suffix (>= 2.0.2, < 4.0)
6
+ diff-lcs (1.3)
7
+ json-schema (2.8.0)
8
+ addressable (>= 2.4)
9
+ public_suffix (3.0.2)
10
+ rspec (3.7.0)
11
+ rspec-core (~> 3.7.0)
12
+ rspec-expectations (~> 3.7.0)
13
+ rspec-mocks (~> 3.7.0)
14
+ rspec-core (3.7.1)
15
+ rspec-support (~> 3.7.0)
16
+ rspec-expectations (3.7.0)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.7.0)
19
+ rspec-mocks (3.7.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-support (3.7.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ json-schema
29
+ rspec
30
+
31
+ BUNDLED WITH
32
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2018, 2019 Thomas Peikert
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Ruby Library for YaPPL
2
+
3
+ [![Build Status](https://travis-ci.com/TPei/ruby_yappl.svg?branch=master)](https://travis-ci.com/TPei/ruby_yappl)
4
+ [![Gem Version](https://badge.fury.io/rb/ruby_yappl.svg)](https://rubygems.org/gems/ruby_yappl)
5
+ [![Release](https://img.shields.io/github/release/tpei/ruby_yappl.svg)](https://github.com/TPei/ruby_yappl/releases)
6
+
7
+ ## What is YaPPL?
8
+
9
+ YaPPL is a Privacy Preference Language (see [YaPPL](https://github.com/maroulb/YaPPL))
10
+
11
+ > YaPPL allows to codify legally sufficient consent and thus provides a valuable basis for GDPR-compliant consent management. In a nutshell, YaPPL is a message and file format that, in combination with the proposed service architecture, a) fulfills legal requirements for technically mediated consent provision, b) can act as an archive for expired preferences for auditing purposes, c) provides an enhanced user-centric access control model for future or unforeseen data processing requests. - Ulbricht & Pallas, 2018
12
+
13
+
14
+ ## What is this?
15
+
16
+ This is a Ruby Gem to work with YaPPL policies :)
17
+
18
+ ## Usage
19
+
20
+ First, you need to add this to your Gemfile: `gem 'ruby_yappl'` and then
21
+ run `bundle install`, now you have it installed. `require 'ruby_yappl'` to require the files.
22
+
23
+ The gem provides different classes to use, mainly `YaPPL::Policy` and `YaPPL::Rule`.
24
+
25
+ A policy can be created by parsing a policy file. Simply pass the json to `YaPPL::Policy.from_policy_file`.
26
+ This will instantiate a `YaPPL::Policy` and the appropriate `YaPPL::Rule` objects.
27
+
28
+ `Policy` provides a few public methods in accordance with the official
29
+ [YaPPL](https://github.com/maroulb/YaPPL) definition:
30
+
31
+ * `#create_policy` serializes the classes to their json representation to
32
+ be saved to database or file.
33
+
34
+ * `#get_excluded_purpose` returns an array of all excluded purposes
35
+
36
+ * `#get_excluded_utilizer` returns an array of all excluded utilizers
37
+
38
+ * `#new_rule(args)` adds a new rule to the Policy. `#add_rule(rule)` can
39
+ also be used if you prefer handling objects instead of parameters.
40
+
41
+ * `#get_tr_rules` returns an array of Transformation rules, including
42
+ the utilizers and purposes they depend on.
43
+
44
+ * `#archive_rule(rule_id)` expires and archives a rule.
45
+
46
+ * `#update_rule(rule_id, args)` updates a rule, archiving its old state.
47
+
48
+ Additionally, `YaPPL::Rule#expired?` can be used to see if a rule still applies.
49
+
50
+ ### Demo
51
+ ```ruby
52
+ require 'ruby_yappl'
53
+
54
+ # deserialize
55
+ policy = YaPPL::Policy.from_json_file(File.read('./my_policy_file.json'))
56
+
57
+ # update a rule
58
+ policy.update_rule(4, {
59
+ permitted_purposes: ['data_analysis'],
60
+ excluded_purposes: ['marketing'],
61
+ permitted_utilizers: ['market_research'],
62
+ excluded_utilizers: ['shopping', 'social_networks'],
63
+ transformations: [{
64
+ attribute: 'temperature',
65
+ tr_func: 'minmax_hourly'
66
+ }]
67
+ })
68
+
69
+ # archive a rule
70
+ policy.archive_rule(5)
71
+
72
+ # reserialize
73
+ File.new('./my_policy.json', 'w').write(policy.create_policy)
74
+
75
+ ```
data/lib/exceptions.rb ADDED
@@ -0,0 +1 @@
1
+ class SchemaError < StandardError; end
@@ -0,0 +1,11 @@
1
+ require 'json-schema'
2
+
3
+ class JsonValidator
4
+ def self.validate(schema_name, data)
5
+ name = "schemas/#{schema_name}.json"
6
+ schema = File.expand_path(name, __FILE__)
7
+ JSON::Validator.validate(schema, data)
8
+ rescue JSON::Schema::JsonParseError, JSON::Schema::UriError
9
+ false
10
+ end
11
+ end
data/lib/ruby_yappl.rb CHANGED
@@ -1,8 +1,9 @@
1
- require './lib/parser'
2
- require './lib/policy'
3
- require './lib/rule'
4
- require './lib/exceptions'
5
- require './lib/json_validator'
1
+ require 'parser'
2
+ require 'policy'
3
+ require 'rule'
4
+ require 'exceptions'
5
+ require 'json_validator'
6
6
 
7
7
  module YaPPL
8
+ VERSION = "0.1.3"
8
9
  end
@@ -0,0 +1,100 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "definitions": {},
4
+ "type": "object",
5
+ "properties": {
6
+ "_id": {"type": "number"},
7
+ "preference": {
8
+ "type": "array",
9
+ "items": {
10
+ "type": "object",
11
+ "properties": {
12
+ "rule": {
13
+ "type": "object",
14
+ "properties": {
15
+ "purpose": {
16
+ "type": "object",
17
+ "properties": {
18
+ "permitted": {
19
+ "type": "array",
20
+ "items": {"type": "string"},
21
+ "uniqueItems": true,
22
+ "default": []
23
+ },
24
+ "excluded": {
25
+ "type": "array",
26
+ "items": {"type": "string"},
27
+ "uniqueItems": true,
28
+ "default": []
29
+ }
30
+ },
31
+ "required": ["permitted", "excluded"],
32
+ "minProperties": 2,
33
+ "maxProperties": 2,
34
+ "additionalProperties": false
35
+ },
36
+ "utilizer": {
37
+ "type": "object",
38
+ "properties": {
39
+ "permitted": {
40
+ "type": "array",
41
+ "items": {"type": "string"},
42
+ "uniqueItems": true,
43
+ "default": []
44
+ },
45
+ "excluded": {
46
+ "type": "array",
47
+ "items": {"type": "string"},
48
+ "uniqueItems": true,
49
+ "default": []
50
+ }
51
+ },
52
+ "required": ["permitted", "excluded"],
53
+ "minProperties": 2,
54
+ "maxProperties": 2,
55
+ "additionalProperties": false
56
+ },
57
+ "transformation": {
58
+ "type": "array",
59
+ "items": {
60
+ "type": "object",
61
+ "properties": {
62
+ "attribute": {"type": "string"},
63
+ "tr_func": {
64
+ "type": "string",
65
+ "default": ""
66
+ }
67
+ },
68
+ "dependencies": {
69
+ "attribute": ["tr_func"],
70
+ "tr_func": ["attribute"]
71
+ },
72
+ "minProperties": 2,
73
+ "maxProperties": 2,
74
+ "additionalProperties": false
75
+ },
76
+ "uniqueItems": true,
77
+ "default": []
78
+ },
79
+ "valid_from": {
80
+ "type": "string",
81
+ "format": "date-time"
82
+ },
83
+ "exp_date": {
84
+ "type": "string",
85
+ "format": "date-time",
86
+ "default": "0000-00-00T00:00:00.00Z"
87
+ }
88
+ },
89
+ "required": ["purpose", "utilizer", "transformation", "valid_from", "exp_date"],
90
+ "minProperties": 5,
91
+ "maxProperties": 5,
92
+ "additionalProperties": false
93
+ }
94
+ }
95
+ },
96
+ "uniqueItems": true,
97
+ "minItems": 1
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'ruby_yappl'
3
+ s.version = '0.1.3'
4
+ s.date = '2019-01-21'
5
+ s.summary = 'ruby gem for the yappl privacy preference language'
6
+ s.authors = ['Thomas Peikert']
7
+ s.email = 't.s.peikert+yappl@gmail.com'
8
+ s.homepage = 'https://github.com/TPei/ruby_yappl'
9
+ s.license = 'MIT'
10
+ s.files = `git ls-files`.split($/)
11
+ s.require_paths = ['lib']
12
+ s.add_dependency('json-schema', '~> 2.8')
13
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'json_validator'
3
+
4
+ RSpec.describe JsonValidator do
5
+ describe '.validate' do
6
+ it 'returns true for valid schema + file' do
7
+ json = File.read('./spec/schemas/valid_schema.json')
8
+ expect(JsonValidator.validate('yappl', json)).to eq true
9
+ end
10
+
11
+ it 'returns false for invalid schema' do
12
+ json = File.read('./spec/schemas/invalid_schema.json')
13
+ expect(JsonValidator.validate('yappl', json)).to eq false
14
+ end
15
+
16
+ it 'returns false for broken json' do
17
+ json = File.read('./spec/schemas/broken_schema.json')
18
+ expect(JsonValidator.validate('yappl', json)).to eq false
19
+ end
20
+
21
+ it 'fails with non-existent schema file' do
22
+ expect { JsonValidator.validate('bla', {}) }.
23
+ to raise_error(JSON::Schema::ReadFailed)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'parser'
3
+
4
+ RSpec.describe YaPPL::Parser do
5
+ describe '.parse' do
6
+ context 'with valid schema' do
7
+ it 'returns a policy' do
8
+ policy = YaPPL::Parser.parse(File.read('./spec/schemas/valid_schema.json'))
9
+ expect(policy.class).to eq YaPPL::Policy
10
+ end
11
+
12
+ it 'creates a proper Policy from json' do
13
+ policy = YaPPL::Parser.parse(File.read('./spec/schemas/valid_schema.json'))
14
+ expect(policy.id).to eq 4493
15
+ expect(policy.rules.count).to eq 1
16
+ end
17
+
18
+ it 'creates proper rules' do
19
+ policy = YaPPL::Parser.parse(File.read('./spec/schemas/valid_schema.json'))
20
+ expect(policy.rules.count).to eq 1
21
+ rule = policy.rules[0]
22
+ expect(rule.class).to eq YaPPL::Rule
23
+ expect(rule.permitted_purposes). to eq ['statistics', 'planology']
24
+ expect(rule.excluded_purposes).to eq ['commercial']
25
+ expect(rule.permitted_utilizers).to eq ['wikimedia', 'tu berlin']
26
+ expect(rule.excluded_utilizers).to eq ['netatmo', 'gate5']
27
+ expect(rule.transformations.count).to eq 1
28
+ expect(rule.transformations[0]['attribute']). to eq 'temperature'
29
+ expect(rule.transformations[0]['tr_func']). to eq 'minmax_hourly'
30
+ expect(rule.valid_from).to eq Time.parse('2017-10-09T00:00:00.000Z')
31
+ expect(rule.expiration_date).to eq Time.parse('0000-01-01T00:00:00.000Z')
32
+ end
33
+ end
34
+
35
+ context 'with invalid schema' do
36
+ it 'raises a SchemaError' do
37
+ expect do
38
+ YaPPL::Parser.parse(File.read('./spec/schemas/invalid_schema.json'))
39
+ end.to raise_error SchemaError
40
+ end
41
+ end
42
+
43
+ context 'with broken schema' do
44
+ it 'raises a SchemaError' do
45
+ expect do
46
+ YaPPL::Parser.parse(File.read('./spec/schemas/broken_schema.json'))
47
+ end.to raise_error SchemaError
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,242 @@
1
+ require 'spec_helper'
2
+ require 'policy'
3
+ require 'rule'
4
+ require 'json_validator'
5
+
6
+ RSpec.describe YaPPL::Policy do
7
+ describe '.from_policy_file' do
8
+ it 'uses the parser' do
9
+ json = File.read('./spec/schemas/valid_schema.json')
10
+ expect(YaPPL::Parser).to receive(:parse).with(json)
11
+ YaPPL::Policy.from_policy_file(json)
12
+ end
13
+
14
+ it 'returns a Policy' do
15
+ json = File.read('./spec/schemas/valid_schema.json')
16
+ policy = YaPPL::Policy.from_policy_file(json)
17
+ expect(policy.class).to eq YaPPL::Policy
18
+ end
19
+ end
20
+
21
+ describe 'parse and serialize' do
22
+ it 'creates the same file again' do
23
+ json = File.read('./spec/schemas/valid_schema.json')
24
+ policy = YaPPL::Policy.from_policy_file(json)
25
+ expect(policy.create_policy).to eq JSON.parse(json).to_json
26
+ end
27
+ end
28
+
29
+ describe '#archive_rule' do
30
+ it 'calls #archive! on appropriate rule' do
31
+ rule_number_one = YaPPL::Rule.new(id: 4)
32
+ rules = [
33
+ YaPPL::Rule.new(id: 7),
34
+ rule_number_one,
35
+ YaPPL::Rule.new(id: 2)
36
+ ]
37
+
38
+ policy = YaPPL::Policy.new(1, rules)
39
+
40
+ expect(rule_number_one).to receive(:archive!)
41
+ policy.archive_rule(4)
42
+ end
43
+ end
44
+
45
+ describe '#create_policy' do
46
+ it 'calls #to_json' do
47
+ policy = YaPPL::Policy.new(1, [])
48
+ expect(policy).to receive(:to_json)
49
+ policy.create_policy
50
+ end
51
+ end
52
+
53
+ describe '#to_json' do
54
+ before do
55
+ @rule = YaPPL::Rule.new(
56
+ id: 1,
57
+ permitted_purposes: ['test1', 'test2'],
58
+ excluded_purposes: ['test3'],
59
+ permitted_utilizers: ['util1'],
60
+ excluded_utilizers: ['util2', 'util3'],
61
+ transformations: [],
62
+ valid_from: Time.now,
63
+ expiration_date: Time.now
64
+ )
65
+
66
+ @policy = YaPPL::Policy.new(1, [@rule])
67
+ end
68
+
69
+ it 'serializes appropriately' do
70
+ expect(@policy.to_json).to eq ({
71
+ id: 1,
72
+ preference: [{ rule: @rule.to_h }]
73
+ }.to_json)
74
+ end
75
+
76
+ it 'generated valid schema json' do
77
+ serialized = @policy.to_json
78
+ puts serialized
79
+ expect(JsonValidator.validate('yappl', serialized)).to eq true
80
+ end
81
+ end
82
+
83
+ describe '#new_rule' do
84
+ it 'creates a new rule and calls #add_rule' do
85
+ policy = YaPPL::Policy.new(1, [])
86
+ time = Time.now
87
+ args = { excluded_purposes: ['test1'], valid_from: time }
88
+ expect(YaPPL::Rule).to receive(:new).with(args).and_return(rule = double)
89
+ expect(policy).to receive(:add_rule).with rule
90
+ policy.new_rule(args)
91
+ end
92
+ end
93
+
94
+ describe '#add_rule' do
95
+ it 'appends a rule to the rules array and sets the id appropriately' do
96
+ rule_one = YaPPL::Rule.new(id: 1)
97
+ rule_three = YaPPL::Rule.new(id: 3)
98
+ policy = YaPPL::Policy.new(1, [rule_one, rule_three])
99
+ rule = YaPPL::Rule.new
100
+ policy.add_rule(rule)
101
+ expect(policy.rules.count).to eq 3
102
+ expect(policy.rules[-1].id).to eq 4
103
+ end
104
+ end
105
+
106
+ describe '#get_excluded_purpose' do
107
+ it 'returns a unique list of excluded purposes of all rules' do
108
+ rule1 = YaPPL::Rule.new(excluded_purposes: ['p1', 'p2'])
109
+ rule2 = YaPPL::Rule.new(excluded_purposes: ['p1', 'p3'])
110
+ policy = YaPPL::Policy.new(1, [rule1, rule2])
111
+ expect(policy.get_excluded_purpose).to eq ['p1', 'p2', 'p3']
112
+ end
113
+ end
114
+
115
+ describe '#get_excluded_utilizer' do
116
+ it 'returns a unique list of excluded utilizers of all rules' do
117
+ rule1 = YaPPL::Rule.new(excluded_utilizers: ['u1', 'u2'])
118
+ rule2 = YaPPL::Rule.new(excluded_utilizers: ['u1', 'u3'])
119
+ policy = YaPPL::Policy.new(1, [rule1, rule2])
120
+ expect(policy.get_excluded_utilizer).to eq ['u1', 'u2', 'u3']
121
+ end
122
+ end
123
+
124
+ describe '#update_rule' do
125
+ it 'updates a rule with new args and archives the old one' do
126
+ old_rule = YaPPL::Rule.new(id: 5, excluded_purposes: ['p1'])
127
+ policy = YaPPL::Policy.new(1, [old_rule])
128
+ args = {
129
+ excluded_purposes: ['p2'],
130
+ excluded_utilizers: ['u1']
131
+ }
132
+ policy.update_rule(5, args)
133
+ expect(policy.rules.count).to eq 2
134
+ new_rule = policy.rule_by_id(5)
135
+ expect(new_rule.id).to eq 5
136
+ expect(new_rule.excluded_purposes).to eq ['p2']
137
+ expect(new_rule.excluded_utilizers).to eq ['u1']
138
+ old_rule = policy.rule_by_id(-1)
139
+ expect(old_rule.id).to eq -1
140
+ expect(old_rule.excluded_purposes).to eq ['p1']
141
+ expect(old_rule.excluded_utilizers).to eq nil
142
+ end
143
+ end
144
+
145
+ describe '#rule_by_id' do
146
+ context 'with valid id' do
147
+ it 'returns matching rule' do
148
+ policy = YaPPL::Policy.new(1, [
149
+ YaPPL::Rule.new(id: 1), YaPPL::Rule.new(id: 4), YaPPL::Rule.new(id: 0)
150
+ ])
151
+ expect(policy.rule_by_id(4).id).to eq 4
152
+ end
153
+ end
154
+
155
+ context 'with id = -1' do
156
+ it 'returns all archived rules' do
157
+ policy = YaPPL::Policy.new(1, [
158
+ YaPPL::Rule.new(id: 1), YaPPL::Rule.new(id: 4), YaPPL::Rule.new(id: 0)
159
+ ])
160
+ policy.archive_rule(4)
161
+ policy.archive_rule(0)
162
+ expect(policy.rule_by_id(-1).count).to eq 2
163
+ end
164
+ end
165
+ end
166
+
167
+ describe '#archived_rules' do
168
+ context 'with archived rules' do
169
+ it 'returns all archived rules' do
170
+ rules = [YaPPL::Rule.new(id: -1), YaPPL::Rule.new(id: -1)]
171
+ policy = YaPPL::Policy.new(1, rules)
172
+ expect(policy.archived_rules).to eq(rules)
173
+ end
174
+ end
175
+
176
+ context 'with no archived rules' do
177
+ it 'returns an empty array' do
178
+ policy = YaPPL::Policy.new(1, [])
179
+ expect(policy.archived_rules).to eq []
180
+ end
181
+ end
182
+ end
183
+
184
+ describe '#active_rules' do
185
+ it 'returns all not expired and not archived rules' do
186
+ active_rules = [YaPPL::Rule.new, YaPPL::Rule.new]
187
+ inactive_rules = [YaPPL::Rule.new(id: -1), YaPPL::Rule.new(expiration_date: Time.now - 1000)]
188
+ policy = YaPPL::Policy.new(1, active_rules + inactive_rules)
189
+ expect(policy.active_rules).to eq active_rules
190
+ end
191
+ end
192
+
193
+ describe '#get_tr_rules' do
194
+ it 'returns active rules with permitted purp/util and transformations' do
195
+ active_rules = [
196
+ YaPPL::Rule.new(
197
+ id: 1,
198
+ permitted_purposes: ['p1'],
199
+ permitted_utilizers: ['u1'],
200
+ transformations: [{
201
+ attribute: 'temperature',
202
+ tr_func: 'minmax_hourly'
203
+ }],
204
+ excluded_utilizers: ['u2']
205
+ ),
206
+ YaPPL::Rule.new(
207
+ id: 2,
208
+ permitted_purposes: ['p2'],
209
+ permitted_utilizers: ['u3'],
210
+ transformations: [{
211
+ attribute: 'step_count',
212
+ tr_func: 'minmax_hourly'
213
+ }],
214
+ excluded_utilizers: ['u4'],
215
+ excluded_purposes: ['p2'],
216
+ valid_from: Time.now,
217
+ expiration_date: Time.new(0, 1, 1)
218
+ )
219
+ ]
220
+ inactive_rules = [YaPPL::Rule.new(id: -1), YaPPL::Rule.new(expiration_date: Time.now - 1000)]
221
+ policy = YaPPL::Policy.new(1, active_rules + inactive_rules)
222
+ expect(policy.get_tr_rules).to eq ([
223
+ {
224
+ permitted_purposes: ['p1'],
225
+ permitted_utilizers: ['u1'],
226
+ transformations: [{
227
+ attribute: 'temperature',
228
+ tr_func: 'minmax_hourly'
229
+ }]
230
+ },
231
+ {
232
+ permitted_purposes: ['p2'],
233
+ permitted_utilizers: ['u3'],
234
+ transformations: [{
235
+ attribute: 'step_count',
236
+ tr_func: 'minmax_hourly'
237
+ }]
238
+ }
239
+ ])
240
+ end
241
+ end
242
+ end
data/spec/rule_spec.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+ require 'rule'
3
+
4
+ RSpec.describe YaPPL::Rule do
5
+ describe '#initialize' do
6
+ it 'sets all given attributes' do
7
+ time = Time.now
8
+
9
+ args = {
10
+ id: 1, permitted_purposes: ['p1'], excluded_purposes: ['p2'],
11
+ permitted_utilizers: ['u1'], excluded_utilizers: ['u2'],
12
+ transformations: [{
13
+ attribute: 'temperature',
14
+ tr_func: 'minmax_hourly'
15
+ }],
16
+ valid_from: time,
17
+ expiration_date: time
18
+ }
19
+
20
+ rule = YaPPL::Rule.new(args)
21
+ expect(rule.id).to eq 1
22
+ expect(rule.permitted_purposes).to eq ['p1']
23
+ expect(rule.excluded_purposes).to eq ['p2']
24
+ expect(rule.permitted_utilizers).to eq ['u1']
25
+ expect(rule.excluded_utilizers).to eq ['u2']
26
+ expect(rule.transformations).to eq [{
27
+ attribute: 'temperature',
28
+ tr_func: 'minmax_hourly'
29
+ }]
30
+ expect(rule.valid_from).to eq time
31
+ expect(rule.expiration_date).to eq time
32
+ end
33
+
34
+ context 'with no valid_from and expiration_date provided' do
35
+ it 'sets defaults values' do
36
+ rule = YaPPL::Rule.new
37
+ expect(rule.valid_from).not_to eq nil
38
+ expect(rule.expiration_date).to eq Time.new(0, 1, 1)
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#archive!' do
44
+ it 'sets expiration_date to now and id to -1' do
45
+ set_time = Time.now - 100_000_000
46
+ rule = YaPPL::Rule.new(id: 100, expiration_date: set_time)
47
+
48
+ rule.archive!
49
+
50
+ expect(rule.id).to eq -1
51
+ expect(rule.expiration_date).to be > set_time
52
+ end
53
+ end
54
+
55
+ describe '#to_h' do
56
+ it 'serializes appropriately' do
57
+ time = Time.now
58
+
59
+ rule = YaPPL::Rule.new(
60
+ id: 1,
61
+ permitted_purposes: ['test1', 'test2'],
62
+ excluded_purposes: ['test3'],
63
+ permitted_utilizers: ['util1'],
64
+ excluded_utilizers: ['util2', 'util3'],
65
+ transformations: [{
66
+ attribute: 'temperature',
67
+ tr_func: 'minmax_hourly'
68
+ }],
69
+ valid_from: time,
70
+ expiration_date: time
71
+ )
72
+
73
+ expect(rule.to_h).to eq({
74
+ purpose: {
75
+ permitted: ['test1', 'test2'],
76
+ excluded: ['test3'],
77
+ },
78
+ utilizer: {
79
+ permitted: ['util1'],
80
+ excluded: ['util2', 'util3'],
81
+ },
82
+ transformation: [{
83
+ attribute: 'temperature',
84
+ tr_func: 'minmax_hourly'
85
+ }],
86
+ valid_from: time.strftime('%FT%T.%LZ'),
87
+ exp_date: time.strftime('%FT%T.%LZ')
88
+ })
89
+ end
90
+ end
91
+
92
+ describe '#expired?' do
93
+ context 'with expiration_date set to default' do
94
+ it 'returns false' do
95
+ rule = YaPPL::Rule.new
96
+ expect(rule.expired?).to eq false
97
+ end
98
+ end
99
+
100
+ context 'with expiration_date set in past' do
101
+ it 'returns true' do
102
+ rule = YaPPL::Rule.new(expiration_date: Time.new(2000, 1, 1))
103
+ expect(rule.expired?).to eq true
104
+ end
105
+ end
106
+
107
+ context 'with expiration_date set in future' do
108
+ it 'returns false' do
109
+ # have fun with this in a thousand years :D
110
+ rule = YaPPL::Rule.new(expiration_date: Time.new(3000, 1, 1))
111
+ expect(rule.expired?).to eq false
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,6 @@
1
+ {
2
+ "this": "is not valid json",
3
+ 1234,
4
+ "what is this?": ??,
5
+ [
6
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "id": 4493,
3
+ "preference": [{
4
+ "rule": {
5
+ "utilizer": {
6
+ "permitted": ["wikimedia", "tu berlin"],
7
+ "excluded": ["netatmo", "gate5"]
8
+ },
9
+ "transformation": [{
10
+ "attribute": "temperature",
11
+ "tr_func": "minmax_hourly"
12
+ }],
13
+ "valid_from": "2017-10-09T00:00:00.00Z",
14
+ "exp_date": "0000-01-01T00:00:00.000Z"
15
+ }
16
+ }
17
+ ]
18
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "id": 4493,
3
+ "preference": [{
4
+ "rule": {
5
+ "purpose": {
6
+ "permitted": ["statistics", "planology"],
7
+ "excluded": ["commercial"]
8
+ },
9
+ "utilizer": {
10
+ "permitted": ["wikimedia", "tu berlin"],
11
+ "excluded": ["netatmo", "gate5"]
12
+ },
13
+ "transformation": [{
14
+ "attribute": "temperature",
15
+ "tr_func": "minmax_hourly"
16
+ }],
17
+ "valid_from": "2017-10-09T00:00:00.000Z",
18
+ "exp_date": "0000-01-01T00:00:00.000Z"
19
+ }
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,102 @@
1
+ require './lib/ruby_yappl'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
43
+ # have no way to turn it off -- the option exists only for backwards
44
+ # compatibility in RSpec 3). It causes shared context metadata to be
45
+ # inherited by the metadata hash of host groups and examples, rather than
46
+ # triggering implicit auto-inclusion in groups with matching metadata.
47
+ config.shared_context_metadata_behavior = :apply_to_host_groups
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+ =begin
52
+ # This allows you to limit a spec run to individual examples or groups
53
+ # you care about by tagging them with `:focus` metadata. When nothing
54
+ # is tagged with `:focus`, all examples get run. RSpec also provides
55
+ # aliases for `it`, `describe`, and `context` that include `:focus`
56
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
57
+ config.filter_run_when_matching :focus
58
+
59
+ # Allows RSpec to persist some state between runs in order to support
60
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
61
+ # you configure your source control system to ignore this file.
62
+ config.example_status_persistence_file_path = "spec/examples.txt"
63
+
64
+ # Limits the available syntax to the non-monkey patched syntax that is
65
+ # recommended. For more details, see:
66
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
67
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
68
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
69
+ config.disable_monkey_patching!
70
+
71
+ # This setting enables warnings. It's recommended, but in some cases may
72
+ # be too noisy due to issues in dependencies.
73
+ config.warnings = true
74
+
75
+ # Many RSpec users commonly either run the entire suite or an individual
76
+ # file, and it's useful to allow more verbose output when running an
77
+ # individual spec file.
78
+ if config.files_to_run.one?
79
+ # Use the documentation formatter for detailed output,
80
+ # unless a formatter has already been configured
81
+ # (e.g. via a command-line flag).
82
+ config.default_formatter = "doc"
83
+ end
84
+
85
+ # Print the 10 slowest examples and example groups at the
86
+ # end of the spec run, to help surface which specs are running
87
+ # particularly slow.
88
+ config.profile_examples = 10
89
+
90
+ # Run specs in random order to surface order dependencies. If you find an
91
+ # order dependency and want to debug it, you can fix the order by providing
92
+ # the seed, which is printed after each run.
93
+ # --seed 1234
94
+ config.order = :random
95
+
96
+ # Seed global randomization in this process using the `--seed` CLI option.
97
+ # Setting this allows you to use `--seed` to deterministically reproduce
98
+ # test failures related to randomization by passing the same `--seed` value
99
+ # as the one that triggered the failure.
100
+ Kernel.srand config.seed
101
+ =end
102
+ end
metadata CHANGED
@@ -1,26 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_yappl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Peikert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
13
27
  description:
14
28
  email: t.s.peikert+yappl@gmail.com
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
33
+ - ".rspec"
34
+ - ".travis.yml"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - lib/exceptions.rb
40
+ - lib/json_validator.rb
19
41
  - lib/parser.rb
20
42
  - lib/policy.rb
21
43
  - lib/ruby_yappl.rb
22
44
  - lib/rule.rb
23
- homepage: http://rubygems.org/gems/ruby_yappl
45
+ - lib/schemas/yappl.json
46
+ - ruby_yappl.gemspec
47
+ - spec/json_validator_spec.rb
48
+ - spec/parser_spec.rb
49
+ - spec/policy_spec.rb
50
+ - spec/rule_spec.rb
51
+ - spec/schemas/broken_schema.json
52
+ - spec/schemas/invalid_schema.json
53
+ - spec/schemas/valid_schema.json
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/TPei/ruby_yappl
24
56
  licenses:
25
57
  - MIT
26
58
  metadata: {}
@@ -40,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
72
  version: '0'
41
73
  requirements: []
42
74
  rubyforge_project:
43
- rubygems_version: 2.5.2
75
+ rubygems_version: 2.7.6
44
76
  signing_key:
45
77
  specification_version: 4
46
78
  summary: ruby gem for the yappl privacy preference language