praxis 2.0.pre.9 → 2.0.pre.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +11 -0
  4. data/TODO.md +1 -4
  5. data/bin/praxis +11 -13
  6. data/lib/praxis.rb +10 -3
  7. data/lib/praxis/action_definition.rb +15 -13
  8. data/lib/praxis/action_definition/headers_dsl_compiler.rb +0 -7
  9. data/lib/praxis/api_general_info.rb +1 -1
  10. data/lib/praxis/application.rb +6 -2
  11. data/lib/praxis/blueprint.rb +357 -0
  12. data/lib/praxis/bootloader.rb +9 -3
  13. data/lib/praxis/bootloader_stages/environment.rb +15 -13
  14. data/lib/praxis/collection.rb +1 -11
  15. data/lib/praxis/config_hash.rb +44 -0
  16. data/lib/praxis/docs/{openapi → open_api}/info_object.rb +0 -0
  17. data/lib/praxis/docs/{openapi → open_api}/media_type_object.rb +0 -0
  18. data/lib/praxis/docs/{openapi → open_api}/operation_object.rb +0 -0
  19. data/lib/praxis/docs/{openapi → open_api}/parameter_object.rb +0 -0
  20. data/lib/praxis/docs/{openapi → open_api}/paths_object.rb +0 -0
  21. data/lib/praxis/docs/{openapi → open_api}/request_body_object.rb +0 -0
  22. data/lib/praxis/docs/{openapi → open_api}/response_object.rb +0 -0
  23. data/lib/praxis/docs/{openapi → open_api}/responses_object.rb +0 -0
  24. data/lib/praxis/docs/{openapi → open_api}/schema_object.rb +0 -0
  25. data/lib/praxis/docs/{openapi → open_api}/server_object.rb +0 -0
  26. data/lib/praxis/docs/{openapi → open_api}/tag_object.rb +0 -0
  27. data/lib/praxis/docs/open_api_generator.rb +90 -5
  28. data/lib/praxis/endpoint_definition.rb +273 -0
  29. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +1 -1
  30. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +1 -1
  31. data/lib/praxis/extensions/field_expansion.rb +3 -36
  32. data/lib/praxis/extensions/pagination.rb +2 -2
  33. data/lib/praxis/extensions/pagination/ordering_params.rb +1 -1
  34. data/lib/praxis/extensions/pagination/pagination_params.rb +6 -4
  35. data/lib/praxis/field_expander.rb +90 -0
  36. data/lib/praxis/finalizable.rb +34 -0
  37. data/lib/praxis/mapper/selector_generator.rb +1 -1
  38. data/lib/praxis/media_type.rb +3 -68
  39. data/lib/praxis/plugin_concern.rb +1 -1
  40. data/lib/praxis/plugins/mapper_plugin.rb +7 -7
  41. data/lib/praxis/renderer.rb +88 -0
  42. data/lib/praxis/request.rb +1 -1
  43. data/lib/praxis/resource_definition.rb +2 -311
  44. data/lib/praxis/response_definition.rb +2 -10
  45. data/lib/praxis/response_template.rb +3 -3
  46. data/lib/praxis/router.rb +2 -2
  47. data/lib/praxis/routing_config.rb +1 -1
  48. data/lib/praxis/tasks/api_docs.rb +13 -63
  49. data/lib/praxis/tasks/routes.rb +1 -1
  50. data/lib/praxis/types/media_type_common.rb +1 -11
  51. data/lib/praxis/version.rb +1 -1
  52. data/praxis.gemspec +0 -1
  53. data/spec/functional_spec.rb +5 -9
  54. data/spec/praxis/action_definition_spec.rb +12 -20
  55. data/spec/praxis/blueprint_spec.rb +373 -0
  56. data/spec/praxis/bootloader_spec.rb +10 -2
  57. data/spec/praxis/collection_spec.rb +0 -13
  58. data/spec/praxis/config_hash_spec.rb +64 -0
  59. data/spec/praxis/{resource_definition_spec.rb → endpoint_definition_spec.rb} +37 -64
  60. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +4 -6
  61. data/spec/praxis/extensions/field_expansion_spec.rb +5 -24
  62. data/spec/praxis/field_expander_spec.rb +149 -0
  63. data/spec/praxis/media_type_identifier_spec.rb +5 -4
  64. data/spec/praxis/media_type_spec.rb +4 -93
  65. data/spec/praxis/renderer_spec.rb +188 -0
  66. data/spec/praxis/response_definition_spec.rb +0 -31
  67. data/spec/praxis/response_spec.rb +1 -1
  68. data/spec/praxis/router_spec.rb +8 -8
  69. data/spec/praxis/routing_config_spec.rb +3 -3
  70. data/spec/spec_app/app/controllers/instances.rb +13 -7
  71. data/spec/spec_app/design/media_types/instance.rb +1 -19
  72. data/spec/spec_app/design/media_types/volume.rb +1 -1
  73. data/spec/spec_app/design/media_types/volume_snapshot.rb +2 -14
  74. data/spec/spec_app/design/resources/instances.rb +5 -8
  75. data/spec/spec_app/design/resources/volume_snapshots.rb +1 -1
  76. data/spec/spec_app/design/resources/volumes.rb +1 -1
  77. data/spec/support/spec_authorization_plugin.rb +1 -1
  78. data/spec/support/spec_blueprints.rb +72 -0
  79. data/spec/support/{spec_resource_definitions.rb → spec_endpoint_definitions.rb} +2 -2
  80. data/spec/support/spec_media_types.rb +6 -26
  81. data/tasks/thor/app.rb +8 -34
  82. data/tasks/thor/example.rb +45 -285
  83. data/tasks/thor/templates/generator/empty_app/.gitignore +0 -1
  84. data/tasks/thor/templates/generator/empty_app/Gemfile +7 -23
  85. data/tasks/thor/templates/generator/empty_app/README.md +1 -1
  86. data/tasks/thor/templates/generator/empty_app/Rakefile +4 -13
  87. data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.empty_directory +0 -0
  88. data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.gitkeep +0 -0
  89. data/tasks/thor/templates/generator/empty_app/config/environment.rb +25 -17
  90. data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.empty_directory +0 -0
  91. data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.gitkeep +0 -0
  92. data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.empty_directory +0 -0
  93. data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.gitkeep +0 -0
  94. data/tasks/thor/templates/generator/empty_app/docs/.empty_directory +0 -0
  95. data/tasks/thor/templates/generator/empty_app/docs/.gitkeep +0 -0
  96. data/tasks/thor/templates/generator/empty_app/spec/spec_helper.rb +14 -9
  97. data/tasks/thor/templates/generator/example_app/.gitignore +1 -0
  98. data/tasks/thor/templates/generator/example_app/Gemfile +19 -0
  99. data/tasks/thor/templates/generator/example_app/Rakefile +54 -0
  100. data/tasks/thor/templates/generator/example_app/app/models/user.rb +6 -0
  101. data/tasks/thor/templates/generator/example_app/app/v1/controllers/users.rb +17 -0
  102. data/tasks/thor/templates/generator/example_app/app/v1/resources/user.rb +46 -0
  103. data/tasks/thor/templates/generator/example_app/config.ru +31 -0
  104. data/tasks/thor/templates/generator/example_app/config/environment.rb +40 -0
  105. data/tasks/thor/templates/generator/example_app/db/migrate/20201010101010_create_users_table.rb +11 -0
  106. data/tasks/thor/templates/generator/example_app/design/api.rb +18 -0
  107. data/tasks/thor/templates/generator/example_app/design/v1/endpoints/users.rb +37 -0
  108. data/tasks/thor/templates/generator/example_app/design/v1/media_types/user.rb +26 -0
  109. data/tasks/thor/templates/generator/example_app/spec/helpers/database_helper.rb +18 -0
  110. data/tasks/thor/templates/generator/example_app/spec/spec_helper.rb +42 -0
  111. data/tasks/thor/templates/generator/example_app/spec/v1/controllers/users_spec.rb +37 -0
  112. metadata +49 -135
  113. data/lib/api_browser/.bowerrc +0 -3
  114. data/lib/api_browser/.editorconfig +0 -21
  115. data/lib/api_browser/Gruntfile.js +0 -581
  116. data/lib/api_browser/app/index.html +0 -59
  117. data/lib/api_browser/app/js/app.js +0 -48
  118. data/lib/api_browser/app/js/controllers/action.js +0 -47
  119. data/lib/api_browser/app/js/controllers/controller.js +0 -10
  120. data/lib/api_browser/app/js/controllers/menu.js +0 -93
  121. data/lib/api_browser/app/js/controllers/trait.js +0 -10
  122. data/lib/api_browser/app/js/controllers/type.js +0 -24
  123. data/lib/api_browser/app/js/directives/attribute_description.js +0 -56
  124. data/lib/api_browser/app/js/directives/attribute_table.js +0 -28
  125. data/lib/api_browser/app/js/directives/conditional_requirements.js +0 -13
  126. data/lib/api_browser/app/js/directives/fixed_if_fits.js +0 -38
  127. data/lib/api_browser/app/js/directives/highlight.js +0 -14
  128. data/lib/api_browser/app/js/directives/menu_item.js +0 -59
  129. data/lib/api_browser/app/js/directives/no_container.js +0 -8
  130. data/lib/api_browser/app/js/directives/readable_list.js +0 -87
  131. data/lib/api_browser/app/js/directives/request_examples.js +0 -31
  132. data/lib/api_browser/app/js/directives/type_placeholder.js +0 -30
  133. data/lib/api_browser/app/js/directives/url.js +0 -15
  134. data/lib/api_browser/app/js/factories/Configuration.js +0 -12
  135. data/lib/api_browser/app/js/factories/Documentation.js +0 -61
  136. data/lib/api_browser/app/js/factories/Example.js +0 -51
  137. data/lib/api_browser/app/js/factories/PageInfo.js +0 -9
  138. data/lib/api_browser/app/js/factories/normalize_attributes.js +0 -20
  139. data/lib/api_browser/app/js/factories/prepare_template.js +0 -15
  140. data/lib/api_browser/app/js/factories/template_for.js +0 -128
  141. data/lib/api_browser/app/js/filters/attribute_name.js +0 -10
  142. data/lib/api_browser/app/js/filters/friendly_json.js +0 -5
  143. data/lib/api_browser/app/js/filters/has_requirement.js +0 -14
  144. data/lib/api_browser/app/js/filters/header_info.js +0 -9
  145. data/lib/api_browser/app/js/filters/is_empty.js +0 -8
  146. data/lib/api_browser/app/js/filters/markdown.js +0 -6
  147. data/lib/api_browser/app/js/filters/resource_name.js +0 -5
  148. data/lib/api_browser/app/js/filters/tag_requirement.js +0 -13
  149. data/lib/api_browser/app/sass/modules/_body.scss +0 -40
  150. data/lib/api_browser/app/sass/modules/_cloke.scss +0 -8
  151. data/lib/api_browser/app/sass/modules/_header.scss +0 -10
  152. data/lib/api_browser/app/sass/modules/_nav.scss +0 -7
  153. data/lib/api_browser/app/sass/modules/_sidebar.scss +0 -134
  154. data/lib/api_browser/app/sass/modules/_switch.scss +0 -55
  155. data/lib/api_browser/app/sass/modules/_table.scss +0 -13
  156. data/lib/api_browser/app/sass/praxis.scss +0 -70
  157. data/lib/api_browser/app/sass/variables/_bootstrap-variables.scss +0 -774
  158. data/lib/api_browser/app/views/action.html +0 -97
  159. data/lib/api_browser/app/views/builtin/field-selector.html +0 -24
  160. data/lib/api_browser/app/views/controller.html +0 -55
  161. data/lib/api_browser/app/views/directives/attribute_description.html +0 -2
  162. data/lib/api_browser/app/views/directives/attribute_description/default.html +0 -2
  163. data/lib/api_browser/app/views/directives/attribute_description/example.html +0 -13
  164. data/lib/api_browser/app/views/directives/attribute_description/headers.html +0 -8
  165. data/lib/api_browser/app/views/directives/attribute_description/member_options.html +0 -4
  166. data/lib/api_browser/app/views/directives/attribute_description/values.html +0 -14
  167. data/lib/api_browser/app/views/directives/attribute_table.html +0 -17
  168. data/lib/api_browser/app/views/directives/menu_item.html +0 -8
  169. data/lib/api_browser/app/views/directives/url.html +0 -3
  170. data/lib/api_browser/app/views/examples/general.html +0 -26
  171. data/lib/api_browser/app/views/home.html +0 -5
  172. data/lib/api_browser/app/views/layout.html +0 -8
  173. data/lib/api_browser/app/views/menu.html +0 -42
  174. data/lib/api_browser/app/views/navbar.html +0 -9
  175. data/lib/api_browser/app/views/trait.html +0 -13
  176. data/lib/api_browser/app/views/type.html +0 -6
  177. data/lib/api_browser/app/views/type/details.html +0 -33
  178. data/lib/api_browser/app/views/types/embedded/array.html +0 -2
  179. data/lib/api_browser/app/views/types/embedded/default.html +0 -12
  180. data/lib/api_browser/app/views/types/embedded/field-selector.html +0 -13
  181. data/lib/api_browser/app/views/types/embedded/links.html +0 -11
  182. data/lib/api_browser/app/views/types/embedded/requirements.html +0 -6
  183. data/lib/api_browser/app/views/types/embedded/single_req.html +0 -9
  184. data/lib/api_browser/app/views/types/embedded/struct.html +0 -14
  185. data/lib/api_browser/app/views/types/label/link.html +0 -1
  186. data/lib/api_browser/app/views/types/label/primitive.html +0 -1
  187. data/lib/api_browser/app/views/types/label/primitive_collection.html +0 -1
  188. data/lib/api_browser/app/views/types/label/type.html +0 -1
  189. data/lib/api_browser/app/views/types/label/type_collection.html +0 -1
  190. data/lib/api_browser/app/views/types/main/array.html +0 -22
  191. data/lib/api_browser/app/views/types/main/default.html +0 -23
  192. data/lib/api_browser/app/views/types/main/hash.html +0 -23
  193. data/lib/api_browser/app/views/types/standalone/array.html +0 -3
  194. data/lib/api_browser/app/views/types/standalone/default.html +0 -18
  195. data/lib/api_browser/app/views/types/standalone/struct.html +0 -2
  196. data/lib/api_browser/bower_template.json +0 -41
  197. data/lib/api_browser/package-lock.json +0 -7110
  198. data/lib/api_browser/package.json +0 -43
  199. data/lib/praxis/docs/generator.rb +0 -243
  200. data/lib/praxis/docs/link_builder.rb +0 -30
  201. data/lib/praxis/links.rb +0 -135
  202. data/lib/praxis/types/multipart.rb +0 -109
  203. data/spec/api_browser/directives/type_placeholder_spec.js +0 -134
  204. data/spec/api_browser/factories/configuration_spec.js +0 -32
  205. data/spec/api_browser/factories/documentation_spec.js +0 -100
  206. data/spec/api_browser/factories/normalize_attributes_spec.js +0 -92
  207. data/spec/api_browser/factories/template_for_spec.js +0 -67
  208. data/spec/api_browser/filters/attribute_name_spec.js +0 -23
  209. data/spec/praxis/types/multipart_spec.rb +0 -112
  210. data/tasks/thor/templates/generator/empty_app/.rspec +0 -1
  211. data/tasks/thor/templates/generator/empty_app/Guardfile +0 -3
  212. data/tasks/thor/templates/generator/empty_app/config/rainbows.rb +0 -57
  213. data/tasks/thor/templates/generator/empty_app/docs/app.js +0 -1
  214. data/tasks/thor/templates/generator/empty_app/docs/styles.scss +0 -3
