representors 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +126 -0
  5. data/LICENSE.md +19 -0
  6. data/README.md +28 -0
  7. data/Rakefile +10 -0
  8. data/lib/representor_support/utilities.rb +39 -0
  9. data/lib/representors.rb +5 -0
  10. data/lib/representors/errors.rb +7 -0
  11. data/lib/representors/field.rb +108 -0
  12. data/lib/representors/options.rb +67 -0
  13. data/lib/representors/representor.rb +161 -0
  14. data/lib/representors/representor_builder.rb +64 -0
  15. data/lib/representors/representor_hash.rb +59 -0
  16. data/lib/representors/serialization.rb +4 -0
  17. data/lib/representors/serialization/deserializer_base.rb +29 -0
  18. data/lib/representors/serialization/deserializer_factory.rb +13 -0
  19. data/lib/representors/serialization/hal_deserializer.rb +44 -0
  20. data/lib/representors/serialization/hal_serializer.rb +91 -0
  21. data/lib/representors/serialization/hale_deserializer.rb +162 -0
  22. data/lib/representors/serialization/hale_serializer.rb +110 -0
  23. data/lib/representors/serialization/serialization_base.rb +27 -0
  24. data/lib/representors/serialization/serialization_factory_base.rb +54 -0
  25. data/lib/representors/serialization/serializer_base.rb +20 -0
  26. data/lib/representors/serialization/serializer_factory.rb +17 -0
  27. data/lib/representors/transition.rb +130 -0
  28. data/lib/representors/version.rb +4 -0
  29. data/spec/fixtures/complex_hal.json +92 -0
  30. data/spec/fixtures/complex_hale_document.json +81 -0
  31. data/spec/fixtures/drds_hash.rb +120 -0
  32. data/spec/fixtures/hale_spec_examples/basic.json +77 -0
  33. data/spec/fixtures/hale_spec_examples/complex_reference_objects.json +157 -0
  34. data/spec/fixtures/hale_spec_examples/data.json +17 -0
  35. data/spec/fixtures/hale_spec_examples/data_objects.json +96 -0
  36. data/spec/fixtures/hale_spec_examples/link_objects.json +18 -0
  37. data/spec/fixtures/hale_spec_examples/nested_ref.json +43 -0
  38. data/spec/fixtures/hale_spec_examples/reference_objects.json +89 -0
  39. data/spec/fixtures/hale_tutorial_examples/basic_links.json +85 -0
  40. data/spec/fixtures/hale_tutorial_examples/basic_links_with_orders.json +96 -0
  41. data/spec/fixtures/hale_tutorial_examples/basic_links_with_references.json +108 -0
  42. data/spec/fixtures/hale_tutorial_examples/embedded.json +182 -0
  43. data/spec/fixtures/hale_tutorial_examples/empty.json +1 -0
  44. data/spec/fixtures/hale_tutorial_examples/enctype.json +14 -0
  45. data/spec/fixtures/hale_tutorial_examples/final.json +141 -0
  46. data/spec/fixtures/hale_tutorial_examples/get_link.json +17 -0
  47. data/spec/fixtures/hale_tutorial_examples/get_link_with_data.json +29 -0
  48. data/spec/fixtures/hale_tutorial_examples/links.json +11 -0
  49. data/spec/fixtures/hale_tutorial_examples/links_only.json +3 -0
  50. data/spec/fixtures/hale_tutorial_examples/meta.json +208 -0
  51. data/spec/fixtures/hale_tutorial_examples/self_link.json +7 -0
  52. data/spec/fixtures/single_drd.rb +266 -0
  53. data/spec/lib/representors/complex_representor_spec.rb +288 -0
  54. data/spec/lib/representors/field_spec.rb +141 -0
  55. data/spec/lib/representors/representor_builder_spec.rb +223 -0
  56. data/spec/lib/representors/representor_spec.rb +285 -0
  57. data/spec/lib/representors/serialization/deserializer_factory_spec.rb +118 -0
  58. data/spec/lib/representors/serialization/hal_deserializer_spec.rb +34 -0
  59. data/spec/lib/representors/serialization/hal_serializer_spec.rb +171 -0
  60. data/spec/lib/representors/serialization/hale_deserializer_spec.rb +59 -0
  61. data/spec/lib/representors/serialization/hale_roundtrip_spec.rb +34 -0
  62. data/spec/lib/representors/serialization/hale_serializer_spec.rb +659 -0
  63. data/spec/lib/representors/serialization/serializer_factory_spec.rb +108 -0
  64. data/spec/lib/representors/transition_spec.rb +349 -0
  65. data/spec/spec_helper.rb +32 -0
  66. data/spec/support/basic-hale.json +12 -0
  67. data/spec/support/hal_representor_shared.rb +206 -0
  68. data/spec/support/helpers.rb +8 -0
  69. data/tasks/benchmark.rake +75 -0
  70. data/tasks/complex_hal_document.json +98 -0
  71. data/tasks/test_specs.rake +37 -0
  72. data/tasks/yard.rake +22 -0
  73. metadata +232 -0
