shaf 1.6.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/iana_link_relations.csv.gz +0 -0
  4. data/lib/shaf/alps/attribute_serializer.rb +41 -0
  5. data/lib/shaf/alps/json_serializer.rb +50 -0
  6. data/lib/shaf/alps/relation_serializer.rb +70 -0
  7. data/lib/shaf/app.rb +32 -7
  8. data/lib/shaf/authenticator/base.rb +161 -0
  9. data/lib/shaf/authenticator/basic_auth.rb +25 -0
  10. data/lib/shaf/authenticator/challenge.rb +32 -0
  11. data/lib/shaf/authenticator/parameter.rb +31 -0
  12. data/lib/shaf/authenticator/request.rb +17 -0
  13. data/lib/shaf/authenticator.rb +56 -0
  14. data/lib/shaf/command/base.rb +4 -0
  15. data/lib/shaf/command/console.rb +1 -1
  16. data/lib/shaf/command/generate.rb +5 -2
  17. data/lib/shaf/command/new.rb +24 -7
  18. data/lib/shaf/command/server.rb +5 -1
  19. data/lib/shaf/command/templates/Gemfile.erb +1 -0
  20. data/{templates/config/settings.yml → lib/shaf/command/templates/config/settings.yml.erb} +9 -12
  21. data/lib/shaf/errors.rb +11 -0
  22. data/lib/shaf/extensions/api_routes.rb +60 -0
  23. data/lib/shaf/extensions/authorize.rb +11 -9
  24. data/lib/shaf/extensions/log.rb +1 -1
  25. data/lib/shaf/extensions/resource_uris.rb +95 -137
  26. data/lib/shaf/extensions/symbolic_routes.rb +9 -19
  27. data/lib/shaf/extensions.rb +3 -3
  28. data/lib/shaf/formable/builder.rb +58 -19
  29. data/lib/shaf/formable/form.rb +14 -11
  30. data/lib/shaf/formable.rb +10 -24
  31. data/lib/shaf/generator/base.rb +84 -3
  32. data/lib/shaf/generator/controller.rb +30 -42
  33. data/lib/shaf/generator/doc.rb +17 -0
  34. data/lib/shaf/generator/forms.rb +11 -14
  35. data/lib/shaf/generator/helper.rb +2 -1
  36. data/lib/shaf/generator/migration/add_column.rb +0 -4
  37. data/lib/shaf/generator/migration/add_index.rb +0 -4
  38. data/lib/shaf/generator/migration/base.rb +15 -3
  39. data/lib/shaf/generator/migration/create_table.rb +0 -4
  40. data/lib/shaf/generator/migration/drop_column.rb +0 -4
  41. data/lib/shaf/generator/migration/rename_column.rb +0 -4
  42. data/lib/shaf/generator/migration/type.rb +4 -26
  43. data/lib/shaf/generator/migration/types.rb +45 -16
  44. data/lib/shaf/generator/model.rb +29 -15
  45. data/lib/shaf/generator/policy.rb +8 -14
  46. data/lib/shaf/generator/profile.rb +47 -0
  47. data/lib/shaf/generator/scaffold.rb +6 -9
  48. data/lib/shaf/generator/serializer.rb +64 -98
  49. data/lib/shaf/generator/templates/api/controller.rb.erb +13 -13
  50. data/lib/shaf/generator/templates/api/forms.rb.erb +2 -2
  51. data/lib/shaf/generator/templates/api/model.rb.erb +1 -1
  52. data/lib/shaf/generator/templates/api/policy.rb.erb +2 -2
  53. data/lib/shaf/generator/templates/api/profile.rb.erb +16 -0
  54. data/lib/shaf/generator/templates/api/serializer.rb.erb +3 -3
  55. data/lib/shaf/generator/templates/spec/integration_spec.rb.erb +15 -16
  56. data/lib/shaf/generator/templates/spec/serializer_spec.rb.erb +5 -5
  57. data/lib/shaf/generator.rb +2 -0
  58. data/lib/shaf/helpers/authentication.rb +79 -0
  59. data/lib/shaf/helpers/paginate.rb +1 -1
  60. data/lib/shaf/helpers/payload.rb +14 -34
  61. data/lib/shaf/helpers/vary.rb +8 -0
  62. data/lib/shaf/helpers.rb +4 -0
  63. data/lib/shaf/logger.rb +12 -0
  64. data/lib/shaf/parser/base.rb +44 -0
  65. data/lib/shaf/parser/form_data.rb +15 -0
  66. data/lib/shaf/parser/json.rb +26 -0
  67. data/lib/shaf/parser.rb +65 -0
  68. data/lib/shaf/profile/attribute.rb +29 -0
  69. data/lib/shaf/profile/evaluator.rb +46 -0
  70. data/lib/shaf/profile/relation.rb +29 -0
  71. data/lib/shaf/profile/unique_id.rb +58 -0
  72. data/lib/shaf/profile.rb +115 -0
  73. data/lib/shaf/profiles/shaf_basic.rb +20 -0
  74. data/lib/shaf/profiles/shaf_error.rb +49 -0
  75. data/lib/shaf/profiles/shaf_form.rb +110 -0
  76. data/lib/shaf/profiles.rb +46 -0
  77. data/lib/shaf/registrable_factory.rb +62 -32
  78. data/lib/shaf/responder/alps_json.rb +25 -0
  79. data/lib/shaf/responder/base.rb +20 -17
  80. data/lib/shaf/responder/hal.rb +66 -5
  81. data/lib/shaf/responder/html.rb +43 -16
  82. data/lib/shaf/responder/problem_json.rb +2 -2
  83. data/lib/shaf/responder.rb +41 -2
  84. data/lib/shaf/router.rb +65 -12
  85. data/lib/shaf/serializer.rb +35 -0
  86. data/lib/shaf/settings.rb +25 -12
  87. data/lib/shaf/spec/authenticator.rb +13 -0
  88. data/lib/shaf/spec/base.rb +1 -1
  89. data/lib/shaf/spec/http_method_utils.rb +1 -1
  90. data/lib/shaf/spec/integration_spec.rb +26 -14
  91. data/lib/shaf/spec/payload_utils.rb +2 -2
  92. data/lib/shaf/spec.rb +1 -0
  93. data/lib/shaf/supported_http_methods.rb +15 -0
  94. data/lib/shaf/tasks/routes_task.rb +14 -17
  95. data/lib/shaf/tasks.rb +0 -1
  96. data/lib/shaf/upgrade/manifest.rb +11 -2
  97. data/lib/shaf/upgrade/package.rb +73 -45
  98. data/lib/shaf/upgrade/version.rb +11 -10
  99. data/lib/shaf/utils.rb +19 -5
  100. data/lib/shaf/version.rb +3 -1
  101. data/lib/shaf/yard/attribute_method_handler.rb +19 -0
  102. data/lib/shaf/yard/attribute_object.rb +30 -0
  103. data/lib/shaf/yard/base_method_handler.rb +30 -0
  104. data/lib/shaf/yard/link_method_handler.rb +39 -0
  105. data/lib/shaf/yard/link_object.rb +60 -0
  106. data/lib/shaf/yard/nested_attributes.rb +37 -0
  107. data/lib/shaf/yard/parser.rb +64 -0
  108. data/lib/shaf/yard/profile_method_handler.rb +51 -0
  109. data/lib/shaf/yard/profile_object.rb +21 -0
  110. data/lib/shaf/yard/resource_object.rb +55 -0
  111. data/lib/shaf/yard/serializer_handler.rb +27 -0
  112. data/lib/shaf/yard.rb +34 -0
  113. data/lib/shaf.rb +6 -0
  114. data/templates/Rakefile +0 -6
  115. data/templates/api/controllers/base_controller.rb +0 -12
  116. data/templates/api/controllers/docs_controller.rb +5 -3
  117. data/templates/api/controllers/root_controller.rb +7 -1
  118. data/templates/api/policies/base_policy.rb +2 -0
  119. data/templates/api/serializers/base_serializer.rb +1 -3
  120. data/templates/api/serializers/error_serializer.rb +1 -5
  121. data/templates/api/serializers/form_serializer.rb +1 -5
  122. data/templates/api/serializers/root_serializer.rb +0 -11
  123. data/templates/api/serializers/validation_error_serializer.rb +1 -5
  124. data/templates/config/bootstrap.rb +1 -2
  125. data/templates/config/directories.rb +52 -44
  126. data/templates/config/helpers.rb +1 -1
  127. data/templates/config/initializers/authentication.rb +18 -0
  128. data/templates/config/initializers/db_migrations.rb +2 -2
  129. data/templates/config/initializers/logging.rb +2 -2
  130. data/templates/config/initializers/middleware.rb +6 -0
  131. data/templates/config/initializers.rb +52 -8
  132. data/templates/config.ru +1 -1
  133. data/templates/spec/spec_helper.rb +5 -0
  134. data/upgrades/0.5.0.tar.gz +0 -0
  135. data/upgrades/1.0.4.tar.gz +0 -0
  136. data/upgrades/1.1.0.tar.gz +0 -0
  137. data/upgrades/1.6.1.tar.gz +0 -0
  138. data/upgrades/2.0.0.tar.gz +0 -0
  139. data/upgrades/3.0.0.tar.gz +0 -0
  140. data/yard_templates/api_doc/doc_index/html/body.erb +3 -0
  141. data/yard_templates/api_doc/doc_index/setup.rb +8 -0
  142. data/yard_templates/api_doc/html/css/api-doc.css +222 -0
  143. data/yard_templates/api_doc/html/favicon.ico +0 -0
  144. data/yard_templates/api_doc/html/js/switch_tab.js +17 -0
  145. data/yard_templates/api_doc/html/setup.rb +59 -0
  146. data/yard_templates/api_doc/layout/html/footer.erb +3 -0
  147. data/yard_templates/api_doc/layout/html/header.erb +7 -0
  148. data/yard_templates/api_doc/layout/html/layout.erb +13 -0
  149. data/yard_templates/api_doc/layout/setup.rb +24 -0
  150. data/yard_templates/api_doc/profile/html/attributes.erb +10 -0
  151. data/yard_templates/api_doc/profile/html/profile.erb +6 -0
  152. data/yard_templates/api_doc/profile/html/relations.erb +10 -0
  153. data/yard_templates/api_doc/profile/setup.rb +38 -0
  154. data/yard_templates/api_doc/profile_attribute/html/attribute.erb +23 -0
  155. data/yard_templates/api_doc/profile_attribute/setup.rb +21 -0
  156. data/yard_templates/api_doc/profile_relation/html/relation.erb +37 -0
  157. data/yard_templates/api_doc/profile_relation/setup.rb +41 -0
  158. data/yard_templates/api_doc/resource/html/attributes.erb +10 -0
  159. data/yard_templates/api_doc/resource/html/profile.erb +14 -0
  160. data/yard_templates/api_doc/resource/html/relations.erb +10 -0
  161. data/yard_templates/api_doc/resource/html/resource.erb +5 -0
  162. data/yard_templates/api_doc/resource/setup.rb +56 -0
  163. data/yard_templates/api_doc/resource_attribute/html/attribute.erb +23 -0
  164. data/yard_templates/api_doc/resource_attribute/setup.rb +20 -0
  165. data/yard_templates/api_doc/resource_relation/html/relation.erb +47 -0
  166. data/yard_templates/api_doc/resource_relation/setup.rb +80 -0
  167. data/yard_templates/api_doc/setup.rb +31 -0
  168. data/yard_templates/api_doc/sidebar/html/profile_list.erb +8 -0
  169. data/yard_templates/api_doc/sidebar/html/search.erb +7 -0
  170. data/yard_templates/api_doc/sidebar/html/serializer_list.erb +8 -0
  171. data/yard_templates/api_doc/sidebar/html/sidebar.erb +13 -0
  172. data/yard_templates/api_doc/sidebar/setup.rb +56 -0
  173. data.tar.gz.sig +2 -2
  174. metadata +143 -38
  175. metadata.gz.sig +0 -0
  176. data/lib/shaf/api_doc/comment.rb +0 -27
  177. data/lib/shaf/api_doc/document.rb +0 -137
  178. data/lib/shaf/extensions/current_user.rb +0 -48
  179. data/lib/shaf/middleware.rb +0 -1
  180. data/lib/shaf/responder/hal_serializable.rb +0 -64
  181. data/lib/shaf/tasks/api_doc_task.rb +0 -125
