praxis 2.0.pre.18 → 2.0.pre.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +54 -0
  3. data/.simplecov +3 -1
  4. data/.travis.yml +2 -1
  5. data/CHANGELOG.md +6 -0
  6. data/CONTRIBUTING.md +2 -79
  7. data/Gemfile +5 -1
  8. data/Guardfile +6 -4
  9. data/LICENSE +0 -2
  10. data/MAINTAINERS.md +1 -0
  11. data/README.md +15 -22
  12. data/Rakefile +4 -2
  13. data/bin/praxis +55 -58
  14. data/lib/praxis/action_definition/headers_dsl_compiler.rb +5 -6
  15. data/lib/praxis/action_definition.rb +64 -94
  16. data/lib/praxis/api_definition.rb +21 -29
  17. data/lib/praxis/api_general_info.rb +55 -66
  18. data/lib/praxis/application.rb +15 -32
  19. data/lib/praxis/blueprint.rb +64 -73
  20. data/lib/praxis/bootloader.rb +24 -33
  21. data/lib/praxis/bootloader_stages/environment.rb +5 -10
  22. data/lib/praxis/bootloader_stages/file_loader.rb +3 -6
  23. data/lib/praxis/bootloader_stages/plugin_config_load.rb +4 -6
  24. data/lib/praxis/bootloader_stages/plugin_config_prepare.rb +2 -2
  25. data/lib/praxis/bootloader_stages/plugin_loader.rb +3 -7
  26. data/lib/praxis/bootloader_stages/plugin_setup.rb +3 -3
  27. data/lib/praxis/bootloader_stages/routing.rb +5 -8
  28. data/lib/praxis/bootloader_stages/subgroup_loader.rb +2 -10
  29. data/lib/praxis/bootloader_stages/warn_unloaded_files.rb +15 -19
  30. data/lib/praxis/callbacks.rb +12 -11
  31. data/lib/praxis/collection.rb +10 -13
  32. data/lib/praxis/config.rb +17 -28
  33. data/lib/praxis/config_hash.rb +2 -1
  34. data/lib/praxis/controller.rb +7 -6
  35. data/lib/praxis/dispatcher.rb +34 -42
  36. data/lib/praxis/docs/open_api/info_object.rb +11 -8
  37. data/lib/praxis/docs/open_api/media_type_object.rb +18 -17
  38. data/lib/praxis/docs/open_api/operation_object.rb +6 -3
  39. data/lib/praxis/docs/open_api/parameter_object.rb +17 -14
  40. data/lib/praxis/docs/open_api/paths_object.rb +11 -9
  41. data/lib/praxis/docs/open_api/request_body_object.rb +14 -13
  42. data/lib/praxis/docs/open_api/response_object.rb +24 -18
  43. data/lib/praxis/docs/open_api/responses_object.rb +3 -1
  44. data/lib/praxis/docs/open_api/schema_object.rb +16 -16
  45. data/lib/praxis/docs/open_api/server_object.rb +5 -2
  46. data/lib/praxis/docs/open_api/tag_object.rb +6 -3
  47. data/lib/praxis/docs/open_api_generator.rb +92 -95
  48. data/lib/praxis/endpoint_definition.rb +60 -77
  49. data/lib/praxis/error_handler.rb +2 -2
  50. data/lib/praxis/exception.rb +2 -0
  51. data/lib/praxis/exceptions/config.rb +3 -1
  52. data/lib/praxis/exceptions/config_load.rb +2 -0
  53. data/lib/praxis/exceptions/config_validation.rb +3 -1
  54. data/lib/praxis/exceptions/invalid_configuration.rb +3 -1
  55. data/lib/praxis/exceptions/invalid_response.rb +3 -1
  56. data/lib/praxis/exceptions/invalid_trait.rb +3 -1
  57. data/lib/praxis/exceptions/stage_not_found.rb +3 -1
  58. data/lib/praxis/exceptions/validation.rb +4 -3
  59. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +163 -149
  60. data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +18 -13
  61. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +13 -9
  62. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +14 -11
  63. data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +12 -9
  64. data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +8 -5
  65. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +89 -65
  66. data/lib/praxis/extensions/attribute_filtering/filters_parser.rb +68 -62
  67. data/lib/praxis/extensions/attribute_filtering.rb +3 -1
  68. data/lib/praxis/extensions/field_expansion.rb +6 -4
  69. data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +10 -8
  70. data/lib/praxis/extensions/field_selection/field_selector.rb +91 -92
  71. data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +12 -12
  72. data/lib/praxis/extensions/field_selection.rb +3 -1
  73. data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +6 -4
  74. data/lib/praxis/extensions/pagination/header_generator.rb +16 -11
  75. data/lib/praxis/extensions/pagination/ordering_params.rb +29 -28
  76. data/lib/praxis/extensions/pagination/pagination_handler.rb +44 -42
  77. data/lib/praxis/extensions/pagination/pagination_params.rb +29 -48
  78. data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +8 -7
  79. data/lib/praxis/extensions/pagination.rb +10 -15
  80. data/lib/praxis/extensions/rails_compat/request_methods.rb +3 -4
  81. data/lib/praxis/extensions/rails_compat.rb +2 -0
  82. data/lib/praxis/extensions/rendering.rb +12 -12
  83. data/lib/praxis/field_expander.rb +8 -9
  84. data/lib/praxis/file_group.rb +8 -12
  85. data/lib/praxis/finalizable.rb +1 -0
  86. data/lib/praxis/handlers/json.rb +5 -2
  87. data/lib/praxis/handlers/plain.rb +2 -1
  88. data/lib/praxis/handlers/www_form.rb +6 -3
  89. data/lib/praxis/handlers/{xml-sample.rb → xml_sample.rb} +26 -22
  90. data/lib/praxis/mapper/active_model_compat.rb +13 -10
  91. data/lib/praxis/mapper/resource.rb +171 -180
  92. data/lib/praxis/mapper/selector_generator.rb +106 -112
  93. data/lib/praxis/mapper/sequel_compat.rb +70 -67
  94. data/lib/praxis/media_type.rb +2 -2
  95. data/lib/praxis/media_type_identifier.rb +26 -22
  96. data/lib/praxis/middleware_app.rb +18 -15
  97. data/lib/praxis/multipart/parser.rb +46 -51
  98. data/lib/praxis/multipart/part.rb +78 -110
  99. data/lib/praxis/notifications.rb +2 -4
  100. data/lib/praxis/plugin.rb +11 -18
  101. data/lib/praxis/plugin_concern.rb +12 -15
  102. data/lib/praxis/plugins/mapper_plugin.rb +15 -13
  103. data/lib/praxis/plugins/pagination_plugin.rb +8 -6
  104. data/lib/praxis/plugins/rails_plugin.rb +33 -28
  105. data/lib/praxis/renderer.rb +11 -15
  106. data/lib/praxis/request.rb +46 -47
  107. data/lib/praxis/request_stages/action.rb +4 -6
  108. data/lib/praxis/request_stages/load_request.rb +2 -4
  109. data/lib/praxis/request_stages/request_stage.rb +19 -23
  110. data/lib/praxis/request_stages/response.rb +4 -6
  111. data/lib/praxis/request_stages/validate.rb +3 -5
  112. data/lib/praxis/request_stages/validate_params_and_headers.rb +15 -16
  113. data/lib/praxis/request_stages/validate_payload.rb +25 -27
  114. data/lib/praxis/request_superclassing.rb +3 -3
  115. data/lib/praxis/resource_definition.rb +1 -0
  116. data/lib/praxis/response.rb +13 -25
  117. data/lib/praxis/response_definition.rb +77 -122
  118. data/lib/praxis/response_template.rb +11 -15
  119. data/lib/praxis/responses/http.rb +23 -44
  120. data/lib/praxis/responses/internal_server_error.rb +18 -21
  121. data/lib/praxis/responses/multipart_ok.rb +4 -9
  122. data/lib/praxis/responses/validation_error.rb +8 -15
  123. data/lib/praxis/route.rb +8 -10
  124. data/lib/praxis/router/rack.rb +13 -7
  125. data/lib/praxis/router/simple.rb +10 -5
  126. data/lib/praxis/router.rb +27 -34
  127. data/lib/praxis/routing_config.rb +52 -29
  128. data/lib/praxis/simple_media_type.rb +5 -8
  129. data/lib/praxis/stage.rb +17 -25
  130. data/lib/praxis/tasks/api_docs.rb +15 -15
  131. data/lib/praxis/tasks/console.rb +3 -1
  132. data/lib/praxis/tasks/environment.rb +2 -0
  133. data/lib/praxis/tasks/routes.rb +26 -24
  134. data/lib/praxis/tasks.rb +3 -1
  135. data/lib/praxis/trait.rb +37 -46
  136. data/lib/praxis/types/fuzzy_hash.rb +13 -14
  137. data/lib/praxis/types/media_type_common.rb +11 -10
  138. data/lib/praxis/types/multipart_array/part_definition.rb +14 -17
  139. data/lib/praxis/types/multipart_array.rb +88 -112
  140. data/lib/praxis/validation_handler.rb +5 -3
  141. data/lib/praxis/version.rb +3 -1
  142. data/lib/praxis.rb +4 -5
  143. data/praxis.gemspec +22 -21
  144. data/spec/functional_spec.rb +40 -52
  145. data/spec/praxis/action_definition_spec.rb +36 -46
  146. data/spec/praxis/api_definition_spec.rb +45 -47
  147. data/spec/praxis/api_general_info_spec.rb +28 -29
  148. data/spec/praxis/application_spec.rb +18 -14
  149. data/spec/praxis/blueprint_spec.rb +33 -34
  150. data/spec/praxis/bootloader_spec.rb +32 -30
  151. data/spec/praxis/callbacks_spec.rb +37 -37
  152. data/spec/praxis/collection_spec.rb +18 -25
  153. data/spec/praxis/config_hash_spec.rb +5 -4
  154. data/spec/praxis/config_spec.rb +27 -26
  155. data/spec/praxis/controller_spec.rb +8 -9
  156. data/spec/praxis/endpoint_definition_spec.rb +25 -32
  157. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +171 -114
  158. data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +22 -21
  159. data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +112 -60
  160. data/spec/praxis/extensions/attribute_filtering/filters_parser_spec.rb +37 -38
  161. data/spec/praxis/extensions/field_expansion_spec.rb +8 -10
  162. data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +14 -13
  163. data/spec/praxis/extensions/field_selection/field_selector_spec.rb +9 -16
  164. data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +50 -49
  165. data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +32 -31
  166. data/spec/praxis/extensions/rendering_spec.rb +9 -9
  167. data/spec/praxis/extensions/support/spec_resources_active_model.rb +32 -49
  168. data/spec/praxis/extensions/support/spec_resources_sequel.rb +48 -48
  169. data/spec/praxis/field_expander_spec.rb +6 -5
  170. data/spec/praxis/file_group_spec.rb +3 -1
  171. data/spec/praxis/handlers/json_spec.rb +6 -5
  172. data/spec/praxis/mapper/resource_spec.rb +27 -30
  173. data/spec/praxis/mapper/selector_generator_spec.rb +50 -50
  174. data/spec/praxis/media_type_identifier_spec.rb +13 -10
  175. data/spec/praxis/media_type_spec.rb +12 -12
  176. data/spec/praxis/middleware_app_spec.rb +23 -22
  177. data/spec/praxis/multipart/parser_spec.rb +7 -9
  178. data/spec/praxis/notifications_spec.rb +4 -4
  179. data/spec/praxis/plugin_concern_spec.rb +5 -6
  180. data/spec/praxis/renderer_spec.rb +10 -9
  181. data/spec/praxis/request_spec.rb +38 -41
  182. data/spec/praxis/request_stages/action_spec.rb +14 -15
  183. data/spec/praxis/request_stages/request_stage_spec.rb +30 -41
  184. data/spec/praxis/request_stages/validate_spec.rb +3 -1
  185. data/spec/praxis/response_definition_spec.rb +79 -92
  186. data/spec/praxis/response_spec.rb +28 -39
  187. data/spec/praxis/responses/internal_server_error_spec.rb +6 -9
  188. data/spec/praxis/responses/validation_error_spec.rb +17 -18
  189. data/spec/praxis/route_spec.rb +4 -7
  190. data/spec/praxis/router_spec.rb +69 -79
  191. data/spec/praxis/routing_config_spec.rb +15 -14
  192. data/spec/praxis/stage_spec.rb +56 -53
  193. data/spec/praxis/trait_spec.rb +17 -18
  194. data/spec/praxis/types/fuzzy_hash_spec.rb +11 -9
  195. data/spec/praxis/types/multipart_array/part_definition_spec.rb +3 -2
  196. data/spec/praxis/types/multipart_array_spec.rb +33 -48
  197. data/spec/spec_app/app/concerns/authenticated.rb +5 -5
  198. data/spec/spec_app/app/concerns/basic_api.rb +3 -1
  199. data/spec/spec_app/app/concerns/log_wrapper.rb +5 -3
  200. data/spec/spec_app/app/controllers/base_class.rb +6 -5
  201. data/spec/spec_app/app/controllers/instances.rb +31 -34
  202. data/spec/spec_app/app/controllers/volumes.rb +6 -6
  203. data/spec/spec_app/app/responses/multipart.rb +1 -2
  204. data/spec/spec_app/app/responses/other_response.rb +2 -2
  205. data/spec/spec_app/config/environment.rb +19 -6
  206. data/spec/spec_app/config.ru +4 -3
  207. data/spec/spec_app/design/api.rb +13 -15
  208. data/spec/spec_app/design/media_types/instance.rb +5 -5
  209. data/spec/spec_app/design/media_types/volume.rb +2 -1
  210. data/spec/spec_app/design/media_types/volume_snapshot.rb +2 -1
  211. data/spec/spec_app/design/resources/instances.rb +9 -15
  212. data/spec/spec_app/design/resources/volume_snapshots.rb +4 -5
  213. data/spec/spec_app/design/resources/volumes.rb +4 -5
  214. data/spec/spec_helper.rb +11 -13
  215. data/spec/support/be_deep_equal_matcher.rb +5 -0
  216. data/spec/support/spec_authorization_plugin.rb +7 -12
  217. data/spec/support/spec_blueprints.rb +2 -1
  218. data/spec/support/spec_complex_authentication_plugin.rb +17 -34
  219. data/spec/support/spec_endpoint_definitions.rb +2 -3
  220. data/spec/support/spec_media_types.rb +28 -35
  221. data/spec/support/spec_resources.rb +20 -18
  222. data/spec/support/spec_simple_authentication_plugin.rb +5 -9
  223. data/tasks/loader.thor +4 -2
  224. data/tasks/thor/app.rb +7 -5
  225. data/tasks/thor/example.rb +23 -22
  226. data/tasks/thor/model.rb +7 -7
  227. data/tasks/thor/scaffold.rb +23 -23
  228. data/tasks/thor/templates/generator/example_app/app/v1/resources/user.rb +0 -8
  229. data/tasks/thor/templates/generator/scaffold/implementation/resources/item.rb +1 -2
  230. metadata +72 -84
  231. data/MAINTAINERS +0 -2
  232. data/TODO.md +0 -25
  233. data/spec/praxis/api_resource_spec.rb +0 -0
  234. data/spec/praxis/dispatcher_spec.rb +0 -0
  235. data/spec/spec_app/app/responses/bulk_response.rb +0 -0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
 