@@ -16,26 +16,8 @@ class Instance < Praxis::MediaType
16
16
 
17
17
  end
18
18
 
19
- view :default do
19
+ default_fieldset do
20
20
  attribute :id
21
21
  attribute :root_volume
22
22
  end
23
-
24
- view :link do
25
- attribute :id
26
- attribute :href
27
- end
28
-
29
- view :create do
30
- attribute :id
31
- attribute :name
32
- end
33
-
34
- view :extended, include_nil: true do
35
- attribute :id
36
- attribute :name
37
- attribute :root_volume
38
- end
39
-
40
-
41
23
  end
@@ -11,7 +11,7 @@ class Volume < Praxis::MediaType
11
11
  attribute :snapshots_summary, VolumeSnapshot::CollectionSummary
12
12
  end
13
13
 
14
- view :default do
14
+ default_fieldset do
15
15
  attribute :id
16
16
  attribute :name
17
17
  attribute :source
@@ -6,16 +6,11 @@ class VolumeSnapshot < Praxis::MediaType
6
6
  attribute :name, String, regexp: /snapshot-(\w+)/
7
7
  end
8
8
 
9
- view :default do
9
+ default_fieldset do
10
10
  attribute :id
11
11
  attribute :name
12
12
  end
13
13
 
14
- view :link do
15
- attribute :id
16
- end
17
-
18
-
19
14
  class CollectionSummary < Praxis::MediaType