@@ -1,137 +0,0 @@
1
- require 'fileutils'
2
- require 'yaml'
3
- require 'redcarpet'
4
- require 'redcarpet/render_strip'
5
-
6
- module Shaf
7
- module ApiDoc
8
- class Document
9
- attr_writer :model
10
- attr_accessor :serializer_class, :policy, :attributes, :links, :curies, :embeds
11
-
12
- def initialize
13
- @model = nil
14
- @serializer_class = nil
15
- @policy = nil
16
- @attributes = {}
17
- @links = {}
18
- @curies = {}
19
- @embeds = {}
20
- @md = {}
21
- end
22
-
23
- def model
24
- @model || @serializer_class && @serializer_class.sub("Serializer", "")
25
- end
26
-
27
- def attribute(attr, comment)
28
- @attributes[attr] = comment unless comment.empty?
29
- end
30
-
31
- def link(rel, comment)
32
- @links[strip_curie(rel)] = comment unless comment.empty?
33
- end
34
-
35
- def curie(rel, comment)
36
- @curies[rel] = comment unless comment.empty?
37
- end
38
-
39
- def embedded(name, comment)
40
- @embeds[strip_curie(name)] = comment unless comment.empty?
41
- end
42
-
43
- def valid?
44
- return false unless model
45
- attributes.merge(links).merge(curies).any?
46
- end
47
-
48
- def generate_markdown!
49
- return @md unless @md.empty?
50
-
51
- generate_title!
52
- generate_policy!
53
- generate_section!(key: :attributes, heading: "Attributes")
54
- generate_section!(key: :curies, heading: "Curies", sub_title: "rel")
55
- generate_section!(key: :links, heading: "Links", sub_title: "rel")
56
- generate_section!(key: :embeds, heading: "Embedded resources", sub_title: "rel")
57
- @md[:doc]
58
- end
59
-
60
- def generate_yaml!
61
- generate_markdown!
62
- renderer = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
63
-
64
- hash = {}
65
- hash['policy'] = renderer.render(@md[:policy]).chomp if @md[:policy]
66
-
67
- [:attributes, :curies, :links, :embeds].each do |key|
68
- hash[key.to_s] = @md[key].map { |k, v| [k.to_s, renderer.render(v).chomp] }.to_h
69
- end
70
- hash.to_yaml
71
- end
72
-
73
- def markdown
74
- generate_markdown!
75
- @md[:doc]
76
- end
77
-
78
- def to_html
79
- options = {autolink: true, fenced_code_blocks: true}
80
- markdown_renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
81
- html = markdown_renderer.render markdown
82
- # For some reason redcarpet don't like to surround my markdown code blocks
83
- # with <pre> tags, so let's fix that here.
84
- html.gsub!("<code>", "<pre><code>")
85
- html.gsub!("</code>", "</code></pre>")
86
- html
87
- end
88
-
89
- def write_html(output)
90
- FileUtils.mkdir_p(output) unless Dir.exist? output
91
- File.open(File.join(output, "#{model.downcase}.html"), "w") do |file|
92
- file.write(to_html)
93
- end
94
- end
95
-
96
- def write_yaml(output)
97
- FileUtils.mkdir_p(output) unless Dir.exist? output
98
- File.open(File.join(output, "#{model.downcase}.yml"), "w") do |file|
99
- file.write(generate_yaml!)
100
- end
101
- end
102
-
103
-
104
- private
105
-
106
- def strip_curie(rel)
107
- rel.split(':', 2)[1] || rel
108
- end
109
-
110
- def generate_title!
111
- @md[:doc] = "# #{model.capitalize}\n"
112
- @md[:title] = model.capitalize
113
- end
114
-
115
- def generate_policy!
116
- return if policy.nil?
117
- @md[:doc] << "## Policy\n#{policy}\n"
118
- @md[:policy] = policy
119
- end
120
-
121
- def generate_section!(key:, heading:, sub_title: "")
122
- list = send(key)
123
- @md[:doc] << "## #{heading}\n"
124
- @md[key] = {}
125
- if list.empty?
126
- @md[:doc] << "This resource does not have any documented #{heading.downcase}\n"
127
- else
128
- sub_title += ": " unless sub_title.empty?
129
- list.each do |name, comment|
130
- @md[:doc] << "- *#{sub_title}#{name.tr('_', '-')}*\n #{comment.to_s.gsub("\n", "\n ")}\n\n"
131
- @md[key][name] = comment.to_s.chomp
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
@@ -1,48 +0,0 @@
1
- require 'digest'
2
-
3
- module Shaf
4
- module CurrentUser
5
- def self.registered(settings)
6
- return unless settings.respond_to?(:current_user) && settings.current_user
7
-
8
- settings.log.info 'Using Shaf::CurrentUser'
9
- settings.helpers Helpers
10
- end
11
-
12
- def self.digest(token)
13
- Digest::SHA256.hexdigest(token) if token
14
- end
15
- end
16
-
17
- module Helpers
18
- ERR_MSG = 'The default Shaf implementation of #current_user requires a ' \
19
- 'User model with a column auth_token_digest'.freeze
20
-
21
- def auth_token
22
- header = Utils.rackify_header(settings.auth_token_header)
23
- request.env[header]
24
- end
25
-
26
- def current_user
27
- return @current_user if defined?(@current_user)
28
-
29
- return unless check_user_model
30
- digest = Shaf::CurrentUser.digest(auth_token) || return
31
- @current_user = User.where(auth_token_digest: digest).first
32
- end
33
-
34
- def authenticated?
35
- !current_user.nil?
36
- end
37
-
38
- def authenticate!
39
- authenticated? || raise(Shaf::Errors::UnauthorizedError)
40
- end
41
-
42
- def check_user_model
43
- return true if defined?(User) && User.columns.include?(:auth_token_digest)
44
- log.warn ERR_MSG
45
- false
46
- end
47
- end
48
- end
@@ -1 +0,0 @@
1
- require 'shaf/middleware/request_id'
@@ -1,64 +0,0 @@
1
- require 'hal_presenter'
2
- require 'shaf/errors'
3
-
4
- module Shaf
5
- module Responder
6
- module HalSerializable
7
- def lookup_rel(rel, response)
8
- hal = response.serialized_hash
9
- links = hal&.dig(:_links, rel.to_sym)
10
- return [] unless links
11
-
12
- links = [links] unless links.is_a? Array
13
- links.map do |link|
14
- {
15
- href: link[:href],
16
- as: 'fetch',
17
- crossorigin: 'anonymous'
18
- }
19
- end
20
- end
21
-
22
- def collection?
23
- !!options.fetch(:collection, false)
24
- end
25
-
26
- def serializer
27
- @serializer ||= options[:serializer] || HALPresenter.lookup_presenter(resource)
28
- end
29
-
30
- def serialized_hash
31
- return {} unless serializer
32
-
33
- @serialized_hash ||=
34
- if collection?
35
- serializer.to_collection(resource, current_user: user, as_hash: true, **options)
36
- else
37
- serializer.to_hal(resource, current_user: user, as_hash: true, **options)
38
- end
39
-
40
- # hal_presenter versions before v1.5.0 does not understand the :as_hash
41
- # keyword argument and will always return a String from
42
- # to_hal/to_collection, thus we need to parse it if its a String.
43
- if @serialized_hash.is_a? String
44
- @body = @serialized_hash
45
- @serialized_hash = JSON.parse(@serialized_hash, symbolize_names: true)
46
- end
47
-
48
- @serialized_hash
49
- end
50
-
51
- def profile
52
- @profile ||= options[:profile]
53
- return unless @profile || serializer
54
-
55
- @profile ||= serializer.semantic_profile
56
- end
57
-
58
- def generate_json
59
- # FIXME: change to Oj??
60
- JSON.generate(serialized_hash)
61
- end
62
- end
63
- end
64
- end
@@ -1,125 +0,0 @@
1
- require 'shaf/api_doc/document'
2
- require 'shaf/api_doc/comment'
3
-
4
- module Shaf
5
- module Tasks
6
- class ApiDocTask
7
- include Rake::DSL
8
-
9
- attr_accessor :document_class, :source_dir, :html_output_dir, :yaml_output_dir
10
-
11
- def initialize
12
- yield self if block_given?
13
- validate_attributes!
14
- @document_class ||= ApiDoc::Document
15
- define_tasks
16
- end
17
-
18
- def validate_attributes!
19
- raise "source_dir must be set!" unless source_dir
20
- raise "html_output_dir must be configured in ApiDocTask" unless html_output_dir
21
- raise "yaml_output_dir must be configured in ApiDocTask" unless yaml_output_dir
22
- end
23
-
24
- def define_tasks
25
- namespace :doc do
26
- desc "Generate API documentation"
27
- task :generate do
28
- files = Dir.glob(File.join(source_dir, "*.rb"))
29
- files.each do |file|
30
- read_file file do |doc|
31
- next unless doc.valid?
32
- doc.write_html @html_output_dir
33
- doc.write_yaml @yaml_output_dir
34
- end
35
- end
36
- end
37
-
38
- desc "Remove generated documentation"
39
- task :clean do
40
- [
41
- Dir.glob(File.join(@yaml_output_dir, "*.yml")),
42
- Dir.glob(File.join(@html_output_dir, "*.html"))
43
- ].flatten.each do |file|
44
- File.unlink file
45
- end
46
- end
47
- end
48
- end
49
-
50
- def read_file(file)
51
- doc = document_class.new
52
- comment = ApiDoc::Comment.new
53
-
54
- File.readlines(file).each do |line|
55
- next if empty_line?(line)
56
-
57
- if c = comment(line)
58
- comment << c
59
- next
60
- end
61
-
62
- parse_line(line, doc, comment)
63
- comment = ApiDoc::Comment.new
64
- end
65
-
66
- return doc unless block_given?
67
- yield doc
68
- end
69
-
70
- def parse_line(line, doc, comment)
71
- if model = model(line)
72
- doc.model = model
73
- elsif serializer_class = serializer_class(line)
74
- doc.serializer_class = serializer_class
75
- elsif policy = policy(line)
76
- doc.policy = policy
77
- elsif attr = attribute(line)
78
- doc.attribute(attr, comment)
79
- elsif rel = link(line)
80
- doc.link(rel, comment)
81
- elsif rel = curie(line)
82
- doc.curie(rel, comment)
83
- elsif name = embed(line)
84
- doc.embedded(name, comment)
85
- end
86
- end
87
-
88
- def empty_line?(line)
89
- true if line[/\A[#\s*]*\Z/]
90
- end
91
-
92
- def serializer_class(line)
93
- line[/\A\s*class\s*(\w+)\Z/, 1]
94
- end
95
-
96
- def model(line)
97
- line[/\A\s*model\s*(?:::)?(\w+)/, 1]
98
- end
99
-
100
- def policy(line)
101
- line[/\A\s*policy\s*(?:::)?(\w+)/, 1]
102
- end
103
-
104
- def comment(line)
105
- line[/\A\s*#(.*)/, 1]
106
- end
107
-
108
- def attribute(line)
109
- line[/\A\s*attribute\s+\(?:(\w+)/, 1]
110
- end
111
-
112
- def link(line)
113
- line[/\A\s*link\s+\(?:?['"]?([-:\w]+)['"]?/, 1]
114
- end
115
-
116
- def curie(line)
117
- line[/\A\s*curie\s+\(?:'?([-\w]+)'?/, 1]
118
- end
119
-
120
- def embed(line)
121
- line[/\A\s*embed\s+\(?:'?([-:\w]+)'?/, 1]
122
- end
123
- end
124
- end
125
- end