3
5
  module AuthorizationPlugin
@@ -11,7 +13,8 @@ module AuthorizationPlugin
11
13
  end
12
14
 
13
15
  def initialize
14
- @options = {config_file: 'config/authorization.yml'}
16
+ super
17
+ @options = { config_file: 'config/authorization.yml' }
15
18
  end
16
19
 
17
20
  def prepare_config!(node)
@@ -30,7 +33,6 @@ module AuthorizationPlugin
30
33
 
31
34
  (request.action.required_abilities - abilities).empty?
32
35
  end
33
-
34
36
  end
35
37
 
36
38
  module Request
@@ -43,33 +45,27 @@ module AuthorizationPlugin
43
45
  extend ActiveSupport::Concern
44
46
 
45
47
  included do
46
-
47
48
  before :action do |controller|
48
49
  verify_abilities(controller.request)
49
50
  end
50
-
51
51
  end
52
52
 
53
-
54
53
  module ClassMethods
55
54
  def verify_abilities(request)
56
55
  return true unless request.action.required_abilities
57
56
 
58
57
  authorized = AuthorizationPlugin::Plugin.instance.authorized?(request)
59
58
 
60
- unless authorized
61
- return Praxis::Responses::Forbidden.new
62
- end
59
+ return Praxis::Responses::Forbidden.new unless authorized
63
60
  end
