restful_objects 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +3 -2
  3. data/bin/restful_server.rb +0 -0
  4. data/lib/restful_objects/action_description.rb +96 -82
  5. data/lib/restful_objects/action_list.rb +17 -17
  6. data/lib/restful_objects/collection_description.rb +47 -47
  7. data/lib/restful_objects/collection_list.rb +17 -17
  8. data/lib/restful_objects/domain_model.rb +83 -79
  9. data/lib/restful_objects/http_response.rb +11 -11
  10. data/lib/restful_objects/link_generator.rb +104 -104
  11. data/lib/restful_objects/object.rb +20 -20
  12. data/lib/restful_objects/object_actions.rb +134 -134
  13. data/lib/restful_objects/object_base.rb +10 -0
  14. data/lib/restful_objects/object_collections.rb +84 -84
  15. data/lib/restful_objects/object_list.rb +16 -16
  16. data/lib/restful_objects/object_macros.rb +35 -35
  17. data/lib/restful_objects/object_properties.rb +78 -78
  18. data/lib/restful_objects/parameter_description.rb +60 -60
  19. data/lib/restful_objects/parameter_description_list.rb +15 -15
  20. data/lib/restful_objects/property_description.rb +68 -68
  21. data/lib/restful_objects/property_list.rb +17 -17
  22. data/lib/restful_objects/service.rb +21 -21
  23. data/lib/restful_objects/service_list.rb +55 -55
  24. data/lib/restful_objects/type.rb +110 -110
  25. data/lib/restful_objects/type_list.rb +30 -30
  26. data/lib/restful_objects/user.rb +30 -30
  27. data/lib/restful_objects/version.rb +1 -1
  28. data/spec/integration/domain-types_actions_spec.rb +43 -43
  29. data/spec/integration/domain-types_collections_spec.rb +45 -45
  30. data/spec/integration/domain-types_properties_spec.rb +41 -41
  31. data/spec/integration/server-root_spec.rb +98 -98
  32. data/spec/spec_helper.rb +52 -52
  33. data/spec/unit/object_actions_spec.rb +380 -380
  34. data/spec/unit/object_collections_spec.rb +190 -190
  35. data/spec/unit/object_properties_spec.rb +215 -206
  36. data/spec/unit/object_spec.rb +228 -228
  37. data/spec/unit/service_spec.rb +125 -125
  38. data/spec/unit/type_list_spec.rb +37 -37
  39. metadata +2 -2