20
15
  identifier 'application/json'
21
16
 
@@ -25,18 +20,11 @@ class VolumeSnapshot < Praxis::MediaType
25
20
  attribute :href, String
26
21
  end
27
22
 
28
- view :default do
29
- attribute :name
30
- attribute :size
31
- attribute :href
32
- end
33
-
34
- view :link do
23
+ default_fieldset do
35
24
  attribute :name
36
25
  attribute :size
37
26
  attribute :href
38
27
  end
39
-
40
28
  end
41
29
 
42
30
  end
@@ -1,6 +1,6 @@
1
1
  module ApiResources
2
2
  class Instances
3
- include Praxis::ResourceDefinition
3
+ include Praxis::EndpointDefinition
4
4
 
5
5
  media_type Instance
6
6
  version '1.0'
@@ -119,13 +119,10 @@ module ApiResources
119
119
  attribute :id
120
120
  end
121
121
 
122
- # TODO: move to something else when Multipart is fully deprecated
123
- silence_warnings do
124
- payload Praxis::Multipart, allow_extra: true do
125
- key 'destination_path', String, required: true
126
- key 'file', Attributor::FileUpload, required: false
127
- extra 'options'
128
- end
122
+ payload Praxis::Types::MultipartArray do
123
+ part 'destination_path', String, required: true
124
+ file 'file', Attributor::Tempfile, required: false
125
+ part(/.*/, String) # Allows extra parts
129
126
  end