64
61
  end
65
62
 
66
63
  def subject
67
- #p [self, :subject]
64
+ # p [self, :subject]
68
65
  end
69
66
  end
70
67
 
71
68
  module EndpointDefinition
72
-
73
69
  end
74
70
 
75
71
  module ActionDefinition
@@ -77,6 +73,7 @@ module AuthorizationPlugin
77
73
 
78
74
  included do
79
75
  attr_accessor :required_abilities
76
+
80
77
  decorate_docs do |action, docs|
81
78
  docs[:required_abilities] = action.required_abilities
82
79
  end
@@ -90,6 +87,4 @@ module AuthorizationPlugin
90
87
  requires_authentication true
91
88
  end
92
89
  end
93
-
94
-
95
90
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  class PersonBlueprint < Praxis::Blueprint
3
4
  attributes do
4
5
  attribute :name, String, example: /[:first_name:]/
@@ -44,7 +45,7 @@ class AddressBlueprint < Praxis::Blueprint
44
45
  attribute :id, Integer
45
46
  attribute :name, String
46
47
  attribute :street, String
47
- attribute :state, String, values: %w(OR CA)
48
+ attribute :state, String, values: %w[OR CA]
48
49
 
49
50
  attribute :resident, PersonBlueprint, example: proc { |address, context| PersonBlueprint.example(context, address: address) }