@@ -1,11 +1,11 @@
1
- module RestfulObjects
2
- class HttpResponse
3
- attr_accessor :body, :content_type, :status
4
-
5
- def initialize(body, content_type, status = 200)
6
- @body = body
7
- @content_type = content_type
8
- @status = status
9
- end
10
- end
11
- end
1
+ module RestfulObjects
2
+ class HttpResponse
3
+ attr_accessor :body, :content_type, :status
4
+
5
+ def initialize(body, content_type, status = 200)
6
+ @body = body
7
+ @content_type = content_type
8
+ @status = status
9
+ end
10
+ end
11
+ end
@@ -1,104 +1,104 @@
1
- module RestfulObjects
2
- module LinkGenerator
3
- def link_to(rel, href, type, options = {})
4
- link = {
5
- 'rel' => generate_rel(rel, options),
6
- 'href' => RestfulObjects::DomainModel.current.base_url + href,
7
- 'type' => generate_repr_type(type, options[:domain_type], options[:element_type]),
8
- 'method' => options[:method] || 'GET' }
9
-
10
- link['arguments'] = options[:arguments] if options[:arguments]
11
-
12
- link
13
- end
14
-
15
- def generate_rel(rel, options = {})
16
- if [:self, :up, :next, :previous, :icon, :help].include?(rel)
17
- rel.to_s
18
- elsif rel == :described_by
19
- 'describedby'
20
- else
21
- if [:action, :action_param, :collection, :delete, :domain_type, :domain_types, :element, :element_type, :persist,
22
- :property, :return_type, :services, :update, :user, :version].include?(rel)
23
- 'urn:org.restfulobjects:rels/' + underscore_to_hyphen_string(rel)
24
- else
25
- 'urn:org.restfulobjects:rels/' +
26
- case rel
27
- when :add_to
28
- 'add-to;collection="' + options[:collection] + '"'
29
- when :attachment
30
- 'attachment;property="' + options[:property] + '"'
31
- when :choice
32
- if options[:property]
33
- 'choice;property="' + options[:property] + '"'
34
- elsif options[:action]
35
- 'choice;action="' + options[:action] + '"'
36
- elsif options[:param]
37
- 'choice;param="' + options[:param] + '"'
38
- else
39
- raise 'option not found for choice rel'
40
- end
41
- when :clear
42
- 'clear;property="' + options[:property] + '"'
43
- when :details
44
- if options[:property]
45
- 'details;property="' + options[:property] + '"'
46
- elsif options[:collection]
47
- 'details;collection="' + options[:collection] + '"'
48
- elsif options[:action]
49
- 'details;action="' + options[:action] + '"'
50
- else
51
- raise 'option not found for details rel'
52
- end
53
- when :invoke
54
- if options[:action]
55
- 'invoke;action="' + options[:action] + '"'
56
- elsif options[:type_action]
57
- 'invoke;typeaction="' + options[:type_action] + '"'
58
- else
59
- raise 'option not found for invoke rel'
60
- end
61
- when :modify
62
- 'modify;property="' + options[:property] + '"'
63
- when :remove_from
64
- 'remove-from;collection="' + options[:collection] + '"'
65
- when :service
66
- 'service;serviceId="' + options[:service_id] + '"'
67
- when :value
68
- if options[:property]
69
- 'value;property="' + options[:property] + '"'
70
- elsif options[:collection]
71
- 'value;collection="' + options[:collection] + '"'
72
- else
73
- raise 'option not found for value rel'
74
- end
75
- else
76
- raise "rel invalid: #{rel}"
77
- end
78
- end
79
- end
80
- end
81
-
82
- def generate_repr_type(type, domain_type = nil, element_type = nil)
83
- valid_repr = [:homepage, :user, :version, :list, :object, :object_property, :object_collection, :object_action,
84
- :object_result, :action_result, :type_list, :domain_type, :property_description, :collection_description,
85
- :action_description, :action_param_description, :type_action_result, :error, :services]
86
-
87
- raise "repr-type invalid: #{type.to_s}" if not valid_repr.include?(type)
88
-
89
- repr = 'application/json;profile="urn:org.restfulobjects:repr-types/' + underscore_to_hyphen_string(type) + '"'
90
-
91
- if domain_type && [:object, :action].include?(type)
92
- repr += ';x-ro-domain-type="' + domain_type + '"'
93
- elsif element_type && [:object_collection, :action].include?(type)
94
- repr +=';x-ro-element-type="' + element_type + '"'
95
- end
96
-
97
- repr
98
- end
99
-
100
- def underscore_to_hyphen_string(symbol)
101
- symbol.to_s.sub('_', '-')
102
- end
103
- end
104
- end
1
+ module RestfulObjects
2
+ module LinkGenerator
3
+ def link_to(rel, href, type, options = {})
4
+ link = {
5
+ 'rel' => generate_rel(rel, options),
6
+ 'href' => RestfulObjects::DomainModel.current.base_url + href,
7
+ 'type' => generate_repr_type(type, options[:domain_type], options[:element_type]),
8
+ 'method' => options[:method] || 'GET' }
9
+
10
+ link['arguments'] = options[:arguments] if options[:arguments]
11
+
12
+ link
13
+ end
14
+
15
+ def generate_rel(rel, options = {})
16
+ if [:self, :up, :next, :previous, :icon, :help].include?(rel)
17
+ rel.to_s
18
+ elsif rel == :described_by
19
+ 'describedby'
20
+ else
21
+ if [:action, :action_param, :collection, :delete, :domain_type, :domain_types, :element, :element_type, :persist,
22
+ :property, :return_type, :services, :update, :user, :version].include?(rel)
23
+ 'urn:org.restfulobjects:rels/' + underscore_to_hyphen_string(rel)
24
+ else
25
+ 'urn:org.restfulobjects:rels/' +
26
+ case rel
27
+ when :add_to
28
+ 'add-to;collection="' + options[:collection] + '"'
29
+ when :attachment
30
+ 'attachment;property="' + options[:property] + '"'
31
+ when :choice
32
+ if options[:property]
33
+ 'choice;property="' + options[:property] + '"'
34
+ elsif options[:action]
35
+ 'choice;action="' + options[:action] + '"'
36
+ elsif options[:param]
37
+ 'choice;param="' + options[:param] + '"'
38
+ else
39
+ raise 'option not found for choice rel'
40
+ end
41
+ when :clear
42
+ 'clear;property="' + options[:property] + '"'
43
+ when :details
44
+ if options[:property]
45
+ 'details;property="' + options[:property] + '"'
46
+ elsif options[:collection]
47
+ 'details;collection="' + options[:collection] + '"'
48
+ elsif options[:action]
49
+ 'details;action="' + options[:action] + '"'
50
+ else
51
+ raise 'option not found for details rel'
52
+ end
53
+ when :invoke
54
+ if options[:action]
55
+ 'invoke;action="' + options[:action] + '"'
56
+ elsif options[:type_action]
57
+ 'invoke;typeaction="' + options[:type_action] + '"'
58
+ else
59
+ raise 'option not found for invoke rel'
60
+ end
61
+ when :modify
62
+ 'modify;property="' + options[:property] + '"'
63
+ when :remove_from
64
+ 'remove-from;collection="' + options[:collection] + '"'
65
+ when :service
66
+ 'service;serviceId="' + options[:service_id] + '"'
67
+ when :value
68
+ if options[:property]
69
+ 'value;property="' + options[:property] + '"'
70
+ elsif options[:collection]
71
+ 'value;collection="' + options[:collection] + '"'
72
+ else
73
+ raise 'option not found for value rel'
74
+ end
75
+ else
76
+ raise "rel invalid: #{rel}"
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ def generate_repr_type(type, domain_type = nil, element_type = nil)
83
+ valid_repr = [:homepage, :user, :version, :list, :object, :object_property, :object_collection, :object_action,
84
+ :object_result, :action_result, :type_list, :domain_type, :property_description, :collection_description,
85
+ :action_description, :action_param_description, :type_action_result, :error, :services]
86
+
87
+ raise "repr-type invalid: #{type.to_s}" if not valid_repr.include?(type)
88
+
89
+ repr = 'application/json;profile="urn:org.restfulobjects:repr-types/' + underscore_to_hyphen_string(type) + '"'
90
+
91
+ if domain_type && [:object, :action].include?(type)
92
+ repr += ';x-ro-domain-type="' + domain_type + '"'
93
+ elsif element_type && [:object_collection, :action].include?(type)
94
+ repr +=';x-ro-element-type="' + element_type + '"'
95
+ end
96
+
97
+ repr
98
+ end
99
+
100
+ def underscore_to_hyphen_string(symbol)
101
+ symbol.to_s.sub('_', '-')
102
+ end
103
+ end
104
+ end
@@ -1,20 +1,20 @@
1
- # encoding: utf-8
2
-
3
- module RestfulObjects
4
- module Object
5
- include LinkGenerator
6
-
7
- def self.included(base)
8
- RestfulObjects::DomainModel.current.types.add(base.name)
9
-
10
- base.class_eval do
11
- extend ObjectMacros
12
- include ObjectBase
13
- include ObjectProperties
14
- include ObjectCollections
15
- include ObjectActions
16
- end
17
- end
18
- end
19
- end
20
-
1
+ # encoding: utf-8
2
+
3
+ module RestfulObjects
4
+ module Object
5
+ include LinkGenerator
6
+
7
+ def self.included(base)
8
+ RestfulObjects::DomainModel.current.types.add(base.name)
9
+
10
+ base.class_eval do
11
+ extend ObjectMacros
12
+ include ObjectBase
13
+ include ObjectProperties
14
+ include ObjectCollections
15
+ include ObjectActions
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -1,134 +1,134 @@
1
- module RestfulObjects
2
- module ObjectActions
3
- def actions_members
4
- members = {}
5
- rs_type.actions.each do |name, action|
6
- members[name] = {
7
- 'memberType' => 'action',
8
- 'links' => [
9
- !is_service ?
10
- link_to(:details, "/objects/#{self.class.name}/#{object_id}/actions/#{name}", :object_action, action: name)
11
- :
12
- link_to(:details, "/services/#{self.class.name}/actions/#{name}", :object_action, action: name)
13
- ],
14
- 'extensions' => action.metadata
15
- }
16
- end
17
- members
18
- end
19
-
20
- def get_action(action)
21
- { 'id' => action,
22
- 'parameters' => generate_parameters(action),
23
- 'links' => [ rs_action_link(action), rs_action_up_link, rs_invoke_link(action) ],
24
- 'extensions' => rs_type.actions[action].metadata
25
- }.to_json
26
- end
27
-
28
- def generate_parameters(action)
29
- parameters = Hash.new
30
- rs_type.actions[action].parameters.each do |name, parameter|
31
- parameters[name] = {
32
- 'links' => [],
33
- 'extensions' => parameter.metadata
34
- }
35
- end
36
- parameters
37
- end
38
-
39
- def get_action_invoke(action, json)
40
- raise 'action does not exists' if not rs_type.actions.include?(action)
41
- action_description = rs_type.actions[action]
42
- json == '' ? {} : arguments = JSON.parse(json)
43
- parameters = []
44
- action_description.parameters.each do |name, parameter|
45
- case parameter.type
46
- when :int
47
- parameters << arguments[name]['value'].to_i
48
- else
49
- parameters << arguments[name]['value']
50
- end
51
- end
52
-
53
- result = send(action.to_sym, *parameters)
54
-
55
- action_link = link_to(:self, "/objects/#{self.class.name}/#{object_id}/actions/#{action}/invoke", :action_result)
56
- action_link['arguments'] = arguments
57
-
58
- response = {
59
- 'links' => [ action_link ],
60
- 'resultType' => 'scalar',
61
- 'result' => {
62
- 'links' => [],
63
- 'extensions' => { }
64
- },
65
- 'extensions' => { }
66
- }
67
-
68
- response['resultType'] = action_description.kind_result_type.to_s
69
- if result.nil?
70
- response['result'] = nil
71
- else
72
- case action_description.kind_result_type
73
- when :scalar
74
- response['resultType'] = 'scalar'
75
- response['result']['value'] = encode_value(result, action_description.result_type)
76
- response['result']['links'] = [ link_to(:return_type, '/domain-types/int', :domain_type) ]
77
- when :object
78
- response['resultType'] = 'object'
79
- response['result'] = result.representation
80
- when :proto_object
81
- response['resultType'] = 'object'
82
- response['result'] = result
83
- when :list
84
- response['resultType'] = 'list'
85
- response['result']['links'] =
86
- [ link_to(:element_type, "/domain-types/#{action_description.result_type.to_s}", :domain_type) ]
87
- list = []
88
- result.each do |member|
89
- member_link = link_to(:element, "/objects/#{action_description.result_type.to_s}/#{member.rs_instance_id}", :object)
90
- member_link['title'] = member.title
91
- list << member_link
92
- end
93
- response['result']['value'] = list
94
- end
95
- end
96
-
97
- response.to_json
98
- end
99
-
100
- def action_return_type(action)
101
- RestfulObjects::DomainModel.current.types[self.class.name].actions[action].result_type
102
- end
103
-
104
- private
105
-
106
- def rs_invoke_link(action)
107
- invoke_link = is_service ?
108
- link_to(:invoke, "/services/#{rs_type.id}/actions/#{action}/invoke", :action_result, action: action)
109
- :
110
- link_to(:invoke, "/objects/#{rs_type.id}/#{object_id}/actions/#{action}/invoke", :action_result, action: action)
111
- invoke_link['arguments'] = {}
112
- rs_type.actions[action].parameters.each do |name, action|
113
- invoke_link['arguments'][name] = { 'value' => nil }
114
- end
115
- invoke_link
116
- end
117
-
118
- def rs_action_link(action)
119
- if is_service
120
- link_to(:self, "/services/#{self.class.name}/actions/#{action}", :object_action)
121
- else
122
- link_to(:self, "/objects/#{self.class.name}/#{object_id}/actions/#{action}", :object_action)
123
- end
124
- end
125
-
126
- def rs_action_up_link
127
- if is_service
128
- link_to(:up, "/services/#{rs_type.id}", :object)
129
- else
130
- link_to(:up, "/objects/#{self.class.name}/#{object_id}", :object)
131
- end
132
- end
133
- end
134
- end
1
+ module RestfulObjects
2
+ module ObjectActions
3
+ def actions_members
4
+ members = {}
5
+ rs_type.actions.each do |name, action|
6
+ members[name] = {
7
+ 'memberType' => 'action',
8
+ 'links' => [
9
+ !is_service ?
10
+ link_to(:details, "/objects/#{self.class.name}/#{object_id}/actions/#{name}", :object_action, action: name)
11
+ :
12
+ link_to(:details, "/services/#{self.class.name}/actions/#{name}", :object_action, action: name)
13
+ ],
14
+ 'extensions' => action.metadata
15
+ }
16
+ end
17
+ members
18
+ end
19
+
20
+ def get_action(action)
21
+ { 'id' => action,
22
+ 'parameters' => generate_parameters(action),
23
+ 'links' => [ rs_action_link(action), rs_action_up_link, rs_invoke_link(action) ],
24
+ 'extensions' => rs_type.actions[action].metadata
25
+ }.to_json
26
+ end
27
+
28
+ def generate_parameters(action)
29
+ parameters = Hash.new
30
+ rs_type.actions[action].parameters.each do |name, parameter|
31
+ parameters[name] = {
32
+ 'links' => [],
33
+ 'extensions' => parameter.metadata
34
+ }
35
+ end
36
+ parameters
37
+ end
38
+
39
+ def get_action_invoke(action, json)
40
+ raise 'action does not exists' if not rs_type.actions.include?(action)
41
+ action_description = rs_type.actions[action]
42
+ json == '' ? {} : arguments = JSON.parse(json)
43
+ parameters = []
44
+ action_description.parameters.each do |name, parameter|
45
+ case parameter.type
46
+ when :int
47
+ parameters << arguments[name.to_s]['value'].to_i
48
+ else
49
+ parameters << arguments[name.to_s]['value']
50
+ end
51
+ end
52
+
53
+ result = send(action.to_sym, *parameters)
54
+
55
+ action_link = link_to(:self, "/objects/#{self.class.name}/#{object_id}/actions/#{action}/invoke", :action_result)
56
+ action_link['arguments'] = arguments
57
+
58
+ response = {
59
+ 'links' => [ action_link ],
60
+ 'resultType' => 'scalar',
61
+ 'result' => {
62
+ 'links' => [],
63
+ 'extensions' => { }
64
+ },
65
+ 'extensions' => { }
66
+ }
67
+
68
+ response['resultType'] = action_description.kind_result_type.to_s
69
+ if result.nil?
70
+ response['result'] = nil
71
+ else
72
+ case action_description.kind_result_type
73
+ when :scalar
74
+ response['resultType'] = 'scalar'
75
+ response['result']['value'] = encode_value(result, action_description.result_type)
76
+ response['result']['links'] = [ link_to(:return_type, '/domain-types/int', :domain_type) ]
77
+ when :object
78
+ response['resultType'] = 'object'
79
+ response['result'] = result.representation
80
+ when :proto_object
81
+ response['resultType'] = 'object'
82
+ response['result'] = result
83
+ when :list
84
+ response['resultType'] = 'list'
85
+ response['result']['links'] =
86
+ [ link_to(:element_type, "/domain-types/#{action_description.result_type.to_s}", :domain_type) ]
87
+ list = []
88
+ result.each do |member|
89
+ member_link = link_to(:element, "/objects/#{action_description.result_type.to_s}/#{member.rs_instance_id}", :object)
90
+ member_link['title'] = member.title
91
+ list << member_link
92
+ end
93
+ response['result']['value'] = list
94
+ end
95
+ end
96
+
97
+ response.to_json
98
+ end
99
+
100
+ def action_return_type(action)
101
+ RestfulObjects::DomainModel.current.types[self.class.name].actions[action].result_type
102
+ end
103
+
104
+ private
105
+
106
+ def rs_invoke_link(action)
107
+ invoke_link = is_service ?
108
+ link_to(:invoke, "/services/#{rs_type.id}/actions/#{action}/invoke", :action_result, action: action)
109
+ :
110
+ link_to(:invoke, "/objects/#{rs_type.id}/#{object_id}/actions/#{action}/invoke", :action_result, action: action)
111
+ invoke_link['arguments'] = {}
112
+ rs_type.actions[action].parameters.each do |name, action|
113
+ invoke_link['arguments'][name] = { 'value' => nil }
114
+ end
115
+ invoke_link
116
+ end
117
+
118
+ def rs_action_link(action)
119
+ if is_service
120
+ link_to(:self, "/services/#{self.class.name}/actions/#{action}", :object_action)
121
+ else
122
+ link_to(:self, "/objects/#{self.class.name}/#{object_id}/actions/#{action}", :object_action)
123
+ end
124
+ end
125
+
126
+ def rs_action_up_link
127
+ if is_service
128
+ link_to(:up, "/services/#{rs_type.id}", :object)
129
+ else
130
+ link_to(:up, "/objects/#{self.class.name}/#{object_id}", :object)
131
+ end
132
+ end
133
+ end
134
+ end