130
127
 
131
128
  response :ok, media_type: 'application/json'
@@ -2,7 +2,7 @@ require_relative 'volumes'
2
2
 
3
3
  module ApiResources
4
4
  class VolumeSnapshots
5
- include Praxis::ResourceDefinition
5
+ include Praxis::EndpointDefinition
6
6
 
7
7
  media_type VolumeSnapshot
8
8
 
@@ -1,6 +1,6 @@
1
1
  module ApiResources
2
2
  class Volumes
3
- include Praxis::ResourceDefinition
3
+ include Praxis::EndpointDefinition
4
4
 
5
5
  media_type Volume
6
6
  version '1.0'
@@ -68,7 +68,7 @@ module AuthorizationPlugin
68
68
  end
69
69
  end
70
70
 
71
- module ResourceDefinition
71
+ module EndpointDefinition
72
72
 
73
73
  end
74
74
 
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ class PersonBlueprint < Praxis::Blueprint
3
+ attributes do
4
+ attribute :name, String, example: /[:first_name:]/
5
+ attribute :email, String, example: proc { |person| "#{person.name}@example.com" }
6
+
7
+ attribute :age, Integer
8
+
9
+ attribute :full_name, FullName
10
+ attribute :aliases, Attributor::Collection.of(FullName)
11
+
12
+ attribute :address, AddressBlueprint, example: proc { |person, context| AddressBlueprint.example(context, resident: person) }
13
+ attribute :work_address, AddressBlueprint
14
+
15
+ attribute :prior_addresses, Attributor::Collection.of(AddressBlueprint)
16
+ attribute :parents do
17
+ attribute :father, String
18
+ attribute :mother, String
19
+ end
20
+
21
+ attribute :tags, Attributor::Collection.of(String)
22
+ attribute :href, String
23
+ attribute :alive, Attributor::Boolean, default: true
24
+ attribute :myself, PersonBlueprint
25
+ attribute :friends, Attributor::Collection.of(PersonBlueprint)
26
+ attribute :metadata, Attributor::Hash
27
+ end
28
+
29
+ default_fieldset do
30
+ attribute :name
31
+ attribute :full_name
32
+ attribute :address do
33
+ attribute :name
34
+ attribute :street
35
+ end
36
+ attribute :prior_addresses do
37
+ attribute :name
38
+ end
39
+ end
40
+ end
41
+
42
+ class AddressBlueprint < Praxis::Blueprint
43
+ attributes do
44
+ attribute :id, Integer
45
+ attribute :name, String
46
+ attribute :street, String
47
+ attribute :state, String, values: %w(OR CA)
48
+
49
+ attribute :resident, PersonBlueprint, example: proc { |address, context| PersonBlueprint.example(context, address: address) }
50
+ end
51
+ end
52
+
53
+ class FullName < Attributor::Model
54
+ attributes do
55
+ attribute :first, String, example: /[:first_name:]/
56
+ attribute :last, String, example: /[:last_name:]/
57
+ end
58
+ end
59
+
60
+ class SimpleHash < Attributor::Model
61
+ attributes do
62
+ attribute :id, Integer
63
+ attribute :hash, Hash
64
+ end
65
+ end
66
+
67
+ class SimpleHashCollection < Attributor::Model
68
+ attributes do
69
+ attribute :id, Integer
70
+ attribute :hash_collection, Attributor::Collection.of(Hash)
71
+ end
72
+ end
@@ -7,7 +7,7 @@ Praxis::ApiDefinition.define do
7
7
  end