50
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
 
3
5
  class Authenticator
@@ -7,7 +9,7 @@ class Authenticator
7
9
  Class
8
10
  end
9
11
 
10
- def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
12
+ def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
11
13
  case value
12
14
  when Hash
13
15
  type = value.delete(:type) || value.delete('type')
@@ -15,43 +17,32 @@ class Authenticator
15
17
  when self
16
18
  value
17
19
  else
18
- raise "#{self.naem} can not load values of type #{value.class}"
20
+ raise "#{naem} can not load values of type #{value.class}"
19
21
  end
20
22
  end
21
23
 
22
- def self.validate(*args)
23
- end
24
+ def self.validate(*args); end
24
25
 
25
- def self.describe
26
- end
27
-
28
- def initialize(**options)
29
- end
26
+ def self.describe; end
30
27
 
31
- def authenticate(request)
32
- raise "sublcass must implement authenticate"
28
+ def authenticate(_request)
29
+ raise 'sublcass must implement authenticate'
33
30
  end
34
-
35
31
  end
36
32
 
37
-
38
33
  class GlobalSessionAuthenticator < Authenticator
39
-
40
- def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
41
- self.new(**value)
42
- end
43
-
44
- def self.describe
34
+ def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
35
+ new(**value)
45
36
  end
46
37
 