@@ -0,0 +1,4 @@
1
+ # Used to prevent the class/module from being loaded more than once
2
+ module Representors
3
+ VERSION = '0.0.5'
4
+ end
@@ -0,0 +1,92 @@
1
+ {
2
+ "total_count": 2,
3
+ "_links": {
4
+ "self": {
5
+ "href": "http://example.com/drds"
6
+ },
7
+ "list": {
8
+ "href": "http://example.com/drds/list",
9
+ "profile": "http://alps.io/schema.org/DRDs"
10
+ },
11
+ "search": {
12
+ "href": "http://example.com/drds/search{?search_term,name}",
13
+ "templated": true
14
+ },
15
+ "create": {
16
+ "href": "www.example.com/drds/create"
17
+ },
18
+ "items": {
19
+ "href": "www.example.com/drds/show/173875983789",
20
+ "profile": "http://alps.io/schema.org/DRD"
21
+ },
22
+ "profile": {
23
+ "href": "http://www.example.com/drds/show/DRDs"
24
+ },
25
+ "help": {
26
+ "href": "http://alps.io/schema.org/DRDs"
27
+ }
28
+ },
29
+ "_embedded": {
30
+ "items": [
31
+ {
32
+ "uuid": "007d8e12-babd-4f2c-b01e-8b5e2f749e1z",
33
+ "name": "drdname",
34
+ "status": "compliant",
35
+ "old_status": "compliant",
36
+ "kind": "nonstandard",
37
+ "size": "medium",
38
+ "leviathan_uuid": "1234-9abc-5678-def1",
39
+ "built_at": "2013-03-20T00:00:00+00:01",
40
+ "leviathan": "http://alps.io/schema.org/Thing/Leviathan",
41
+ "location": [
42
+ "moya",
43
+ "japan"
44
+ ],
45
+ "location_detail": "inside",
46
+ "leviathan_email": "joe@grumpycat.org",
47
+ "destroyed": "destroyed",
48
+ "leviathan_health_points": 32,
49
+ "term": "blunderbus",
50
+ "_links": {
51
+ "self": {
52
+ "href": "www.example.com/drds/show/173875983789"
53
+ },
54
+ "show": {
55
+ "href": "www.example.com/drds/show/173875983789",
56
+ "profile": "http://alps.io/schema.org/DRD"
57
+ },
58
+ "leviathan-link": {
59
+ "href": "http://example.com/drds/Leviathan",
60
+ "name": "leviathan",
61
+ "profile": "http://alps.io/schema.org/Leviathan#leviathan"
62
+ },
63
+ "repair-history": {
64
+ "href": "http://example.com/drds/Repairs",
65
+ "profile": "http://alps.io/schema.org/Repairs#history"
66
+ },
67
+ "activate": {
68
+ "href": "http://example.com/drds/activate/173875983789",
69
+ "profile": "http://alps.io/schema.org/DRD"
70
+ },
71
+ "deactivate": {
72
+ "href": "http://example.com/drds/show/173875983789",
73
+ "profile": "http://alps.io/schema.org/DRD"
74
+ },
75
+ "delete": {
76
+ "href": "http://example.com/drds/show/173875983789"
77
+ },
78
+ "update": {
79
+ "href": "http://example.com/drds/show/173875983789",
80
+ "profile": "http://alps.io/schema.org/DRDs#update"
81
+ },
82
+ "profile": {
83
+ "href": "http://www.example.com/drds/show/DRD"
84
+ },
85
+ "help": {
86
+ "href": "http://alps.io/schema.org/DRD"
87
+ }
88
+ }
89
+ }
90
+ ]
91
+ }
92
+ }
@@ -0,0 +1,81 @@
1
+ {
2
+ "_meta": {
3
+ "lookup": {
4
+ "send_info": { "options": [ "yes", "no", "maybe" ], "in": true }
5
+ },
6
+ "edit_form": {
7
+ "method": "PUT",
8
+ "enctype": "application/json",
9
+ "render": "resource",
10
+ "data": {
11
+ "name": { "type": "string", "required": true },
12
+ "send_info": { "options": [ "yes", "no", "maybe" ], "in": true },
13
+ "user_id": { "scope": "href", "required": true }
14
+ }
15
+ }
16
+ },
17
+ "_links": {
18
+ "self": { "href": "..." },
19
+ "search": {
20
+ "href": ".../{?send_info}",
21
+ "templated": true,
22
+ "method": "GET",
23
+ "data": {
24
+ "send_info": { "scope": "href", "options": [ "yes", "no", "maybe" ], "in": true, "max_length": 40 }
25
+ }
26
+ },
27
+ "agent": { "href": "/agent/1", "method": "GET", "render": "embed" },
28
+ "customer": [
29
+ { "href": "/customer/1", "method": "GET" },
30
+ { "href": "/customer/2", "method": "GET" }
31
+ ]
32
+ },
33
+ "_embedded": {
34
+ "agent": {
35
+ "_links": {
36
+ "self": { "href": "/agent/1", "method": "GET" }
37
+ },
38
+ "name": "Mike"
39
+ },
40
+ "customer": [
41
+ {
42
+ "_links": {
43
+ "self": { "href": "/customer/1", "method": "GET" },
44
+ "edit": {
45
+ "href": ".../{?user_id}",
46
+ "method": "PUT",
47
+ "enctype": "application/json",
48
+ "templated": true,
49
+ "render": "resource",
50
+ "data": {
51
+ "name": { "type": "string", "required": true, "value": "Tom" },
52
+ "send_info": { "options": [ "yes", "no", "maybe" ], "in": true, "value": "yes", "max_length": 40 },
53
+ "user_id": { "scope": "href", "required": true }
54
+ }
55
+ }
56
+ },
57
+ "name": "Tom",
58
+ "send_info": "yes"
59
+ },
60
+ {
61
+ "_links": {
62
+ "self": { "href": "/customer/2", "method": "GET" },
63
+ "edit": {
64
+ "href": ".../{?user_id}",
65
+ "method": "PUT",
66
+ "enctype": "application/json",
67
+ "render": "resource",
68
+ "templated": true,
69
+ "data": {
70
+ "name": { "type": "string", "required": true, "value": "Harry" },
71
+ "send_info": { "options": [ "yes", "no", "maybe" ], "in": true, "value": "no" },
72
+ "user_id": { "scope": "href", "required": true }
73
+ }
74
+ }
75
+ },
76
+ "name": "Harry",
77
+ "send_info": "no"
78
+ }
79
+ ]
80
+ }
81
+ }
@@ -0,0 +1,120 @@
1
+ module ComplexRepresentor
2
+ DRDS_HASH = {
3
+ href: 'www.example.com/drds',
4
+ id: 'DRDs',
5
+ protocol: 'http',
6
+ doc: 'Describes the semantics, states and state transitions associated with DRDs.',
7
+ links: {
8
+ profile: 'http://www.example.com/drds/show/DRDs',
9
+ help: 'http://alps.io/schema.org/DRDs'
10
+ },
11
+ attributes: {
12
+ "total_count" => {
13
+ doc: 'The total count of DRDs.',
14
+ type: 'semantic',
15
+ profile: 'http://alps.io/schema.org/Integer',
16
+ sample: 1,
17
+ value: 2,
18
+ },
19
+ },
20
+ transitions: [
21
+ {
22
+ rel: 'self',
23
+ href: 'http://example.com/drds',
24
+ method: 'GET',
25
+ },
26
+ {
27
+ rel: 'list',
28
+ doc: 'Returns a list of DRDs.',
29
+ profile: 'http://alps.io/schema.org/DRDs',
30
+ href: 'http://example.com/drds/list',
31
+ },
32
+ {
33
+ rel: 'search',
34
+ doc: 'Returns a list of DRDs that satisfy the search term.',
35
+ href: 'http://example.com/drds/search{?search_term,name}',
36
+ rt: 'drds',
37
+ descriptors: {
38
+ search_term: {
39
+ doc: 'The terms to search.',
40
+ profile: 'http://alps.io/schema.org/Text',
41
+ sample: 'searchterm',
42
+ multiple: true,
43
+ scope: 'href',
44
+ },
45
+ name: {
46
+ doc: 'The name of the DRD.',
47
+ profile: 'http://alps.io/schema.org/Text',
48
+ sample: 'drdname',
49
+ value: 'drdname',
50
+ scope: 'href',
51
+ field_type: 'text',
52
+ },
53
+ },
54
+ },
55
+ {
56
+ rel: 'create',
57
+ doc: 'Creates a DRD.',
58
+ rt: 'drd',
59
+ links: {
60
+ profile: 'http://alps.io/schema.org/DRDs#update',
61
+ help: 'http://help.example.com/Forms/update',
62
+ },
63
+ href: 'www.example.com/drds/create',
64
+ method: 'POST',
65
+ descriptors: {
66
+ name: {
67
+ doc: 'The name of the DRD.',
68
+ profile: 'http://alps.io/schema.org/Text',
69
+ sample: 'drdname',
70
+ field_type: 'text', #
71
+ validators: [
72
+ 'required',
73
+ {maxlength: 50},
74
+ ],
75
+ },
76
+ leviathan: {
77
+ doc: 'The associated Leviathan resource.',
78
+ profile: 'http://alps.io/schema.org/Thing/Leviathan',
79
+ sample: 'http://alps.io/schema.org/Thing/Leviathan',
80
+ value: 'http://alps.io/schema.org/Thing/Leviathan',
81
+ type: 'object', #TODO Embedded Objects
82
+ descriptors: {
83
+ leviathan_uuid: {
84
+ doc: 'The UUID of the creator Leviathan.',
85
+ profile: 'http://alps.io/schema.org/Text',
86
+ sample: '007d8e12-babd-4f2c-b01e-8b5e2f749e1b',
87
+ type: 'text',
88
+ field_type: 'text',
89
+ },
90
+ leviathan_health_points: {
91
+ doc: 'The health points of Leviathan.',
92
+ profile: 'http://alps.io/schema.org/Integer',
93
+ type: 'integer',
94
+ field_type: 'number',
95
+ validators: [
96
+ 'required',
97
+ {min: 0},
98
+ {max: 100},
99
+ ],
100
+ sample: 42,
101
+ },
102
+ leviathan_email: {
103
+ doc: 'The Leviathan respond to email.',
104
+ profile: 'http://alps.io/schema.org/Text',
105
+ sample: 'joe@grumpycat.org',
106
+ type: 'text',
107
+ field_type: 'email',
108
+ validators: [
109
+ 'required',
110
+ {pattern: "^.+@.+$"}
111
+ ]
112
+ },
113
+ },
114
+ },
115
+ },
116
+ },
117
+ ],
118
+ #To put embedded items in the hash please see: single_drd.rb
119
+ }
120
+ end
@@ -0,0 +1,77 @@
1
+ {
2
+ "_meta": {
3
+ "any": {
4
+ "json": "object"
5
+ }
6
+ },
7
+ "_links": {
8
+ "self": {
9
+ "href": "..."
10
+ },
11
+ "search": {
12
+ "href": ".../{?send_info}",
13
+ "templated": true,
14
+ "method": "GET",
15
+ "data": {
16
+ "send_info": {
17
+ "options": [
18
+ "yes",
19
+ "no",
20
+ "maybe"
21
+ ],
22
+ "in": true
23
+ }
24
+ }
25
+ },
26
+ "agent": {
27
+ "href": "/agent/1",
28
+ "method": "GET",
29
+ "render": "embed"
30
+ },
31
+ "customer": [
32
+ {
33
+ "href": "/customer/1",
34
+ "method": "GET"
35
+ }
36
+ ]
37
+ },
38
+ "_embedded": {
39
+ "customer": [
40
+ {
41
+ "_links": {
42
+ "self": {
43
+ "href": "/customer/1",
44
+ "method": "GET"
45
+ },
46
+ "edit": {
47
+ "href": ".../{?user_id}",
48
+ "method": "PUT",
49
+ "request_encoding": "application/json",
50
+ "render": "resource",
51
+ "data": {
52
+ "name": {
53
+ "type": "string",
54
+ "required": true
55
+ },
56
+ "send_info": {
57
+ "options": [
58
+ "yes",
59
+ "no",
60
+ "maybe"
61
+ ],
62
+ "in": true
63
+ },
64
+ "user_id": {
65
+ "scope": "href",
66
+ "required": true
67
+ }
68
+ }
69
+ }
70
+ },
71
+ "name": "Tom",
72
+ "send_info": "yes"
73
+ }
74
+ ]
75
+ }
76
+ }
77
+
@@ -0,0 +1,157 @@
1
+ {
2
+ "_meta": {
3
+ "lookup": {
4
+ "send_info": {
5
+ "options": [
6
+ "yes",
7
+ "no",
8
+ "maybe"
9
+ ],
10
+ "in": true
11
+ }
12
+ },
13
+ "edit_form": {
14
+ "method": "PUT",
15
+ "request_encoding": "application/json",
16
+ "render": "resource",
17
+ "data": {
18
+ "name": {
19
+ "type": "string",
20
+ "required": true
21
+ },
22
+ "send_info": {
23
+ "options": [
24
+ "yes",
25
+ "no",
26
+ "maybe"
27
+ ],
28
+ "in": true
29
+ },
30
+ "user_id": {
31
+ "scope": "href",
32
+ "required": true
33
+ }
34
+ }
35
+ }
36
+ },
37
+ "_links": {
38
+ "self": {
39
+ "href": "..."
40
+ },
41
+ "search": {
42
+ "href": ".../{?send_info}",
43
+ "templated": true,
44
+ "method": "GET",
45
+ "data": {
46
+ "send_info": {
47
+ "options": [
48
+ "yes",
49
+ "no",
50
+ "maybe"
51
+ ],
52
+ "in": true
53
+ }
54
+ }
55
+ },
56
+ "agent": {
57
+ "href": "/agent/1",
58
+ "method": "GET",
59
+ "render": "embed"
60
+ },
61
+ "customer": [
62
+ {
63
+ "href": "/customer/1",
64
+ "method": "GET"
65
+ },
66
+ {
67
+ "href": "/customer/2",
68
+ "method": "GET"
69
+ }
70
+ ]
71
+ },
72
+ "_embedded": {
73
+ "agent": {
74
+ "_links": {
75
+ "self": {
76
+ "href": "/agent/1",
77
+ "method": "GET"
78
+ }
79
+ },
80
+ "name": "Mike"
81
+ },
82
+ "customer": [
83
+ {
84
+ "_links": {
85
+ "self": {
86
+ "href": "/customer/1",
87
+ "method": "GET"
88
+ },
89
+ "edit": {
90
+ "href": ".../{?user_id}",
91
+ "method": "PUT",
92
+ "request_encoding": "application/json",
93
+ "render": "resource",
94
+ "data": {
95
+ "name": {
96
+ "type": "string",
97
+ "required": true,
98
+ "value": "Tom"
99
+ },
100
+ "send_info": {
101
+ "options": [
102
+ "yes",
103
+ "no",
104
+ "maybe"
105
+ ],
106
+ "in": true,
107
+ "value": "yes"
108
+ },
109
+ "user_id": {
110
+ "scope": "href",
111
+ "required": true
112
+ }
113
+ }
114
+ }
115
+ },
116
+ "name": "Tom",
117
+ "send_info": "yes"
118
+ },
119
+ {
120
+ "_links": {
121
+ "self": {
122
+ "href": "/customer/2",
123
+ "method": "GET"
124
+ },
125
+ "edit": {
126
+ "href": ".../{?user_id}",
127
+ "method": "PUT",
128
+ "request_encoding": "application/json",
129
+ "render": "resource",
130
+ "data": {
131
+ "name": {
132
+ "type": "string",
133
+ "required": true,
134
+ "value": "Harry"
135
+ },
136
+ "send_info": {
137
+ "options": [
138
+ "yes",
139
+ "no",
140
+ "maybe"
141
+ ],
142
+ "in": true,
143
+ "value": "no"
144
+ },
145
+ "user_id": {
146
+ "scope": "href",
147
+ "required": true
148
+ }
149
+ }
150
+ }
151
+ },
152
+ "name": "Harry",
153
+ "send_info": "no"
154
+ }
155
+ ]
156
+ }
157
+ }