8
8
 
9
9
  class PeopleResource
10
- include Praxis::ResourceDefinition
10
+ include Praxis::EndpointDefinition
11
11
 
12
12
  description 'People resource'
13
13
 
@@ -42,7 +42,7 @@ end
42
42
 
43
43
 
44
44
  class AddressResource
45
- include Praxis::ResourceDefinition
45
+ include Praxis::EndpointDefinition
46
46
 
47
47
  description 'Address resource'
48
48
 
@@ -8,7 +8,7 @@ class Person < Praxis::MediaType
8
8
  attribute :href, String, example: proc { |person| "/people/#{person.id}" }
9
9
  end
10
10
 
11
- view :default do
11
+ default_fieldset do
12
12
  attribute :id
13
13
  attribute :name
14
14
  end
@@ -20,7 +20,7 @@ class Person < Praxis::MediaType
20
20
  attribute :size, Integer
21
21
  end
22
22
 
23
- view :default do
23
+ default_fieldset do
24
24
  attribute :href
25
25
  end
26
26
 
@@ -47,7 +47,7 @@ class Address < Praxis::MediaType
47
47
  attribute :fields, Praxis::Types::FieldSelector.for(Person)
48
48
  end
49
49
 
50
- view :default do
50
+ default_fieldset do
51
51
  attribute :id