47
- def initialize(**options)
48
- end
38
+ def self.describe; end
49
39
 
50
40
  def authenticate(request)
51
- body = {name: 'Unauthorized'}
41
+ body = { name: 'Unauthorized' }
52
42
 
53
43
  if (session = request.env['global_session'])
54
44
  return true if session.valid?
45
+
55
46
  body[:message] = 'Invalid session.'
56
47
  else
57
48
  body[:message] = 'Missing session.'
@@ -59,10 +50,8 @@ class GlobalSessionAuthenticator < Authenticator
59
50
 
60
51
  Praxis::Responses::Unauthorized.new(body: body)
61
52
  end
62
-
63
53
  end
64
54
 
65
-
66
55
  module ComplexAuthenticationPlugin
67
56
  include Praxis::PluginConcern
68
57
 
@@ -70,24 +59,23 @@ module ComplexAuthenticationPlugin
70
59
  include Singleton
71
60
 
72
61
  def initialize
73
- @options = {config_file: 'config/authentication.yml'}
62
+ @options = { config_file: 'config/authentication.yml' }
63
+ super
74
64
  end
75
65
 
76
66
  def config_key
77
67
  :authentication
78
68
  end
79
69
 
80
- def prepare_config!(node)
70
+ def prepare_config!(_node)
81
71
  self.config_attribute = Attributor::Attribute.new(Authenticator, required: true)
82
72
  end
83
73
 
84
74
  def self.authenticate(request)
85
75
  instance.config.authenticate(request)
86
76
  end
87
-
88
77
  end
89
78
 
90
-
91
79
  module Request
92
80
  end
93
81
 
@@ -96,15 +84,11 @@ module ComplexAuthenticationPlugin
96
84
 
97
85
  included do
98
86
  before :action do |controller|
99
- if controller.request.action.authentication_required
100
- Plugin.authenticate(controller.request)
101
- end
87
+ Plugin.authenticate(controller.request) if controller.request.action.authentication_required
102
88
  end
103
89
  end
104
-
105
90
  end
106
91
 
107
-
108
92
  module ActionDefinition
109
93
  extend ActiveSupport::Concern
110
94
 
@@ -121,6 +105,5 @@ module ComplexAuthenticationPlugin
121
105
  def authentication_required
122
106
  @authentication_required ||= false
123
107
  end
124
-
125
108
  end
126
109
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'spec_media_types'
2
4
 
3
5
  Praxis::ApiDefinition.define do
@@ -37,10 +39,8 @@ class PeopleResource
37
39
  attribute :id, Integer, required: true
38
40
  end
39
41
  end
40
-
41
42
  end
42
43
 
43
-
44
44
  class AddressResource
45
45
  include Praxis::EndpointDefinition
46
46
 
@@ -68,5 +68,4 @@ class AddressResource
68
68
  attribute :id, Integer, required: true
69
69
  end
70
70
  end
71
-
72
71
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'praxis/extensions/field_selection'
2
4
  class Person < Praxis::MediaType
3
- identifier "application/vnd.acme.person"
5
+ identifier 'application/vnd.acme.person'
4
6
 
5
7
  attributes do
6
8
  attribute :id, Integer
@@ -13,7 +15,6 @@ class Person < Praxis::MediaType
13
15
  attribute :name
14
16
  end
15
17
 
16
-
17
18
  class CollectionSummary < Praxis::MediaType
18
19
  attributes do
19
20
  attribute :href, String
@@ -23,11 +24,9 @@ class Person < Praxis::MediaType
23
24
  default_fieldset do
24
25
  attribute :href
25
26
  end
26
-
27
27
  end
28
28
  end
29
29
 
30
-
31
30
  class Address < Praxis::MediaType
32
31
  identifier 'application/json'
33
32
 
@@ -55,26 +54,23 @@ class Address < Praxis::MediaType
55
54
  end
56
55
  end
57
56
 
58
-
59
-
60
-
61
57
  class Blog < Praxis::MediaType
62
58
  identifier 'application/vnd.bloggy.blog'
63
- description "A Bloggy Blog"
59
+ description 'A Bloggy Blog'
64
60
 
65
61
  attributes do
66
62
  attribute :id, Integer,
67
- description: 'Unique identifier'
63
+ description: 'Unique identifier'
68
64
  attribute :name, String,
69
- description: 'Short name'
65
+ description: 'Short name'
70
66
  attribute :href, String,
71
- example: proc {|o,ctx| "/api/v1.0/blogs/#{o.id}"},
72
- description: 'Href for use with this API'
67
+ example: proc { |o, _ctx| "/api/v1.0/blogs/#{o.id}" },
68
+ description: 'Href for use with this API'
73
69
  attribute :description, String,
74
- description: 'Longer description'
70
+ description: 'Longer description'
75
71
  attribute :url, String,
