odrl-ruby 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,110 +1,110 @@
1
- # frozen_string_literal: true
2
-
3
- module ODRL
4
- class Constraint < Base
5
- attr_accessor :uid, :rightOperand, :leftOperand, :operator, :rightOperandReference, :dataType, :unit, :status
6
-
7
- def initialize(
8
- rightOperand:, operator:, leftOperand:, uid: nil,
9
- rightOPerandReference: nil,
10
- dataType: nil,
11
- unit: nil,
12
- status: nil,
13
- type: CCONSTRAINT,
14
- **args
15
- )
16
-
17
- @uid = uid
18
- @uid ||= Base.baseURI + "#constraint_" + Base.getuuid
19
- super(uid: @uid, type: type, **args)
20
-
21
- @rightOperand = rightOperand
22
- raise "Constraints must haves a Right operand such as 'event' - I'm dead!" unless @rightOperand
23
-
24
- # if it is already a URI, then let it go
25
- # @rightOperand = "http://www.w3.org/ns/odrl/2/#{@rightOperand}" unless @rightOperand =~ %r{https?://}
26
-
27
- @leftOperand = leftOperand
28
- unless LEFTOPERANDS.include?(@leftOperand)
29
- warn "The selected leftOperand is not part of the default set... just sayin'"
30
- end
31
- unless @leftOperand
32
- raise "Constraints must haves a Left Operand such as 'http://some.event.org/on-now' - I'm dead!"
33
- end
34
-
35
- # if it is already a URI, then let it go
36
- @leftOperand = "http://www.w3.org/ns/odrl/2/#{@leftOperand}" unless @leftOperand =~ %r{https?://}
37
-
38
- @operator = operator
39
- raise "Constraints must haves an operator such as 'eq' - I'm dead!" unless @operator
40
-
41
- warn "The selected operator is not part of the default set... just sayin'" unless OPERATORS.include?(@operator)
42
-
43
- # if it is already a URI, then let it go
44
- @operator = "http://www.w3.org/ns/odrl/2/#{@operator}" unless @operator =~ %r{https?://}
45
-
46
- @rightOperandReference = rightOperandReference
47
- @dataType = dataType
48
- @unit = unit
49
- @status = status
50
- end
51
-
52
- def load_graph
53
- super
54
- # TODO: This is bad DRY!! Put the bulk of this method into the base object
55
- if rightOperand
56
- predicate = PRIGHT
57
- object = rightOperand
58
- subject = uid
59
- repo = repository
60
- triplify(subject, predicate, object, repo)
61
- end
62
- if leftOperand
63
- predicate = PLEFT
64
- object = leftOperand
65
- subject = uid
66
- repo = repository
67
- triplify(subject, predicate, object, repo)
68
- end
69
- if operator
70
- predicate = POPERATOR
71
- object = operator
72
- subject = uid
73
- repo = repository
74
- triplify(subject, predicate, object, repo)
75
- end
76
- if rightOperandReference
77
- predicate = POPERANDREFERENCE
78
- object = rightOperandReference
79
- subject = uid
80
- repo = repository
81
- triplify(subject, predicate, object, repo)
82
- end
83
- if dataType
84
- predicate = PDATATYPE
85
- object = dataType
86
- subject = uid
87
- repo = repository
88
- triplify(subject, predicate, object, repo)
89
- end
90
- if unit
91
- predicate = PUNIT
92
- object = unit
93
- subject = uid
94
- repo = repository
95
- triplify(subject, predicate, object, repo)
96
- end
97
- return unless status
98
-
99
- predicate = PSTATUS
100
- object = status
101
- subject = uid
102
- repo = repository
103
- triplify(subject, predicate, object, repo)
104
- end
105
-
106
- def serialize(format:)
107
- super
108
- end
109
- end
110
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ODRL
4
+ class Constraint < Base
5
+ attr_accessor :uid, :rightOperand, :leftOperand, :operator, :rightOperandReference, :dataType, :unit, :status
6
+
7
+ def initialize(
8
+ rightOperand:, operator:, leftOperand:, uid: nil,
9
+ rightOPerandReference: nil,
10
+ dataType: nil,
11
+ unit: nil,
12
+ status: nil,
13
+ type: CCONSTRAINT,
14
+ **args
15
+ )
16
+
17
+ @uid = uid
18
+ @uid ||= Base.baseURI + "#constraint_" + Base.getuuid
19
+ super(uid: @uid, type: type, **args)
20
+
21
+ @rightOperand = rightOperand
22
+ raise "Constraints must haves a Right operand such as 'event' - I'm dead!" unless @rightOperand
23
+
24
+ # if it is already a URI, then let it go
25
+ # @rightOperand = "http://www.w3.org/ns/odrl/2/#{@rightOperand}" unless @rightOperand =~ %r{https?://}
26
+
27
+ @leftOperand = leftOperand
28
+ unless LEFTOPERANDS.include?(@leftOperand)
29
+ warn "The selected leftOperand is not part of the default set... just sayin'"
30
+ end
31
+ unless @leftOperand
32
+ raise "Constraints must haves a Left Operand such as 'http://some.event.org/on-now' - I'm dead!"
33
+ end
34
+
35
+ # if it is already a URI, then let it go
36
+ @leftOperand = "http://www.w3.org/ns/odrl/2/#{@leftOperand}" unless @leftOperand =~ %r{https?://}
37
+
38
+ @operator = operator
39
+ raise "Constraints must haves an operator such as 'eq' - I'm dead!" unless @operator
40
+
41
+ warn "The selected operator is not part of the default set... just sayin'" unless OPERATORS.include?(@operator)
42
+
43
+ # if it is already a URI, then let it go
44
+ @operator = "http://www.w3.org/ns/odrl/2/#{@operator}" unless @operator =~ %r{https?://}
45
+
46
+ @rightOperandReference = rightOperandReference
47
+ @dataType = dataType
48
+ @unit = unit
49
+ @status = status
50
+ end
51
+
52
+ def load_graph
53
+ super
54
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
55
+ if rightOperand
56
+ predicate = PRIGHT
57
+ object = rightOperand
58
+ subject = uid
59
+ repo = repository
60
+ triplify(subject, predicate, object, repo)
61
+ end
62
+ if leftOperand
63
+ predicate = PLEFT
64
+ object = leftOperand
65
+ subject = uid
66
+ repo = repository
67
+ triplify(subject, predicate, object, repo)
68
+ end
69
+ if operator
70
+ predicate = POPERATOR
71
+ object = operator
72
+ subject = uid
73
+ repo = repository
74
+ triplify(subject, predicate, object, repo)
75
+ end
76
+ if rightOperandReference
77
+ predicate = POPERANDREFERENCE
78
+ object = rightOperandReference
79
+ subject = uid
80
+ repo = repository
81
+ triplify(subject, predicate, object, repo)
82
+ end
83
+ if dataType
84
+ predicate = PDATATYPE
85
+ object = dataType
86
+ subject = uid
87
+ repo = repository
88
+ triplify(subject, predicate, object, repo)
89
+ end
90
+ if unit
91
+ predicate = PUNIT
92
+ object = unit
93
+ subject = uid
94
+ repo = repository
95
+ triplify(subject, predicate, object, repo)
96
+ end
97
+ return unless status
98
+
99
+ predicate = PSTATUS
100
+ object = status
101
+ subject = uid
102
+ repo = repository
103
+ triplify(subject, predicate, object, repo)
104
+ end
105
+
106
+ def serialize(format:)
107
+ super
108
+ end
109
+ end
110
+ end
@@ -1,7 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
- module ODRL
4
- module ODRL
5
- VERSION = "0.2.3"
6
- end
7
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ODRL
4
+ module ODRL
5
+ VERSION = "0.2.4"
6
+ end
7
+ end
data/lib/odrl/party.rb CHANGED
@@ -1,97 +1,97 @@
1
- # frozen_string_literal: true
2
-
3
- module ODRL
4
- class Party < Base
5
- attr_accessor :uid, :refinements, :partOf, :predicate, :type
6
-
7
- def initialize(
8
- uid: nil,
9
- refinements: nil,
10
- partOf: nil,
11
- predicate: nil,
12
- type: CPARTY,
13
- **args
14
- )
15
-
16
- @uid = uid
17
- @uid ||= Base.baseURI + "#party_" + Base.getuuid
18
- super(uid: @uid, type: type, **args)
19
-
20
- @refinements = {}
21
- @partOf = {}
22
- @predicate = predicate.to_s
23
-
24
- if @predicate&.match(/https?:/)
25
- # do nothing! It's their choice to send a full predicate!
26
- elsif @predicate # we're guessing it will be a string from the valid list?
27
- unless PARTYFUNCTIONS.include? @predicate
28
- raise "You didn't indicate a valid predicate"
29
- # @predicate = ODRLV.assigner
30
- else
31
- @predicate = "#{ODRLV.to_s}#{predicate}" # make the URI form of the valid predicate
32
- end
33
- elsif @predicate.nil?
34
- raise "If you don't indicate a predicate at all"
35
- # @predicate = ODRLV.assigner
36
- end
37
-
38
- refinements = [refinements] unless refinements.is_a? Array
39
- unless refinements.first.nil?
40
- refinements.each do |c|
41
- addRefinement(refinement: c)
42
- end
43
- end
44
-
45
- partOf = [partOf] unless partOf.is_a? Array
46
- unless partOf.first.nil?
47
- partOf.each do |p|
48
- p.addPart(part: self)
49
- end
50
- end
51
-
52
- unless @partOf and !(@partOf.is_a? PartyCollection) # if it exists and is the wrong type
53
- raise "The parent collection of a Party must be of type ODRL::PaertyCollection."
54
- end
55
-
56
- end
57
-
58
- def addRefinement(refinement: args)
59
- raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
60
-
61
- refinements[refinement.uid] = [PREFINEMENT, refinement]
62
- end
63
-
64
- def addPart(part: args)
65
- raise "Party cannot be added as part of something that is not an PartyCollection" unless self.is_a?(PartyCollection)
66
- part.partOf[uid] = [PPARTOF, self]
67
- end
68
-
69
- def load_graph
70
- super
71
- # TODO: This is bad DRY!! Put the bulk of this method into the base object
72
- # TODO: Currently we don't support partOf
73
- %i[refinements].each do |connected_object_type|
74
- next unless send(connected_object_type)
75
-
76
- send(connected_object_type).each do |_uid, typedconnection|
77
- predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
78
- object = odrlobject.uid
79
- subject = uid
80
- repo = repository
81
- triplify(subject, predicate, object, repo)
82
- odrlobject.load_graph # start the cascade
83
- end
84
- end
85
- end
86
-
87
- def serialize(format:)
88
- super
89
- end
90
- end
91
-
92
- class PartyCollection < Party
93
- def initialize(type: CPARTYCOLLECTION, **args)
94
- super(type: type, **args)
95
- end
96
- end
97
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ODRL
4
+ class Party < Base
5
+ attr_accessor :uid, :refinements, :partOf, :predicate, :type
6
+
7
+ def initialize(
8
+ uid: nil,
9
+ refinements: nil,
10
+ partOf: nil,
11
+ predicate: nil,
12
+ type: CPARTY,
13
+ **args
14
+ )
15
+
16
+ @uid = uid
17
+ @uid ||= Base.baseURI + "#party_" + Base.getuuid
18
+ super(uid: @uid, type: type, **args)
19
+
20
+ @refinements = {}
21
+ @partOf = {}
22
+ @predicate = predicate.to_s
23
+
24
+ if @predicate&.match(/https?:/)
25
+ # do nothing! It's their choice to send a full predicate!
26
+ elsif @predicate # we're guessing it will be a string from the valid list?
27
+ unless PARTYFUNCTIONS.include? @predicate
28
+ raise "You didn't indicate a valid predicate"
29
+ # @predicate = ODRLV.assigner
30
+ else
31
+ @predicate = "#{ODRLV.to_s}#{predicate}" # make the URI form of the valid predicate
32
+ end
33
+ elsif @predicate.nil?
34
+ raise "If you don't indicate a predicate at all"
35
+ # @predicate = ODRLV.assigner
36
+ end
37
+
38
+ refinements = [refinements] unless refinements.is_a? Array
39
+ unless refinements.first.nil?
40
+ refinements.each do |c|
41
+ addRefinement(refinement: c)
42
+ end
43
+ end
44
+
45
+ partOf = [partOf] unless partOf.is_a? Array
46
+ unless partOf.first.nil?
47
+ partOf.each do |p|
48
+ p.addPart(part: self)
49
+ end
50
+ end
51
+
52
+ unless @partOf and !(@partOf.is_a? PartyCollection) # if it exists and is the wrong type
53
+ raise "The parent collection of a Party must be of type ODRL::PaertyCollection."
54
+ end
55
+
56
+ end
57
+
58
+ def addRefinement(refinement: args)
59
+ raise "Refinement is not an ODRL Constraint" unless refinement.is_a?(Constraint)
60
+
61
+ refinements[refinement.uid] = [PREFINEMENT, refinement]
62
+ end
63
+
64
+ def addPart(part: args)
65
+ raise "Party cannot be added as part of something that is not an PartyCollection" unless self.is_a?(PartyCollection)
66
+ part.partOf[uid] = [PPARTOF, self]
67
+ end
68
+
69
+ def load_graph
70
+ super
71
+ # TODO: This is bad DRY!! Put the bulk of this method into the base object
72
+ # TODO: Currently we don't support partOf
73
+ %i[refinements].each do |connected_object_type|
74
+ next unless send(connected_object_type)
75
+
76
+ send(connected_object_type).each do |_uid, typedconnection|
77
+ predicate, odrlobject = typedconnection # e.g. "refinement", RefinementObject
78
+ object = odrlobject.uid
79
+ subject = uid
80
+ repo = repository
81
+ triplify(subject, predicate, object, repo)
82
+ odrlobject.load_graph # start the cascade
83
+ end
84
+ end
85
+ end
86
+
87
+ def serialize(format:)
88
+ super
89
+ end
90
+ end
91
+
92
+ class PartyCollection < Party
93
+ def initialize(type: CPARTYCOLLECTION, **args)
94
+ super(type: type, **args)
95
+ end
96
+ end
97
+ end
data/lib/odrl/policy.rb CHANGED
@@ -1,97 +1,97 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "odrl/version"
4
-
5
- module ODRL
6
- class Error < StandardError; end
7
-
8
- class Policy < Base
9
- attr_accessor :rules, :profiles
10
-
11
- def initialize(uid: nil, type: CPOLICY, **args)
12
- @uid = uid
13
- self.uid = Base.baseURI + "#policy_" + Base.getuuid unless @uid
14
- super(uid: @uid, type: type, **args)
15
-
16
- @rules = {}
17
- @profiles = {}
18
- end
19
-
20
- def addDuty(rule:)
21
- uid = rule.uid
22
- rules[uid] = [POBLIGATION, rule]
23
- end
24
-
25
- def addPermission(rule:)
26
- uid = rule.uid
27
- rules[uid] = [PPERMISSION, rule]
28
- end
29
-
30
- def addProhibition(rule:)
31
- uid = rule.uid
32
- rules[uid] = [PPROHIBITION, rule]
33
- end
34
-
35
- def addProfile(profile:)
36
- uid = profile.uid
37
- profiles[uid] = [PPROFILE, uid]
38
- end
39
-
40
- def load_graph
41
- super
42
- rules.each do |_uid, rulepair|
43
- predicate, ruleobject = rulepair # e.g. "permission", RuleObject
44
- object = ruleobject.uid
45
- subject = uid
46
- repo = repository
47
- triplify(subject, predicate, object, repo)
48
- ruleobject.load_graph # start the cascade
49
- end
50
-
51
- # TODO make test for profiles
52
- profiles.each do |_uid, profile_pair|
53
- predicate, profileuri = profile_pair # e.g. "permission", RuleObject
54
- object = profileuri
55
- subject = uid
56
- repo = repository
57
- triplify(subject, predicate, object, repo)
58
- end
59
-
60
- end
61
-
62
- def serialize(format:)
63
- super
64
- end
65
- end
66
-
67
- class Set < Policy
68
- def initialize(type: CSET, **args)
69
- super(type: type, **args)
70
- end
71
- end
72
-
73
- class Offer < Set
74
- def initialize(type: COFFER, **args)
75
- super(type: type, **args)
76
- end
77
- end
78
-
79
- class Agreement < Set
80
- def initialize(type: CAGREEMENT, **args)
81
- super(type: type, **args)
82
- end
83
- end
84
-
85
- class Request < Set
86
- def initialize(type: CREQUEST, **args)
87
- super(type: type, **args)
88
- end
89
- end
90
-
91
- class Privacy < Set
92
- def initialize(type: CPRIVACY, **args)
93
- super(type: type, **args)
94
- end
95
- end
96
- # ====================================================
97
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "odrl/version"
4
+
5
+ module ODRL
6
+ class Error < StandardError; end
7
+
8
+ class Policy < Base
9
+ attr_accessor :rules, :profiles
10
+
11
+ def initialize(uid: nil, type: CPOLICY, **args)
12
+ @uid = uid
13
+ self.uid = Base.baseURI + "#policy_" + Base.getuuid unless @uid
14
+ super(uid: @uid, type: type, **args)
15
+
16
+ @rules = {}
17
+ @profiles = {}
18
+ end
19
+
20
+ def addDuty(rule:)
21
+ uid = rule.uid
22
+ rules[uid] = [POBLIGATION, rule]
23
+ end
24
+
25
+ def addPermission(rule:)
26
+ uid = rule.uid
27
+ rules[uid] = [PPERMISSION, rule]
28
+ end
29
+
30
+ def addProhibition(rule:)
31
+ uid = rule.uid
32
+ rules[uid] = [PPROHIBITION, rule]
33
+ end
34
+
35
+ def addProfile(profile:)
36
+ uid = profile.uid
37
+ profiles[uid] = [PPROFILE, uid]
38
+ end
39
+
40
+ def load_graph
41
+ super
42
+ rules.each do |_uid, rulepair|
43
+ predicate, ruleobject = rulepair # e.g. "permission", RuleObject
44
+ object = ruleobject.uid
45
+ subject = uid
46
+ repo = repository
47
+ triplify(subject, predicate, object, repo)
48
+ ruleobject.load_graph # start the cascade
49
+ end
50
+
51
+ # TODO make test for profiles
52
+ profiles.each do |_uid, profile_pair|
53
+ predicate, profileuri = profile_pair # e.g. "permission", RuleObject
54
+ object = profileuri
55
+ subject = uid
56
+ repo = repository
57
+ triplify(subject, predicate, object, repo)
58
+ end
59
+
60
+ end
61
+
62
+ def serialize(format:)
63
+ super
64
+ end
65
+ end
66
+
67
+ class Set < Policy
68
+ def initialize(type: CSET, **args)
69
+ super(type: type, **args)
70
+ end
71
+ end
72
+
73
+ class Offer < Set
74
+ def initialize(type: COFFER, **args)
75
+ super(type: type, **args)
76
+ end
77
+ end
78
+
79
+ class Agreement < Set
80
+ def initialize(type: CAGREEMENT, **args)
81
+ super(type: type, **args)
82
+ end
83
+ end
84
+
85
+ class Request < Set
86
+ def initialize(type: CREQUEST, **args)
87
+ super(type: type, **args)
88
+ end
89
+ end
90
+
91
+ class Privacy < Set
92
+ def initialize(type: CPRIVACY, **args)
93
+ super(type: type, **args)
94
+ end
95
+ end
96
+ # ====================================================
97
+ end