52
52
  attribute :name
53
53
  attribute :owner
@@ -95,7 +95,7 @@ class Blog < Praxis::MediaType
95
95
 
96
96
  end
97
97
 
98
- view :default do
98
+ default_fieldset do
99
99
  attribute :id
100
100
  attribute :href
101
101
  attribute :name
@@ -105,12 +105,6 @@ class Blog < Praxis::MediaType
105
105
 
106
106
  attribute :owner
107
107
  end
108
-
109
- view :overview do
110
- attribute :id
111
- attribute :name
112
- attribute :description
113
- end
114
108
  end
115
109
 
116
110
 
@@ -147,7 +141,7 @@ class Post < Praxis::MediaType
147
141
  end
148
142
  end
149
143
 
150
- view :default do
144
+ default_fieldset do
151
145
  attribute :id
152
146
  attribute :href
153
147
 
@@ -198,25 +192,11 @@ class User < Praxis::MediaType
198
192
  example: proc { |user,ctx| Post::CollectionSummary.example(ctx, href: "#{user.href}/posts") }
199
193
  end
200
194
 
201
- view :default do
195
+ default_fieldset do
202
196
  attribute :id
203
197
  attribute :href
204
198
 
205
199
  attribute :first
206
200
  attribute :last
207
201
  end
208
-
209
- view :extended do
210
- attribute :id
211
- attribute :href
212
-
213
- attribute :first
214
- attribute :last
215
- attribute :primary_blog, view: :overview
216
- end
217
-
218
- view :with_post_links do
219
- attribute :id
220
- attribute :posts, view: :link
221
- end
222
202
  end
@@ -13,41 +13,15 @@ module PraxisGen
13
13
  # Generator for a blank new app (with a full skeleton ready to get you going)
14
14
  def new
15
15
  puts "Creating new blank Praxis app under #{app_name}"
16
- create_root_files
17
- create_config
18
- create_app
19
- create_design
20
- create_spec
21
- create_docs
22
- end
23
-
24
- private
25
- def create_root_files
26
- ['config.ru','Gemfile','Guardfile','Rakefile','README.md'].each do |file|
27
- copy_file file, "#{app_name}/#{file}"
16
+ # Copy example files
17
+ ['config.ru','Gemfile','Rakefile','README.md'].each do |file|
18
+ copy_file file, verbose: true
19
+ end
20
+ # Copy example directories
21
+ root_dirs = ['config','app','design','spec','docs']
22
+ root_dirs.each do |dir|
23
+ directory dir, recursive: true
28
24
  end
29
25
  end
30
-
31
- def create_config
32
- copy_file "config/environment.rb", "#{app_name}/config/environment.rb"
33
- copy_file "config/rainbows.rb", "#{app_name}/config/rainbows.rb"
34
- end
35
-
36
- def create_app
37
- directory "app", "#{app_name}/app", :recursive => true
38
- end
39
-
40
- def create_design
41
- directory "design", "#{app_name}/design", :recursive => true
42
- end
43
-
44
- def create_spec
45
- directory "spec", "#{app_name}/spec", :recursive => true
46
- end
47
-
48
- def create_docs
49
- directory "docs", "#{app_name}/docs", :recursive => true
50
- end
51
-
52
26
  end
53
27
  end
@@ -1,291 +1,51 @@
1
1
 
2
2
  module PraxisGen