76
- example: proc {|o,ctx| "example.com/blogs/#{o.id}"},
77
- description: 'URL for a web browser'
72
+ example: proc { |o, _ctx| "example.com/blogs/#{o.id}" },
73
+ description: 'URL for a web browser'
78
74
 
79
75
  attribute :timestamps do
80
76
  attribute :created_at, DateTime
@@ -82,17 +78,16 @@ class Blog < Praxis::MediaType
82
78
  end
83
79
 
84
80
  attribute :tags, Attributor::Collection.of(String),
85
- description: 'Array of tags'
81
+ description: 'Array of tags'
86
82
 
87
83
  attribute :recent_posts, Praxis::Collection.of(Post),
88
- description: 'Array of recent related Post resources'
84
+ description: 'Array of recent related Post resources'
89
85
  attribute :owner, User,
90
- description: 'Related User resource'
86
+ description: 'Related User resource'
91
87
 
92
88
  attribute :posts_summary, Post::CollectionSummary,
93
- example: proc { |blog,ctx| Post::CollectionSummary.example(ctx, href: "#{blog.href}/posts") },
94
- description: "Summary of information from related Post resources"
95
-
89
+ example: proc { |blog, ctx| Post::CollectionSummary.example(ctx, href: "#{blog.href}/posts") },
90
+ description: 'Summary of information from related Post resources'
96
91
  end
97
92
 
98
93
  default_fieldset do
@@ -107,7 +102,6 @@ class Blog < Praxis::MediaType
107
102
  end
108
103
  end
109
104
 
110
-
111
105
  class Post < Praxis::MediaType
112
106
  identifier 'application/vnd.bloggy.post'
113
107
 
@@ -115,23 +109,23 @@ class Post < Praxis::MediaType
115
109
 
116
110
  attributes do
117
111
  attribute :id, Integer,
118
- description: 'Unique identifier'
112
+ description: 'Unique identifier'
119
113
  attribute :href, String,
120
- example: proc {|o,ctx| "/api/v1.0/posts/#{o.id}"},
121
- description: 'Href for use with this API'
114
+ example: proc { |o, _ctx| "/api/v1.0/posts/#{o.id}" },
115
+ description: 'Href for use with this API'
122
116
 
123
117
  attribute :title, String,
124
- example: /\w+/
118
+ example: /\w+/
125
119
  attribute :content, String,
126
- example: /[:sentence:]{4,5}/
120
+ example: /[:sentence:]{4,5}/
127
121
  attribute :url, String,
128
- description: 'URL for a web browser',
129
- example: proc {|o,ctx| "example.com/posts/#{o.id}"}
122
+ description: 'URL for a web browser',
123
+ example: proc { |o, _ctx| "example.com/posts/#{o.id}" }
130
124
 
131
125
  attribute :author, User,
132
- description: 'Related User resource'
126
+ description: 'Related User resource'
133
127
  attribute :blog, Blog,
134
- description: 'Related Blog resource'
128
+ description: 'Related Blog resource'
135
129
 
136
130
  attribute :followup_posts, Attributor::Collection.of(Post)
137
131
 
@@ -161,21 +155,20 @@ class Post < Praxis::MediaType
161
155
  end
162
156
  end
163
157
 
164
-
165
158
  class User < Praxis::MediaType
166
159
  identifier 'application/vnd.bloggy.user'
167
160
 
168
161
  attributes do
169
162
  attribute :id, Integer
170
163
  attribute :href, String,
171
- example: proc {|o,ctx| "/api/v1.0/users/#{o.id}"}
164
+ example: proc { |o, _ctx| "/api/v1.0/users/#{o.id}" }
172
165
 
173
166
  attribute :first, String, example: /[:first_name:]/
174
167
  attribute :last, String, example: /[:last_name:]/
175
168
  attribute :posts, Attributor::Collection.of(Post)
176
169
 
177
170
  attribute :post_matrix, Attributor::Collection.of(Attributor::Collection.of(Post)),
178
- description: 'matrix of posts with some row and some column axes that make sense'
171
+ description: 'matrix of posts with some row and some column axes that make sense'
179
172
  attribute :daily_posts, Attributor::Collection.of(Attributor::Struct) do
180
173
  attribute :day, String
181
174
  attribute :posts, Attributor::Collection.of(Post)
@@ -189,7 +182,7 @@ class User < Praxis::MediaType
189
182
  end
190
183
 
191
184
  attribute :posts_summary, Post::CollectionSummary,
192
- example: proc { |user,ctx| Post::CollectionSummary.example(ctx, href: "#{user.href}/posts") }
185
+ example: proc { |user, ctx| Post::CollectionSummary.example(ctx, href: "#{user.href}/posts") }
193
186
  end
194
187
 
195
188
  default_fieldset do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ostruct'
2
4
 
3
5
  require 'praxis/mapper/active_model_compat'
@@ -63,15 +65,15 @@ end
63
65
  class YamlArrayModel < OpenStruct
64
66
  include Praxis::Mapper::ActiveModelCompat
65
67
  def self._praxis_associations
