restful_objects 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/LICENSE +191 -0
- data/README.md +83 -0
- data/bin/restful_server.rb +12 -0
- data/lib/restful_objects/{model.rb → domain_model.rb} +0 -0
- data/lib/restful_objects/object.rb +2 -114
- data/lib/restful_objects/object_base.rb +116 -0
- data/lib/restful_objects/property_description.rb +68 -0
- data/lib/restful_objects/service.rb +21 -0
- data/lib/restful_objects/version.rb +1 -1
- data/lib/restful_objects.rb +34 -0
- data/spec/integration/domain-types_actions_spec.rb +43 -0
- data/spec/integration/domain-types_collections_spec.rb +45 -0
- data/spec/integration/domain-types_properties_spec.rb +41 -0
- data/spec/integration/server-root_spec.rb +98 -0
- data/spec/spec_helper.rb +52 -0
- data/spec/unit/object_actions_spec.rb +380 -0
- data/spec/unit/object_collections_spec.rb +190 -0
- data/spec/unit/object_properties_spec.rb +206 -0
- data/spec/unit/object_spec.rb +228 -0
- data/spec/unit/service_spec.rb +125 -0
- data/spec/unit/type_list_spec.rb +37 -0
- metadata +49 -20
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDhlYTM4MGRmZDMxOWQzM2JlYmQ5N2Q5NTFkM2YxZjhmYzQ1MDA1MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZGE4MmUxYTdiZTdiNDViNjI3NmIzMmIwZWMyZjJiYjAwZWIwMzFhMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
N2FhZjcwNzA2Mzc1YmQzZGRlZjliZGNjNjdhMjE4NzhiZDAwOGEzZTg4MmUx
|
10
|
+
NGRlYWYwOTU0NWM2OWZmZDVkNjA2YWZmNDM2ZjY3MDRmZTBjZmE2YTFjNDk5
|
11
|
+
OWIyZDllZjdjMjc4YWExZGFhOTIyM2I2NDBhMDYwN2M0NTIxNjA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTU2ODM3N2RjODM0ZjBjNDU0YWVkNTYwN2U3MjFhZGI4OGQ3MDdkZWU0NmYx
|
14
|
+
ZjZlMTFjMTJjNzAxZWEyOWI1Yzg2MmMxMDliYTE3ZTNkYTI1YmE0ZjdmZWQx
|
15
|
+
MjExZDY2ZDk1MjM4NzBmODkyMTE1MDdkOTgxZDFkNzNkN2ZmMjE=
|
data/LICENSE
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
13
|
+
owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
21
|
+
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
23
|
+
permissions granted by this License.
|
24
|
+
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
27
|
+
files.
|
28
|
+
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
31
|
+
generated documentation, and conversions to other media types.
|
32
|
+
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
35
|
+
in or attached to the work (an example is provided in the Appendix below).
|
36
|
+
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
43
|
+
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
55
|
+
owner as "Not a Contribution."
|
56
|
+
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
59
|
+
incorporated within the Work.
|
60
|
+
|
61
|
+
2. Grant of Copyright License.
|
62
|
+
|
63
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
64
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
65
|
+
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
66
|
+
publicly display, publicly perform, sublicense, and distribute the Work and such
|
67
|
+
Derivative Works in Source or Object form.
|
68
|
+
|
69
|
+
3. Grant of Patent License.
|
70
|
+
|
71
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
72
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
73
|
+
irrevocable (except as stated in this section) patent license to make, have
|
74
|
+
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
75
|
+
such license applies only to those patent claims licensable by such Contributor
|
76
|
+
that are necessarily infringed by their Contribution(s) alone or by combination
|
77
|
+
of their Contribution(s) with the Work to which such Contribution(s) was
|
78
|
+
submitted. If You institute patent litigation against any entity (including a
|
79
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
80
|
+
Contribution incorporated within the Work constitutes direct or contributory
|
81
|
+
patent infringement, then any patent licenses granted to You under this License
|
82
|
+
for that Work shall terminate as of the date such litigation is filed.
|
83
|
+
|
84
|
+
4. Redistribution.
|
85
|
+
|
86
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
87
|
+
in any medium, with or without modifications, and in Source or Object form,
|
88
|
+
provided that You meet the following conditions:
|
89
|
+
|
90
|
+
You must give any other recipients of the Work or Derivative Works a copy of
|
91
|
+
this License; and
|
92
|
+
You must cause any modified files to carry prominent notices stating that You
|
93
|
+
changed the files; and
|
94
|
+
You must retain, in the Source form of any Derivative Works that You distribute,
|
95
|
+
all copyright, patent, trademark, and attribution notices from the Source form
|
96
|
+
of the Work, excluding those notices that do not pertain to any part of the
|
97
|
+
Derivative Works; and
|
98
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any
|
99
|
+
Derivative Works that You distribute must include a readable copy of the
|
100
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
101
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
102
|
+
following places: within a NOTICE text file distributed as part of the
|
103
|
+
Derivative Works; within the Source form or documentation, if provided along
|
104
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
105
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
106
|
+
the NOTICE file are for informational purposes only and do not modify the
|
107
|
+
License. You may add Your own attribution notices within Derivative Works that
|
108
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
109
|
+
provided that such additional attribution notices cannot be construed as
|
110
|
+
modifying the License.
|
111
|
+
You may add Your own copyright statement to Your modifications and may provide
|
112
|
+
additional or different license terms and conditions for use, reproduction, or
|
113
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
114
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
115
|
+
with the conditions stated in this License.
|
116
|
+
|
117
|
+
5. Submission of Contributions.
|
118
|
+
|
119
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
120
|
+
for inclusion in the Work by You to the Licensor shall be under the terms and
|
121
|
+
conditions of this License, without any additional terms or conditions.
|
122
|
+
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
123
|
+
any separate license agreement you may have executed with Licensor regarding
|
124
|
+
such Contributions.
|
125
|
+
|
126
|
+
6. Trademarks.
|
127
|
+
|
128
|
+
This License does not grant permission to use the trade names, trademarks,
|
129
|
+
service marks, or product names of the Licensor, except as required for
|
130
|
+
reasonable and customary use in describing the origin of the Work and
|
131
|
+
reproducing the content of the NOTICE file.
|
132
|
+
|
133
|
+
7. Disclaimer of Warranty.
|
134
|
+
|
135
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the
|
136
|
+
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
|
137
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
138
|
+
including, without limitation, any warranties or conditions of TITLE,
|
139
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
140
|
+
solely responsible for determining the appropriateness of using or
|
141
|
+
redistributing the Work and assume any risks associated with Your exercise of
|
142
|
+
permissions under this License.
|
143
|
+
|
144
|
+
8. Limitation of Liability.
|
145
|
+
|
146
|
+
In no event and under no legal theory, whether in tort (including negligence),
|
147
|
+
contract, or otherwise, unless required by applicable law (such as deliberate
|
148
|
+
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
149
|
+
liable to You for damages, including any direct, indirect, special, incidental,
|
150
|
+
or consequential damages of any character arising as a result of this License or
|
151
|
+
out of the use or inability to use the Work (including but not limited to
|
152
|
+
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
153
|
+
any and all other commercial damages or losses), even if such Contributor has
|
154
|
+
been advised of the possibility of such damages.
|
155
|
+
|
156
|
+
9. Accepting Warranty or Additional Liability.
|
157
|
+
|
158
|
+
While redistributing the Work or Derivative Works thereof, You may choose to
|
159
|
+
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
160
|
+
other liability obligations and/or rights consistent with this License. However,
|
161
|
+
in accepting such obligations, You may act only on Your own behalf and on Your
|
162
|
+
sole responsibility, not on behalf of any other Contributor, and only if You
|
163
|
+
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
164
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
165
|
+
accepting any such warranty or additional liability.
|
166
|
+
|
167
|
+
END OF TERMS AND CONDITIONS
|
168
|
+
|
169
|
+
APPENDIX: How to apply the Apache License to your work
|
170
|
+
|
171
|
+
To apply the Apache License to your work, attach the following boilerplate
|
172
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
173
|
+
identifying information. (Don't include the brackets!) The text should be
|
174
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
175
|
+
recommend that a file or class name and description of purpose be included on
|
176
|
+
the same "printed page" as the copyright notice for easier identification within
|
177
|
+
third-party archives.
|
178
|
+
|
179
|
+
Copyright [yyyy] [name of copyright owner]
|
180
|
+
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
182
|
+
you may not use this file except in compliance with the License.
|
183
|
+
You may obtain a copy of the License at
|
184
|
+
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
186
|
+
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
190
|
+
See the License for the specific language governing permissions and
|
191
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
![ProjectLogo](docs/project_logo.png)
|
2
|
+
|
3
|
+
### What's it about?
|
4
|
+
It's a framework for developing servers that adhere to the [Restful Objects specification](http://restfulobjects.org/). It's main purpose is to be completely transparent to the developer and let him focus mainly on the Domain Model that implements the business logic, thereby abstracting him from the API & HTTP issues of exposing the Restful resources.
|
5
|
+
|
6
|
+
So it's like plain old ruby objects, with HTTP Restful steroids for free!
|
7
|
+
|
8
|
+
### How it looks like?
|
9
|
+
```ruby
|
10
|
+
require 'restful_objects'
|
11
|
+
|
12
|
+
class Node
|
13
|
+
include RestfulObjects::Object
|
14
|
+
|
15
|
+
property :label, :string
|
16
|
+
property :visited, :bool
|
17
|
+
collection :dendencies, Node
|
18
|
+
end
|
19
|
+
|
20
|
+
class Graph
|
21
|
+
include RestfulObjects::Object
|
22
|
+
|
23
|
+
collection :nodes, Nodo
|
24
|
+
action :topological_sort, return_value: { list: Nodo }
|
25
|
+
|
26
|
+
def topological_sort
|
27
|
+
list = []
|
28
|
+
nodex.each { |node| node.visited = false }
|
29
|
+
unvisited_nodes = nodes
|
30
|
+
while not unvisited_nodes.empty? do
|
31
|
+
visit(unvisited_nodes.pop, list)
|
32
|
+
end
|
33
|
+
list
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def visit(node, list)
|
39
|
+
if !node.visited
|
40
|
+
node.dependencies.each { |dependent_node| visit(dependent_node, list) }
|
41
|
+
node.visited = true
|
42
|
+
list << node
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### How to install it?
|
49
|
+
Run
|
50
|
+
|
51
|
+
```shell
|
52
|
+
gem install restful_objects
|
53
|
+
```
|
54
|
+
|
55
|
+
or add
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
gem 'restful_objects'
|
59
|
+
```
|
60
|
+
|
61
|
+
to your's project Gemfile and 'bundle install' it.
|
62
|
+
|
63
|
+
### Project Status
|
64
|
+
The source quality is at alpha state, and it was created mostly as a proof of concept. Still is has plenty of specs and a big percentage of the specification implemented, the grey areas are mostly related to errors and validations. It has been developed with MRI and hasn't been tested on other Ruby implementations.
|
65
|
+
|
66
|
+
### Dependencies
|
67
|
+
- [Ruby 1.9.3 or higher](https://www.ruby-lang.org/)
|
68
|
+
- [Sinatra](http://www.sinatrarb.com/)
|
69
|
+
- [RSpec](http://rspec.info/)
|
70
|
+
- [json_expressions](https://github.com/chancancode/json_expressions) for json testing.
|
71
|
+
|
72
|
+
### Resources
|
73
|
+
- [Restful Objects Spec](http://restfulobjects.org/)
|
74
|
+
- [Introduction to Restful Objects](http://www.infoq.com/articles/Intro_Restful_Objects)
|
75
|
+
|
76
|
+
### License
|
77
|
+
MIT License.
|
78
|
+
|
79
|
+
### Credits
|
80
|
+
This project has been developed as the main subject of Pablo Vizcay undergradute System's Engineering thesis, directed by [Dr. Alejandro Zunino](http://azunino.sites.exa.unicen.edu.ar/) for the [U.N.I.C.E.N. University in Tandil - Buenos Aires - Argentina](http://www.exa.unicen.edu.ar/).
|
81
|
+
|
82
|
+
![UnicenLogo](docs/unicen_logo.png)
|
83
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'restful_objects'
|
2
|
+
|
3
|
+
if ARGV.first.nil? then
|
4
|
+
puts 'You must provide the script path to load: "restful_server script.rb"'
|
5
|
+
exit(1)
|
6
|
+
end
|
7
|
+
|
8
|
+
load ARGV.first
|
9
|
+
|
10
|
+
RestfulObjects::DomainModel.current.base_url = 'http://localhost:4567'
|
11
|
+
RestfulObjects::Server.run!
|
12
|
+
|
File without changes
|
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'base64'
|
1
|
+
# encoding: utf-8
|
3
2
|
|
4
3
|
module RestfulObjects
|
5
4
|
module Object
|
@@ -17,116 +16,5 @@ module RestfulObjects
|
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
20
|
-
|
21
|
-
module ObjectBase
|
22
|
-
attr_accessor :is_service, :title
|
23
|
-
|
24
|
-
def initialize
|
25
|
-
super
|
26
|
-
@deleted = false
|
27
|
-
@is_service = self.class.ancestors.include? RestfulObjects::Service
|
28
|
-
@title = "#{self.class.name} (#{object_id})"
|
29
|
-
rs_register_in_model
|
30
|
-
rs_type.collections.each_value { |collection| instance_variable_set "@#{collection.id}".to_sym, Array.new }
|
31
|
-
on_after_create if respond_to? :on_after_create
|
32
|
-
end
|
33
|
-
|
34
|
-
def rs_register_in_model
|
35
|
-
rs_model.objects.register(self) if not @is_service
|
36
|
-
end
|
37
|
-
|
38
|
-
def rs_model
|
39
|
-
RestfulObjects::DomainModel.current
|
40
|
-
end
|
41
|
-
|
42
|
-
def rs_type
|
43
|
-
rs_model.types[self.class.name]
|
44
|
-
end
|
45
|
-
|
46
|
-
def get_representation
|
47
|
-
HttpResponse.new(representation.to_json,
|
48
|
-
'application/json;profile="urn:org.restfulobjects:repr-types/object";x-ro-domain-type="' + rs_type.id + '"')
|
49
|
-
end
|
50
|
-
|
51
|
-
def rs_instance_id
|
52
|
-
object_id
|
53
|
-
end
|
54
|
-
|
55
|
-
def representation
|
56
|
-
representation = {
|
57
|
-
'title' => title,
|
58
|
-
'members' => generate_members,
|
59
|
-
'links' => [ link_to(:described_by, "/domain-types/#{self.class.name}", :domain_type) ],
|
60
|
-
'extensions' => rs_type.metadata
|
61
|
-
}
|
62
|
-
|
63
|
-
if not is_service
|
64
|
-
representation['instanceId'] = object_id.to_s
|
65
|
-
representation['links'] << link_to(:self, "/objects/#{self.class.name}/#{object_id}", :object)
|
66
|
-
else
|
67
|
-
representation['serviceId'] = self.class.name
|
68
|
-
representation['links'] << link_to(:self, "/services/#{self.class.name}", :object)
|
69
|
-
end
|
70
|
-
|
71
|
-
representation
|
72
|
-
end
|
73
|
-
|
74
|
-
def generate_members
|
75
|
-
if is_service
|
76
|
-
actions_members
|
77
|
-
else
|
78
|
-
properties_members.merge(collections_members.merge(actions_members))
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def rs_delete
|
83
|
-
on_before_delete if respond_to? :on_before_delete
|
84
|
-
@deleted = true
|
85
|
-
on_after_delete if respond_to? :on_after_delete
|
86
|
-
end
|
87
|
-
|
88
|
-
def deleted?
|
89
|
-
@deleted
|
90
|
-
end
|
91
|
-
|
92
|
-
def encode_value(value, type)
|
93
|
-
return nil if value.nil?
|
94
|
-
case type
|
95
|
-
when :string
|
96
|
-
value.to_s
|
97
|
-
when :int
|
98
|
-
value.to_i
|
99
|
-
when :decimal
|
100
|
-
value.to_f
|
101
|
-
when :date
|
102
|
-
value.strftime('%Y-%m-%d')
|
103
|
-
when :blob
|
104
|
-
Base64.encode64(value).strip
|
105
|
-
else
|
106
|
-
raise "encode_value unsupported property type: #{type}"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def decode_value(value, type)
|
111
|
-
return nil if value.nil?
|
112
|
-
case type
|
113
|
-
when :string
|
114
|
-
value.to_s
|
115
|
-
when :int
|
116
|
-
value.to_i
|
117
|
-
when :decimal
|
118
|
-
Float(value)
|
119
|
-
when :date
|
120
|
-
Date.parse(value)
|
121
|
-
when :blob
|
122
|
-
Base64.decode64(value)
|
123
|
-
else
|
124
|
-
raise "decode_value unsupported property type: #{type}"
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def get_self_link
|
129
|
-
link_to(:self, "/objects/#{self.class.name}/#{self.object_id}", :object)
|
130
|
-
end
|
131
|
-
end
|
132
19
|
end
|
20
|
+
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module RestfulObjects
|
4
|
+
module ObjectBase
|
5
|
+
attr_accessor :is_service, :title
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
@deleted = false
|
10
|
+
@is_service = self.class.ancestors.include? RestfulObjects::Service
|
11
|
+
@title = "#{self.class.name} (#{object_id})"
|
12
|
+
rs_register_in_model
|
13
|
+
rs_type.collections.each_value { |collection| instance_variable_set "@#{collection.id}".to_sym, Array.new }
|
14
|
+
on_after_create if respond_to?(:on_after_create)
|
15
|
+
end
|
16
|
+
|
17
|
+
def rs_register_in_model
|
18
|
+
rs_model.objects.register(self) unless @is_service
|
19
|
+
end
|
20
|
+
|
21
|
+
def rs_model
|
22
|
+
RestfulObjects::DomainModel.current
|
23
|
+
end
|
24
|
+
|
25
|
+
def rs_type
|
26
|
+
rs_model.types[self.class.name]
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_representation
|
30
|
+
HttpResponse.new(representation.to_json,
|
31
|
+
'application/json;profile="urn:org.restfulobjects:repr-types/object";x-ro-domain-type="' + rs_type.id + '"')
|
32
|
+
end
|
33
|
+
|
34
|
+
def rs_instance_id
|
35
|
+
object_id
|
36
|
+
end
|
37
|
+
|
38
|
+
def representation
|
39
|
+
representation = {
|
40
|
+
'title' => title,
|
41
|
+
'members' => generate_members,
|
42
|
+
'links' => [ link_to(:described_by, "/domain-types/#{self.class.name}", :domain_type) ],
|
43
|
+
'extensions' => rs_type.metadata
|
44
|
+
}
|
45
|
+
|
46
|
+
if not is_service
|
47
|
+
representation['instanceId'] = object_id.to_s
|
48
|
+
representation['links'] << link_to(:self, "/objects/#{self.class.name}/#{object_id}", :object)
|
49
|
+
else
|
50
|
+
representation['serviceId'] = self.class.name
|
51
|
+
representation['links'] << link_to(:self, "/services/#{self.class.name}", :object)
|
52
|
+
end
|
53
|
+
|
54
|
+
representation
|
55
|
+
end
|
56
|
+
|
57
|
+
def generate_members
|
58
|
+
if is_service
|
59
|
+
actions_members
|
60
|
+
else
|
61
|
+
properties_members.merge(collections_members.merge(actions_members))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def rs_delete
|
66
|
+
on_before_delete if respond_to?(:on_before_delete)
|
67
|
+
@deleted = true
|
68
|
+
on_after_delete if respond_to?(:on_after_delete)
|
69
|
+
end
|
70
|
+
|
71
|
+
def deleted?
|
72
|
+
@deleted
|
73
|
+
end
|
74
|
+
|
75
|
+
def encode_value(value, type)
|
76
|
+
return nil if value.nil?
|
77
|
+
case type
|
78
|
+
when :string
|
79
|
+
value.to_s
|
80
|
+
when :int
|
81
|
+
value.to_i
|
82
|
+
when :decimal
|
83
|
+
value.to_f
|
84
|
+
when :date
|
85
|
+
value.strftime('%Y-%m-%d')
|
86
|
+
when :blob
|
87
|
+
Base64.encode64(value).strip
|
88
|
+
else
|
89
|
+
raise "encode_value unsupported property type: #{type}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def decode_value(value, type)
|
94
|
+
return nil if value.nil?
|
95
|
+
case type
|
96
|
+
when :string
|
97
|
+
value.to_s
|
98
|
+
when :int
|
99
|
+
value.to_i
|
100
|
+
when :decimal
|
101
|
+
Float(value)
|
102
|
+
when :date
|
103
|
+
Date.parse(value)
|
104
|
+
when :blob
|
105
|
+
Base64.decode64(value)
|
106
|
+
else
|
107
|
+
raise "decode_value unsupported property type: #{type}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def get_self_link
|
112
|
+
link_to(:self, "/objects/#{self.class.name}/#{self.object_id}", :object)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module RestfulObjects
|
2
|
+
class PropertyDescription
|
3
|
+
include LinkGenerator
|
4
|
+
|
5
|
+
attr_accessor :id, :domain_type, :return_type, :friendly_name, :description, :optional, :read_only, :member_order,
|
6
|
+
:max_length, :disabled_reason, :pattern
|
7
|
+
|
8
|
+
def initialize(id, domain_type, return_type, options)
|
9
|
+
raise "property type #{return_type} usupported" if not [:string, :int, :decimal, :date, :blob].include?(return_type)
|
10
|
+
|
11
|
+
@id = id
|
12
|
+
@domain_type = domain_type
|
13
|
+
@return_type = return_type
|
14
|
+
@friendly_name = options[:friendly_name] || id
|
15
|
+
@description = options[:description] || id
|
16
|
+
@optional = options[:optional].nil? ? true : options[:optional]
|
17
|
+
@read_only = options[:read_only].nil? ? false : options[:read_only]
|
18
|
+
@member_order = options[:member_order] || 1
|
19
|
+
@max_length = options[:max_length]
|
20
|
+
@disabled_reason = options[:disabled_reason] || 'read-only property' if read_only
|
21
|
+
@pattern = options[:pattern]
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_representation
|
25
|
+
representation = {
|
26
|
+
'id' => @id,
|
27
|
+
'optional' => optional,
|
28
|
+
'memberOrder' => @member_order,
|
29
|
+
'links' => [
|
30
|
+
link_to(:self, "/domain-types/#{@domain_type}/properties/#{@id}", :property_description),
|
31
|
+
link_to(:up, "/domain-types/#{@domain_type}", :domain_type),
|
32
|
+
link_to(:return_type, "/domain-types/#{@return_type}", :domain_type)
|
33
|
+
],
|
34
|
+
'extensions' => {}
|
35
|
+
}
|
36
|
+
|
37
|
+
representation['friendlyName'] = friendly_name if friendly_name
|
38
|
+
representation['description'] = description if description
|
39
|
+
|
40
|
+
representation.to_json
|
41
|
+
end
|
42
|
+
|
43
|
+
def metadata
|
44
|
+
result = { 'friendlyName' => friendly_name,
|
45
|
+
'description' => description,
|
46
|
+
'returnType' => return_type,
|
47
|
+
'format' => format,
|
48
|
+
'optional' => optional,
|
49
|
+
'memberOrder' => member_order }
|
50
|
+
result['maxLength'] = max_length if max_length
|
51
|
+
result['pattern'] = pattern if pattern
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def format
|
56
|
+
case return_type
|
57
|
+
when :string
|
58
|
+
'string'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_value_as_json
|
63
|
+
{ 'id' =>
|
64
|
+
{ 'value' => '' }
|
65
|
+
}.to_json
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RestfulObjects
|
2
|
+
module Service
|
3
|
+
include LinkGenerator
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
RestfulObjects::DomainModel.current.types.add(base.name)
|
7
|
+
|
8
|
+
base.class_eval do
|
9
|
+
extend ObjectMacros
|
10
|
+
include ObjectBase
|
11
|
+
include ObjectActions
|
12
|
+
|
13
|
+
def rs_register_in_model
|
14
|
+
# do_nothing
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
RestfulObjects::DomainModel.current.services.register(base)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'json'
|
3
|
+
require 'base64'
|
4
|
+
require 'singleton'
|
5
|
+
require 'bigdecimal'
|
6
|
+
require 'forwardable'
|
7
|
+
require 'sinatra/base'
|
8
|
+
|
9
|
+
require_relative "restful_objects/version"
|
10
|
+
require_relative 'restful_objects/server'
|
11
|
+
require_relative 'restful_objects/link_generator'
|
12
|
+
require_relative 'restful_objects/http_response'
|
13
|
+
require_relative 'restful_objects/user'
|
14
|
+
require_relative 'restful_objects/property_list'
|
15
|
+
require_relative 'restful_objects/property_description'
|
16
|
+
require_relative 'restful_objects/collection_list'
|
17
|
+
require_relative 'restful_objects/collection_description'
|
18
|
+
require_relative 'restful_objects/action_list'
|
19
|
+
require_relative 'restful_objects/action_description'
|
20
|
+
require_relative 'restful_objects/parameter_description'
|
21
|
+
require_relative 'restful_objects/parameter_description_list'
|
22
|
+
require_relative 'restful_objects/object_list'
|
23
|
+
require_relative 'restful_objects/object'
|
24
|
+
require_relative 'restful_objects/object_base'
|
25
|
+
require_relative 'restful_objects/object_macros'
|
26
|
+
require_relative 'restful_objects/object_properties'
|
27
|
+
require_relative 'restful_objects/object_collections'
|
28
|
+
require_relative 'restful_objects/object_actions'
|
29
|
+
require_relative 'restful_objects/type'
|
30
|
+
require_relative 'restful_objects/type_list'
|
31
|
+
require_relative 'restful_objects/service'
|
32
|
+
require_relative 'restful_objects/service_list'
|
33
|
+
require_relative 'restful_objects/domain_model'
|
34
|
+
|