3
- class Example < Thor
4
- include Thor::Actions
5
-
6
- namespace "praxis:example"
7
-
8
- argument :app_name, required: true
9
- desc "new", "Generates a new 'hello world' example application under an <app_name> directory"
10
-
11
- def new
12
- puts "GENERATION COMMENCED!! (for #{app_name})"
13
- # Fix weird symbols in the app name (if they are)
14
- @app_name = app_name.downcase.gsub(/[^a-z0-9_\/.]/, '')
15
- # Generate a new app
16
- empty_directory path('app')
17
- empty_directory path('design')
18
- empty_directory path('lib')
19
- empty_directory path('spec')
20
- generate_config_environment_rb
21
- generate_gemfile
22
- generate_rakefile
23
- generate_config_ru
24
- generate_app_definitions_hello_world
25
- generate_app_controllers_hello_world
26
- #
27
- puts
28
- puts "To run the example application:"
29
- puts
30
- puts " # terminal 1:"
31
- puts " cd #{app_name}"
32
- puts " bundle"
33
- puts " bundle exec rackup"
34
- puts
35
- puts " # terminal 2:"
36
- puts " # Index: list the hello words (especifying api version through the query string) "
37
- puts " curl -i 'http://localhost:8888/api/hello?api_version=1.0' -H 'Authorization: Bearer XYZ' "
38
- puts ""
39
- puts " # Show: list one of the hello words (especifying api version through a header) "
40
- puts " curl -i 'http://localhost:8888/api/hello/1' -H 'X-Api-Version: 1.0' -H 'Authorization: Bearer XYZ'"
41
- puts ""
42
- puts " # NotFound: Hello word will not be found under API 2.0"
43
- puts " curl -i 'http://localhost:8888/api/hello/1' -H 'X-Api-Version: 2.0' -H 'Authorization: Bearer XYZ'"
44
- puts " #Note: The Authorization header is made required in the application to emulate OAuth2 (but not used)"
45
- nil
46
- end
47
- private
48
- # Returns relative path for the new application
49
- #
50
- # @return [String]
51
- #
52
- # @example
53
- # # > /praxis generate My-test_praxisApp
54
- # app_dir_pathname #=> 'mytest_praxisapp'
55
- #
56
- #
57
- def app_dir_pathname
58
- @app_dir_pathname ||= Pathname.new(app_name)
59
- end
60
-
61
-
62
- # Returns path string built from the set of the given strings
63
- #
64
- # @param [String,Array] strings
65
- #
66
- # @return [String]
67
- #
68
- # @example
69
- # path('a', 'b', 'c') #=> 'my_test_app/a/b/c'
70
- #
71
- def path(*strings)
72
- app_dir_pathname.join(*strings).to_s
73
- end
74
-
75
-
76
- # Creates './config/environment.rb' file
77
- #
78
- # @return [void]
79
- #
80
- def generate_config_environment_rb
81
- create_file path('config/environment.rb') do
82
- <<-RUBY
83
- # Main entry point - DO NOT MODIFY THIS FILE
84
- ENV['RACK_ENV'] ||= 'development'
85
-
86
- Bundler.require(:default, ENV['RACK_ENV'])
87
-
88
- # Default application layout.
89
- # NOTE: This layout need NOT be specified explicitly.
90
- # It is provided just for illustration.
91
- Praxis::Application.instance.layout do
92
- map :initializers, 'config/initializers/**/*'
93
- map :lib, 'lib/**/*'
94
- map :design, 'design/' do
95
- map :api, 'api.rb'
96
- map :media_types, '**/media_types/**/*'
97
- map :resources, '**/resources/**/*'
98
- end
99
- map :app, 'app/' do
100
- map :models, 'models/**/*'
101
- map :controllers, '**/controllers/**/*'
102
- map :responses, '**/responses/**/*'
103
- end
104
- end
105
- RUBY
106
- end
107
- nil
108
- end
109
-
110
-
111
- # Creates './Gemfile' file
112
- #
113
- # @return [void]
114
- #
115
- def generate_gemfile
116
- create_file path('Gemfile') do
117
- <<-RUBY
118
- source 'https://rubygems.org'
119
-
120
- gem 'praxis'
121
- gem 'rack', '~> 1.0'
122
- gem 'rake'
123
-
124
- group :development, :test do
125
- gem 'rspec'
126
- end
127
- RUBY
128
- end
129
- nil
130
- end
131
-
132
-
133
- # Creates './Rakefile' file
134
- #
135
- # @return [void]
136
- #
137
- def generate_rakefile
138
- create_file path('Rakefile') do
139
- <<-RUBY
140
- require 'praxis'
141
- require 'praxis/tasks'
142
- RUBY
143
- end
144
- nil
145
- end
146
-
147
-
148
- # Creates './config.ru' file
149
- #
150
- # @return [void]
151
- #
152
- def generate_config_ru
153
- create_file path('config.ru') do
154
- <<-RUBY
155
- #\\ -p 8888
156
-
157
- require 'bundler/setup'
158
- require 'praxis'
159
-
160
- application = Praxis::Application.instance
161
- application.logger = Logger.new(STDOUT)
162
- application.setup
163
-
164
- run application
165
- RUBY
166
- end
3
+ class Example < Thor
4
+ include Thor::Actions
5
+
6
+ namespace "praxis:example"
7
+
8
+ def self.source_root
9
+ File.dirname(__FILE__) + "/templates/generator/example_app"
10
+ end
11
+
12
+ argument :app_name, required: true
13
+ desc "example", "Generates a new example application under an <app_name> directory to showcase some features"
14
+
15
+ def example
16
+ sanitized = app_name.downcase.gsub(/[^a-z0-9_\-.]/, '')
17
+ puts "APP_NAME: #{app_name}"
18
+ raise "Please use only letters, numbers, underscores, dashes or periods for the app name" unless sanitized == app_name
19
+
20
+ # Copy example files
21
+ root_files = ['Gemfile','config.ru','Rakefile']
22
+ root_files.each do |file|
23
+ copy_file file, verbose: true
24
+ end
25
+ # Copy example directories
26
+ root_dirs = ['app','config','design','db','spec']
27
+ root_dirs.each do |dir|
28
+ directory dir, recursive: true
29
+ end
30
+
31
+ puts
32
+ puts "To run the example application:"
33
+ puts
34
+ puts " cd #{app_name}"
35
+ puts " bundle"
36
+ puts " bundle exec rake db:create db:migrate db:seed # To create/migrate/seed the dev DB"
37
+ puts " bundle exec rackup # To start the web server"
38
+ puts
39
+ puts "From another terminal/app, use curl (or your favorite HTTP client) to retrieve data from the API"
40
+ puts " For example: "
41
+ puts " Get all users without filters or limit, and display only uid, and last_name fields"
42
+ puts " curl -H 'X-Api-Version: 1' http://localhost:9292/users?fields=uid,last_name"
43
+ puts
44
+ puts " Get the last 5 users, ordered by last_name (descending), and display only uid, and last_name fields"
45
+ puts " curl -H 'X-Api-Version: 1' 'http://localhost:9292/users?fields=uid,last_name&order=-last_name&pagination=by%3Dlast_name,items%3D5' "
46
+ puts " (Note: To list all routes use: bundle exec rake praxis:routes)"
47
+ puts
167
48
  nil