66
- {
67
- parents: {
68
- model: ParentModel,
69
- primary_key: :id,
70
- type: :one_to_many,
71
- local_key_columns: [:id],
72
- remote_key_columns: [:parent_id]
68
+ {
69
+ parents: {
70
+ model: ParentModel,
71
+ primary_key: :id,
72
+ type: :one_to_many,
73
+ local_key_columns: [:id],
74
+ remote_key_columns: [:parent_id]
75
+ }
73
76
  }
74
- }
75
77
  end
76
78
  end
77
79
 
@@ -79,7 +81,7 @@ end
79
81
  class BaseResource < Praxis::Mapper::Resource
80
82
  def href
81
83
  base_href = '' # "/api"
82
- base_href + "/#{self.class.collection_name}/#{self.id}"
84
+ base_href + "/#{self.class.collection_name}/#{id}"
83
85
  end
84
86
 
85
87
  property :href, dependencies: [:id]
@@ -94,33 +96,33 @@ end
94
96
  class ParentResource < BaseResource
95
97
  model ParentModel
96
98
 
97
- property :display_name, dependencies: [:simple_name, :id, :other_attribute]
99
+ property :display_name, dependencies: %i[simple_name id other_attribute]
98
100
  end
99
101
 
100
102
  class SimpleResource < BaseResource
101
103
  model SimpleModel
102
104
 
103
- resource_delegate :other_model => [:other_attribute]
105
+ resource_delegate other_model: [:other_attribute]
104
106
 
105
107
  def other_resource
106
- self.other_model
108
+ other_model
107
109
  end
108
110
 
109
- property :aliased_method, dependencies: [:column1, :other_model]
111
+ property :aliased_method, dependencies: %i[column1 other_model]
110
112
  property :other_resource, dependencies: [:other_model]
111
113
 
112
- property :parent, dependencies: [:parent, :added_column]
114
+ property :parent, dependencies: %i[parent added_column]
113
115
 
114
116
  property :name, dependencies: [:simple_name]
115
- property :direct_other_name, dependencies: [ 'other_model.name' ]
116
- property :aliased_other_name, dependencies: [ 'other_model.display_name' ]
117
+ property :direct_other_name, dependencies: ['other_model.name']
118
+ property :aliased_other_name, dependencies: ['other_model.display_name']
117
119
 
118
120
  property :everything, dependencies: [:*]
119
121
  property :everything_from_parent, dependencies: ['parent.*']
120
- property :circular_dep, dependencies: [ :circular_dep, :column1 ]
122
+ property :circular_dep, dependencies: %i[circular_dep column1]
121
123
  property :no_deps, dependencies: []
122
124
 
123
- property :deep_nested_deps, dependencies: [ 'parent.simple_children.other_model.parent.display_name']
125
+ property :deep_nested_deps, dependencies: ['parent.simple_children.other_model.parent.display_name']
124
126
  end
125
127
 
126
128
  class YamlArrayResource < BaseResource
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'singleton'
2
4
 
3
5
  module SimpleAuthenticationPlugin
@@ -7,7 +9,8 @@ module SimpleAuthenticationPlugin
7
9
  include Singleton
8
10
 
9
11
  def initialize
10
- @options = {config_file: 'config/authentication.yml'}
12
+ @options = { config_file: 'config/authentication.yml' }
13
+ super
11
14
  end
12
15
 
13
16
  def config_key
@@ -23,10 +26,8 @@ module SimpleAuthenticationPlugin
23
26
  def self.authenticate(request)
24
27
  request.current_user == 'guest'
25
28
  end
26
-
27
29
  end
28
30
 
29
-
30
31
  module Request
31
32
  def current_user
32
33
  'guest'
@@ -39,15 +40,11 @@ module SimpleAuthenticationPlugin
39
40
  included do
40
41
  before :action do |controller|
41
42
  action = controller.request.action
42
- if action.authentication_required
43
- Plugin.authenticate(controller.request)
44
- end
43
+ Plugin.authenticate(controller.request) if action.authentication_required
45
44
  end
46
45
  end
47
-
48
46
  end
49
47
 
50
-
51
48
  module ActionDefinition
52
49
  extend ActiveSupport::Concern
53
50
 
@@ -64,6 +61,5 @@ module SimpleAuthenticationPlugin
64
61
  def authentication_required
65
62
  @authentication_required ||= false
66
63
  end
67
-
68
64
  end
69
65
  end
data/tasks/loader.thor CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thor'
2
4
 
3
- files = Dir[File.join(File.dirname(__FILE__), "thor/*.rb")]
5
+ files = Dir[File.join(File.dirname(__FILE__), 'thor/*.rb')]
4
6
  files.each do |f|
5
- require File.expand_path(f)
7
+ require File.expand_path(f)
6
8
  end
data/tasks/thor/app.rb CHANGED
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module PraxisGen
2
4
  class App < Thor
3
5
  include Thor::Actions
4
6
 
5
- namespace "praxis:app"
7
+ namespace 'praxis:app'
6
8
  def self.source_root
7
- File.dirname(__FILE__) + "/templates/generator/empty_app"
9
+ "#{File.dirname(__FILE__)}/templates/generator/empty_app"
8
10
  end
9
11
 
10
12
  argument :app_name, required: true
11
- desc "new", "Generates a blank new app under <app_name> (with a full skeleton ready to start coding)"
13
+ desc 'new', 'Generates a blank new app under <app_name> (with a full skeleton ready to start coding)'
12
14
 
13
15
  # Generator for a blank new app (with a full skeleton ready to get you going)
14
16
  def new
15
17
  puts "Creating new blank Praxis app under #{app_name}"
16
18
  # Copy example files
17
- ['config.ru','Gemfile','Rakefile','README.md'].each do |file|
19
+ ['config.ru', 'Gemfile', 'Rakefile', 'README.md'].each do |file|
18
20
  copy_file file, verbose: true
19
21
  end
20
22
  # Copy example directories
21
- root_dirs = ['config','app','design','spec','docs']
23
+ root_dirs = %w[config app design spec docs]
22
24
  root_dirs.each do |dir|
23
25
  directory dir, recursive: true
24
26
  end
@@ -1,55 +1,56 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module PraxisGen
3
4
  class Example < Thor
4
5
  include Thor::Actions
5
6
 
6
- namespace "praxis:example"
7
+ namespace 'praxis:example'
7
8
 
8
9
  def self.source_root
9
- File.dirname(__FILE__) + "/templates/generator/example_app"
10
+ "#{File.dirname(__FILE__)}/templates/generator/example_app"
10
11
  end
11
12
 
12
13
  argument :app_name, required: true
13
- desc "example", "Generates a new example application under an <app_name> directory to showcase some features"
14
+ desc 'example', 'Generates a new example application under an <app_name> directory to showcase some features'
14
15
 
15
16
  def example
16
17
  sanitized = app_name.downcase.gsub(/[^a-z0-9_\-.]/, '')
17
18
  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
+ raise 'Please use only letters, numbers, underscores, dashes or periods for the app name' unless sanitized == app_name
19
20
 
20
21
  # Copy example files
21
- root_files = ['Gemfile','config.ru','Rakefile']
22
+ root_files = ['Gemfile', 'config.ru', 'Rakefile']
22
23
  root_files.each do |file|
23
24
  copy_file file, verbose: true
24
25
  end
25
26
  # Copy example directories
26
- root_dirs = ['app','config','design','db','spec']
27
+ root_dirs = %w[app config design db spec]
27
28
  root_dirs.each do |dir|
28
29
  directory dir, recursive: true
29
30
  end
30
31
 
31
32
  puts
32
- puts "To run the example application:"
33
+ puts 'To run the example application:'
33
34
  puts
34
35
  puts " cd #{app_name}"
35
- puts " bundle"
36
- puts " bundle exec rake db:recreate # 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 id, and first_name fields"
36
+ puts ' bundle'
37
+ puts ' bundle exec rake db:recreate # To create/migrate/seed the dev DB'
38
+ puts ' bundle exec rackup # To start the web server'
39
+ puts
40
+ puts 'From another terminal/app, use curl (or your favorite HTTP client) to retrieve data from the API'
41
+ puts ' For example: '
42
+ puts ' Get all users without filters or limit, and display only id, and first_name fields'
42
43
  puts " curl -G -H 'X-Api-Version: 1' http://localhost:9292/users \\"
43
- puts " --data-urlencode \"fields=id,first_name\""
44
+ puts ' --data-urlencode "fields=id,first_name"'
44
45
  puts
45
- puts " Get the last 5 users, with last_names starting with \"L\" ordered by first_name (descending)"
46
- puts " and display only id, first_name, last_name, and email fields"
46
+ puts ' Get the last 5 users, with last_names starting with "L" ordered by first_name (descending)'
47
+ puts ' and display only id, first_name, last_name, and email fields'
47
48
  puts " curl -G -H 'X-Api-Version: 1' http://localhost:9292/users \\"
48
- puts " --data-urlencode \"filters=last_name=L*\" \\"
49
- puts " --data-urlencode \"pagination=by=first_name,items=5\" \\"
50
- puts " --data-urlencode \"order=-first_name\" \\"
51
- puts " --data-urlencode \"fields=id,first_name,last_name,email\""
52
- puts " (Note: To list all routes use: bundle exec rake praxis:routes)"
49
+ puts ' --data-urlencode "filters=last_name=L*" \\'
50
+ puts ' --data-urlencode "pagination=by=first_name,items=5" \\'
51
+ puts ' --data-urlencode "order=-first_name" \\'
52
+ puts ' --data-urlencode "fields=id,first_name,last_name,email"'
53
+ puts ' (Note: To list all routes use: bundle exec rake praxis:routes)'
53
54
  puts
54
55
  nil
55
56
  end