shaf 1.5.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/iana_link_relations.csv.gz +0 -0
  5. data/lib/shaf.rb +6 -0
  6. data/lib/shaf/alps/attribute_serializer.rb +41 -0
  7. data/lib/shaf/alps/json_serializer.rb +50 -0
  8. data/lib/shaf/alps/relation_serializer.rb +70 -0
  9. data/lib/shaf/api_doc/link_relations.rb +77 -0
  10. data/lib/shaf/app.rb +12 -5
  11. data/lib/shaf/authenticator.rb +56 -0
  12. data/lib/shaf/authenticator/base.rb +161 -0
  13. data/lib/shaf/authenticator/basic_auth.rb +25 -0
  14. data/lib/shaf/authenticator/challenge.rb +32 -0
  15. data/lib/shaf/authenticator/parameter.rb +31 -0
  16. data/lib/shaf/authenticator/request.rb +17 -0
  17. data/lib/shaf/command/console.rb +1 -1
  18. data/lib/shaf/command/generate.rb +5 -2
  19. data/lib/shaf/command/new.rb +20 -7
  20. data/lib/shaf/command/templates/Gemfile.erb +1 -0
  21. data/{templates/config/settings.yml → lib/shaf/command/templates/config/settings.yml.erb} +1 -5
  22. data/lib/shaf/errors.rb +11 -0
  23. data/lib/shaf/extensions.rb +3 -3
  24. data/lib/shaf/extensions/api_routes.rb +60 -0
  25. data/lib/shaf/extensions/authorize.rb +11 -9
  26. data/lib/shaf/extensions/log.rb +1 -1
  27. data/lib/shaf/extensions/resource_uris.rb +139 -63
  28. data/lib/shaf/extensions/symbolic_routes.rb +22 -19
  29. data/lib/shaf/formable.rb +1 -2
  30. data/lib/shaf/formable/form.rb +1 -1
  31. data/lib/shaf/generator.rb +2 -0
  32. data/lib/shaf/generator/base.rb +2 -3
  33. data/lib/shaf/generator/controller.rb +11 -7
  34. data/lib/shaf/generator/doc.rb +17 -0
  35. data/lib/shaf/generator/forms.rb +1 -0
  36. data/lib/shaf/generator/helper.rb +2 -1
  37. data/lib/shaf/generator/migration/base.rb +7 -3
  38. data/lib/shaf/generator/migration/type.rb +4 -26
  39. data/lib/shaf/generator/migration/types.rb +45 -16
  40. data/lib/shaf/generator/model.rb +1 -2
  41. data/lib/shaf/generator/profile.rb +52 -0
  42. data/lib/shaf/generator/serializer.rb +38 -73
  43. data/lib/shaf/generator/templates/api/policy.rb.erb +2 -2
  44. data/lib/shaf/generator/templates/api/profile.rb.erb +16 -0
  45. data/lib/shaf/generator/templates/api/serializer.rb.erb +2 -2
  46. data/lib/shaf/generator/templates/spec/integration_spec.rb.erb +1 -2
  47. data/lib/shaf/generator/templates/spec/serializer_spec.rb.erb +5 -5
  48. data/lib/shaf/helpers.rb +4 -0
  49. data/lib/shaf/helpers/authentication.rb +79 -0
  50. data/lib/shaf/helpers/cache_control.rb +1 -2
  51. data/lib/shaf/helpers/json_html.rb +58 -18
  52. data/lib/shaf/helpers/payload.rb +26 -41
  53. data/lib/shaf/helpers/vary.rb +8 -0
  54. data/lib/shaf/logger.rb +12 -0
  55. data/lib/shaf/parser.rb +65 -0
  56. data/lib/shaf/parser/base.rb +44 -0
  57. data/lib/shaf/parser/form_data.rb +15 -0
  58. data/lib/shaf/parser/json.rb +26 -0
  59. data/lib/shaf/profile.rb +115 -0
  60. data/lib/shaf/profile/attribute.rb +29 -0
  61. data/lib/shaf/profile/evaluator.rb +46 -0
  62. data/lib/shaf/profile/relation.rb +29 -0
  63. data/lib/shaf/profile/unique_id.rb +58 -0
  64. data/lib/shaf/profiles.rb +46 -0
  65. data/lib/shaf/profiles/shaf_basic.rb +20 -0
  66. data/lib/shaf/profiles/shaf_error.rb +49 -0
  67. data/lib/shaf/profiles/shaf_form.rb +110 -0
  68. data/lib/shaf/responder.rb +41 -2
  69. data/lib/shaf/responder/alps_json.rb +25 -0
  70. data/lib/shaf/responder/base.rb +20 -17
  71. data/lib/shaf/responder/hal.rb +63 -8
  72. data/lib/shaf/responder/html.rb +65 -9
  73. data/lib/shaf/responder/problem_json.rb +1 -1
  74. data/lib/shaf/serializer.rb +35 -0
  75. data/lib/shaf/settings.rb +25 -12
  76. data/lib/shaf/spec.rb +1 -0
  77. data/lib/shaf/spec/authenticator.rb +13 -0
  78. data/lib/shaf/spec/base.rb +1 -1
  79. data/lib/shaf/spec/http_method_utils.rb +1 -1
  80. data/lib/shaf/spec/integration_spec.rb +25 -13
  81. data/lib/shaf/spec/payload_utils.rb +2 -2
  82. data/lib/shaf/supported_http_methods.rb +15 -0
  83. data/lib/shaf/tasks/api_doc_task.rb +24 -3
  84. data/lib/shaf/tasks/routes_task.rb +14 -17
  85. data/lib/shaf/upgrade/manifest.rb +11 -2
  86. data/lib/shaf/upgrade/package.rb +78 -49
  87. data/lib/shaf/upgrade/version.rb +11 -10
  88. data/lib/shaf/utils.rb +19 -5
  89. data/lib/shaf/version.rb +3 -1
  90. data/lib/shaf/yard.rb +34 -0
  91. data/lib/shaf/yard/attribute_method_handler.rb +19 -0
  92. data/lib/shaf/yard/attribute_object.rb +30 -0
  93. data/lib/shaf/yard/base_method_handler.rb +30 -0
  94. data/lib/shaf/yard/link_method_handler.rb +39 -0
  95. data/lib/shaf/yard/link_object.rb +60 -0
  96. data/lib/shaf/yard/nested_attributes.rb +37 -0
  97. data/lib/shaf/yard/parser.rb +64 -0
  98. data/lib/shaf/yard/profile_method_handler.rb +51 -0
  99. data/lib/shaf/yard/profile_object.rb +21 -0
  100. data/lib/shaf/yard/resource_object.rb +55 -0
  101. data/lib/shaf/yard/serializer_handler.rb +27 -0
  102. data/templates/api/controllers/base_controller.rb +0 -10
  103. data/templates/api/controllers/docs_controller.rb +5 -3
  104. data/templates/api/controllers/root_controller.rb +7 -1
  105. data/templates/api/policies/base_policy.rb +2 -0
  106. data/templates/api/serializers/base_serializer.rb +1 -3
  107. data/templates/api/serializers/error_serializer.rb +1 -5
  108. data/templates/api/serializers/form_serializer.rb +1 -5
  109. data/templates/api/serializers/validation_error_serializer.rb +1 -5
  110. data/templates/config.ru +1 -1
  111. data/templates/config/bootstrap.rb +1 -2
  112. data/templates/config/directories.rb +52 -44
  113. data/templates/config/helpers.rb +1 -1
  114. data/templates/config/initializers.rb +52 -8
  115. data/templates/config/initializers/authentication.rb +18 -0
  116. data/templates/config/initializers/db_migrations.rb +2 -2
  117. data/templates/config/initializers/logging.rb +2 -2
  118. data/templates/frontend/assets/css/main.css +33 -1
  119. data/templates/frontend/views/headers.erb +20 -0
  120. data/templates/frontend/views/layout.erb +7 -1
  121. data/templates/frontend/views/payload.erb +1 -0
  122. data/templates/spec/spec_helper.rb +2 -0
  123. data/upgrades/0.5.0.tar.gz +0 -0
  124. data/upgrades/1.0.4.tar.gz +0 -0
  125. data/upgrades/1.1.0.tar.gz +0 -0
  126. data/upgrades/1.6.0.tar.gz +0 -0
  127. data/upgrades/1.6.1.tar.gz +0 -0
  128. data/upgrades/2.0.0.tar.gz +0 -0
  129. data/yard_templates/api_doc/doc_index/html/body.erb +3 -0
  130. data/yard_templates/api_doc/doc_index/setup.rb +8 -0
  131. data/yard_templates/api_doc/html/css/api-doc.css +222 -0
  132. data/yard_templates/api_doc/html/favicon.ico +0 -0
  133. data/yard_templates/api_doc/html/js/switch_tab.js +17 -0
  134. data/yard_templates/api_doc/html/setup.rb +59 -0
  135. data/yard_templates/api_doc/layout/html/footer.erb +3 -0
  136. data/yard_templates/api_doc/layout/html/header.erb +7 -0
  137. data/yard_templates/api_doc/layout/html/layout.erb +13 -0
  138. data/yard_templates/api_doc/layout/setup.rb +24 -0
  139. data/yard_templates/api_doc/profile/html/attributes.erb +10 -0
  140. data/yard_templates/api_doc/profile/html/profile.erb +6 -0
  141. data/yard_templates/api_doc/profile/html/relations.erb +10 -0
  142. data/yard_templates/api_doc/profile/setup.rb +38 -0
  143. data/yard_templates/api_doc/profile_attribute/html/attribute.erb +23 -0
  144. data/yard_templates/api_doc/profile_attribute/setup.rb +21 -0
  145. data/yard_templates/api_doc/profile_relation/html/relation.erb +37 -0
  146. data/yard_templates/api_doc/profile_relation/setup.rb +41 -0
  147. data/yard_templates/api_doc/resource/html/attributes.erb +10 -0
  148. data/yard_templates/api_doc/resource/html/profile.erb +14 -0
  149. data/yard_templates/api_doc/resource/html/relations.erb +10 -0
  150. data/yard_templates/api_doc/resource/html/resource.erb +5 -0
  151. data/yard_templates/api_doc/resource/setup.rb +56 -0
  152. data/yard_templates/api_doc/resource_attribute/html/attribute.erb +23 -0
  153. data/yard_templates/api_doc/resource_attribute/setup.rb +20 -0
  154. data/yard_templates/api_doc/resource_relation/html/relation.erb +47 -0
  155. data/yard_templates/api_doc/resource_relation/setup.rb +80 -0
  156. data/yard_templates/api_doc/setup.rb +31 -0
  157. data/yard_templates/api_doc/sidebar/html/profile_list.erb +8 -0
  158. data/yard_templates/api_doc/sidebar/html/search.erb +7 -0
  159. data/yard_templates/api_doc/sidebar/html/serializer_list.erb +8 -0
  160. data/yard_templates/api_doc/sidebar/html/sidebar.erb +13 -0
  161. data/yard_templates/api_doc/sidebar/setup.rb +56 -0
  162. metadata +140 -30
  163. metadata.gz.sig +0 -0
  164. data/lib/shaf/extensions/current_user.rb +0 -48
  165. data/lib/shaf/responder/hal_serializable.rb +0 -54
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module Shaf
6
+ module Parser
7
+ class Error < StandardError; end
8
+
9
+ INPUT_BODY = 'shaf.input_body'
10
+
11
+ class << self
12
+ def register(parser)
13
+ parsers << parser
14
+ end
15
+
16
+ def unregister(parser)
17
+ parsers.delete(parser)
18
+ end
19
+
20
+ def input?(request)
21
+ !!input(request)
22
+ end
23
+
24
+ def for(request)
25
+ clazz = parser_for(request)
26
+ return unless clazz
27
+
28
+ body = input(request)
29
+ clazz.new(request: request, body: body)
30
+ end
31
+
32
+ private
33
+
34
+ def parser_for(request)
35
+ parsers.find do |parser|
36
+ parser.can_handle? request
37
+ end
38
+ end
39
+
40
+ def parsers
41
+ @parsers ||= Set.new
42
+ end
43
+
44
+ def input(request)
45
+ body = request.get_header(INPUT_BODY)
46
+ body ||= read_input(request).tap do |b|
47
+ request.set_header(INPUT_BODY, b)
48
+ end
49
+
50
+ body unless String(body).strip.empty?
51
+ end
52
+
53
+ def read_input(request)
54
+ request.body.rewind
55
+ request.body.read
56
+ ensure
57
+ request.body.rewind
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ require 'shaf/parser/base'
64
+ require 'shaf/parser/json'
65
+ require 'shaf/parser/form_data'
@@ -0,0 +1,44 @@
1
+ module Shaf
2
+ module Parser
3
+ class Base
4
+ class << self
5
+ def inherited(child)
6
+ Parser.register(child)
7
+ super
8
+ end
9
+
10
+ def mime_type(type = nil, value = nil)
11
+ if type
12
+ @mime_type = type
13
+ @mime_type = Sinatra::Base.mime_type(type, value) if type.is_a? Symbol
14
+ end
15
+
16
+ @mime_type if defined? @mime_type
17
+ end
18
+
19
+ def can_handle?(request)
20
+ mime_type == request.content_type
21
+ end
22
+
23
+ end
24
+
25
+ attr_reader :request, :body
26
+
27
+ def initialize(request:, body:)
28
+ @request = request
29
+ @body = body
30
+ end
31
+
32
+ def call
33
+ raise NotImplementedError, "#{self.class} must implement #call"
34
+ end
35
+
36
+ private
37
+
38
+ def mime_type
39
+ self.class.mime_type
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,15 @@
1
+ module Shaf
2
+ module Parser
3
+ class FormData < Base
4
+ def self.can_handle?(request)
5
+ request.form_data? || request.parseable_data?
6
+ end
7
+
8
+ def call
9
+ request.POST.tap do |data| # Returns form params from Rack::Request
10
+ data.delete '_method' # If the method override hack is used remove the _method key
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module Shaf
2
+ module Parser
3
+ class Json < Base
4
+
5
+ mime_type :json, 'application/json'
6
+
7
+ def self.can_handle?(request)
8
+ request.content_type&.match? %r{application/(.*\+)?json}
9
+ end
10
+
11
+ def call
12
+ @payload ||= parse_json
13
+ end
14
+
15
+ private
16
+
17
+ def parse_json
18
+ return {} if body.empty?
19
+
20
+ JSON.parse(body, symbolize_names: true)
21
+ rescue JSON::ParserError => e
22
+ raise Error, e.message
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shaf/profile/evaluator'
4
+ require 'shaf/extensions/resource_uris'
5
+
6
+ module Shaf
7
+ class Profile
8
+ include Shaf::UriHelper
9
+
10
+ class << self
11
+ def inherited(child)
12
+ Profiles.register child
13
+ end
14
+
15
+ def name(str = nil)
16
+ @name = str if str
17
+ @name if defined? @name
18
+ end
19
+
20
+ def doc(str = nil)
21
+ @doc = str if str
22
+ @doc if defined? @doc
23
+ end
24
+
25
+ def urn(value = nil)
26
+ @urn = value if value
27
+ @urn if defined? @urn
28
+ end
29
+
30
+ def example(str)
31
+ examples << str
32
+ end
33
+
34
+ def match?(str)
35
+ normalize(name) == normalize(str)
36
+ end
37
+
38
+ def attributes
39
+ @attributes ||= []
40
+ end
41
+
42
+ def relations
43
+ @relations ||= []
44
+ end
45
+
46
+ def examples
47
+ @examples ||= []
48
+ end
49
+
50
+ def attribute(*args, **kwargs, &block)
51
+ evaluator.attribute(*args, **kwargs, &block)
52
+ end
53
+
54
+ def relation(*args, **kwargs, &block)
55
+ evaluator.rel(*args, **kwargs, &block)
56
+ end
57
+ alias rel relation
58
+
59
+ def descriptor(id)
60
+ find_attribute(id) || find_relation(id)
61
+ end
62
+
63
+ def find_attribute(id)
64
+ attributes.find { |attr| attr.id.to_sym == id.to_sym }
65
+ end
66
+
67
+ def find_relation(id)
68
+ relations.find { |rel| rel.id.to_sym == id.to_sym }
69
+ end
70
+
71
+ def use(*descriptors, from:, doc: nil)
72
+ descriptors.each do |id|
73
+ desc = from.descriptor(id)
74
+ href = profile_path(from.name, fragment_id: id)
75
+
76
+ case desc
77
+ when Relation
78
+ kwargs = {
79
+ doc: doc || desc&.doc,
80
+ href: href,
81
+ http_methods: desc.http_methods,
82
+ payload_type: desc.payload_type,
83
+ content_type: desc.content_type,
84
+ }
85
+ relation(id, **kwargs)
86
+ when Attribute
87
+ attribute(id, href: href, doc: doc)
88
+ when NilClass
89
+ raise "#{from.name} does not have a descriptor with id #{id}"
90
+ else
91
+ raise Errors::ServerError, "Unsupported descriptor: #{desc}"
92
+ end
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def evaluator
99
+ Evaluator.new(parent: self)
100
+ end
101
+
102
+ def normalize(name)
103
+ name.to_s.downcase.tr('-', '_')
104
+ end
105
+ end
106
+
107
+ def name
108
+ normalize(self.class.name)
109
+ end
110
+
111
+ def normalize(str)
112
+ self.class.normalize(str)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shaf/profile/unique_id'
4
+
5
+ module Shaf
6
+ class Profile
7
+ class Attribute
8
+ include UniqueId
9
+
10
+ attr_reader :name, :doc, :href, :type, :parent
11
+
12
+ def initialize(name, **opts)
13
+ @name = name.to_sym
14
+ @doc = opts[:doc].freeze
15
+ @href = opts[:href].freeze
16
+ @type = opts[:type]&.to_s
17
+ @parent = opts[:parent]
18
+ end
19
+
20
+ def attributes
21
+ @attributes ||= []
22
+ end
23
+
24
+ def relations
25
+ @relations ||= []
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shaf/profile/attribute'
4
+ require 'shaf/profile/relation'
5
+
6
+ module Shaf
7
+ class Profile
8
+ class Evaluator
9
+ attr_reader :parent, :allowed
10
+
11
+ def initialize(parent:, allowed: nil)
12
+ @parent = parent
13
+ @allowed = allowed && Array(allowed).map(&:to_sym)
14
+ end
15
+
16
+ def attribute(name, doc:, type: :string, &block)
17
+ return unless allow? :attribute
18
+
19
+ attr = Attribute.new(name, doc: doc, type: type, parent: parent)
20
+ self.class.new(parent: attr, allowed: allowed).instance_exec(&block) if block
21
+ parent.attributes << attr
22
+ end
23
+
24
+ def relation(name, **kwargs, &block)
25
+ return unless allow? :rel
26
+
27
+ rel = Relation.new(name, parent: parent, **kwargs)
28
+ self.class.new(parent: rel, allowed: [:attribute]).instance_exec(&block) if block
29
+ parent.relations << rel
30
+ end
31
+ alias rel relation
32
+
33
+ private
34
+
35
+ def allow?(name)
36
+ return true unless allowed
37
+ return true if allowed.include? name
38
+
39
+ Shaf.log.warn "#{name} is not allowed to be nested inside #{parent.class} " \
40
+ "(or parent object containing #{parent.class})"
41
+
42
+ false
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shaf/profile/unique_id'
4
+
5
+ module Shaf
6
+ class Profile
7
+ class Relation
8
+ include UniqueId
9
+
10
+ attr_reader :name, :doc, :href, :http_methods, :payload_type, :content_type, :parent
11
+
12
+ def initialize(name, **opts)
13
+ @name = name.to_sym
14
+ @doc = opts[:doc].freeze
15
+ @href = opts[:href].freeze
16
+ http_methods = Array(opts[:http_method]) + Array(opts[:http_methods])
17
+ http_methods << 'GET' if http_methods.empty?
18
+ @http_methods = http_methods.map { |m| m.to_s.upcase }.uniq.freeze
19
+ @payload_type = opts[:payload_type].freeze
20
+ @content_type = opts[:content_type].freeze
21
+ @parent = opts[:parent]
22
+ end
23
+
24
+ def attributes
25
+ @attributes ||= []
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shaf
4
+ class Profile
5
+ module UniqueId
6
+ def id
7
+ return @id if defined? @id
8
+ @id = __find_unique_id
9
+ end
10
+
11
+ def __pending_id?
12
+ @__pending_id ||= false
13
+ end
14
+
15
+ private
16
+
17
+ def __find_unique_id
18
+ @__pending_id = true
19
+
20
+ return name.to_s unless __id_collision? name.to_s
21
+
22
+ id = [parent.name, name].join('_')
23
+ return id unless __id_collision? id
24
+
25
+ id = "#{id}0"
26
+
27
+ loop do
28
+ id = id.next
29
+ break id unless __id_collision? id
30
+ end
31
+ ensure
32
+ @__pending_id = false
33
+ end
34
+
35
+ def __id_collision? id
36
+ descriptor = self
37
+
38
+ loop do
39
+ break false unless descriptor.respond_to?(:parent) && descriptor.parent
40
+ descriptor = descriptor.parent
41
+
42
+ __parent_descriptors(descriptor).each do |desc|
43
+ next if desc == self
44
+ next if desc.__pending_id?
45
+ return true if desc.id == id
46
+ end
47
+ end
48
+ end
49
+
50
+ def __parent_descriptors(parent)
51
+ descriptors = []
52
+ descriptors += parent.attributes if parent.respond_to? :attributes
53
+ descriptors += parent.relations if parent.respond_to? :relations
54
+ descriptors
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+ require 'shaf/errors'
2
+
3
+ module Shaf
4
+ module Profiles
5
+ class ProfileNotFoundError < Errors::NotFoundError
6
+ def initialize(name)
7
+ msg = %Q(Profile with name "#{name}" does not exist)
8
+ super(msg, id: name)
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def register(clazz)
14
+ profiles << clazz
15
+ end
16
+
17
+ def unregister(clazz)
18
+ profiles.delete(clazz)
19
+ end
20
+
21
+ def find(name)
22
+ name = String(name)
23
+ return if name.empty?
24
+
25
+ profiles.find { |profile| profile.match? name }
26
+ end
27
+
28
+ def find!(name)
29
+ find(name) or raise ProfileNotFoundError, name
30
+ end
31
+
32
+ def profiles
33
+ @profiles ||= []
34
+ end
35
+
36
+ def clear
37
+ @profiles.clear
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ require 'shaf/profile'
44
+ require 'shaf/profiles/shaf_form'
45
+ require 'shaf/profiles/shaf_error'
46
+ require 'shaf/profiles/shaf_basic'