168
49
  end
169
-
170
-
171
- def generate_app_definitions_hello_world
172
- create_file path('design/api.rb') do
173
- <<-RUBY
174
- # Use this file to define your response templates and traits.
175
- #
176
- # For example, to define a response template:
177
- # response_template :custom do |media_type:|
178
- # status 200
179
- # media_type media_type
180
- # end
181
- Praxis::ApiDefinition.define do
182
- # Trait that when included will require a Bearer authorization header to be passed in.
183
- trait :authorized do
184
- headers do
185
- key "Authorization", String, regexp: /^.*Bearer\s/, required: true
186
- end
187
- end
188
- end
189
- RUBY
190
- end
191
-
192
- create_file path('design/resources/hello.rb') do
193
- <<-RUBY
194
- module V1
195
- module ApiResources
196
- class Hello
197
- include Praxis::ResourceDefinition
198
-
199
- media_type V1::MediaTypes::Hello
200
- version '1.0'
201
-
202
- prefix '/api/hello'
203
-
204
- # Will apply to all actions of this resource
205
- trait :authorized
206
-
207
- action_defaults do
208
- response :ok
209
- end
210
-
211
- action :index do
212
-
213
- routing do
214
- get ''
215
- end
216
- end
217
-
218
- action :show do
219
-
220
- routing do
221
- get '/:id'
222
- end
223
- params do
224
- attribute :id, Integer, required: true, min: 0
225
- end
226
- response :not_found
227
- end
228
- end
229
- end
230
- end
231
- RUBY
232
- end
233
-
234
- create_file path('design/media_types/hello.rb') do
235
- <<-RUBY
236
- module V1
237
- module MediaTypes
238
- class Hello < Praxis::MediaType
239
-
240
- identifier 'application/json'
241
-
242
- attributes do
243
- attribute :string, String
244
- end
245
-
246
- view :default do
247
- attribute :string
248
- end
249
- end
250
- end
251
- end
252
- RUBY
253
- end
254
- end
255
-
256
-
257
- def generate_app_controllers_hello_world
258
- create_file path('app/controllers/hello.rb') do
259
- <<-RUBY
260
- module V1
261
- class Hello
262
- include Praxis::Controller
263
-
264
- implements V1::ApiResources::Hello
265
-
266
- HELLO_WORLD = [ 'Hello world!', 'Привет мир!', 'Hola mundo!', '你好世界!', 'こんにちは世界!' ]
267
-
268
- def index(**params)
269
- response.headers['Content-Type'] = 'application/json'
270
- response.body = HELLO_WORLD.to_json
271
- response
272
- end
273
-
274
- def show(id:, **other_params)
275
- hello = HELLO_WORLD[id]
276
- if hello
277
- response.body = { id: id, data: hello }
278
- else
279
- self.response = Praxis::Responses::NotFound.new(body: "Hello word with index \#{id} not found in our DB")
280
- end
281
- response.headers['Content-Type'] = 'application/json'
282
- response
283
- end
284
50
  end
285
51
  end
286
- RUBY
287
- end
288
- end
289
-
290
- end
291
- end