brick 1.0.106 → 1.0.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 988907627ce35c257a41cbcf4b455ec852c9635fe4f8721c5af167e416fbb4ef
4
- data.tar.gz: c4a2fe612aa26a628f347d830197f961cf21b67c825ff968fa589e6bd342d1d7
3
+ metadata.gz: 74bd7cd6539a5f048aecc3e02be8912e1bc77851ac8b9f65402892e964a024de
4
+ data.tar.gz: c32c53d0012f2a9292463d803d0c679d4f32800c8931c3361469aa22c09a3fc5
5
5
  SHA512:
6
- metadata.gz: 3488a28db2bf8f3c7f363e9b2eb4355e9de2d4021db9a39fb45656f896046bee412a7fd310861f522915bbc244af35a2707005757357ee6b9f9877ea331e7240
7
- data.tar.gz: 8a44cbbadde246520b028d0e474226cdf2d43e8d51bed023e20f21a0d5b6cc0e690a397908bcdbdb9b7ef21c7743bec64130258b859d1583b6c0facffd8edf5f
6
+ metadata.gz: f84ee2aae03622a4ca67a6921daf7e975f85601e66fc608c2357613da976414d2ceeec87276887232a832413116ec1495638c883ecc712db3706f51c34efbd44
7
+ data.tar.gz: e97fbe41eca4d839795f7567bfd3e8a3d6494d4887032fa5f0f87d88ba3c9fdc810bde8a259c3cbf0b8f944c8b345c33290aa5d1e8f7aa67737e7fe7be248a6d
@@ -1560,44 +1560,53 @@ class Object
1560
1560
  unless ::Brick.config.enable_api == false
1561
1561
  json['paths'] = relations.each_with_object({}) do |relation, s|
1562
1562
  next if (api_vers = relation.last.fetch(:api, nil)) &&
1563
- !(api_ver_paths = api_vers[current_api_ver])
1563
+ !(api_ver_paths = api_vers[current_api_ver] || api_vers[nil])
1564
1564
 
1565
- # binding.pry if relation.last.fetch(:isView, nil) && api_ver_paths != { relation.first => ::Brick::ALL_API_ACTIONS }
1566
- (api_ver_paths || { relation.first => ::Brick::ALL_API_ACTIONS }).each do |api_ver_path, actions|
1567
- relation_name = api_ver_path || relation.first.tr('.', '/')
1565
+ schema_tag = {}
1566
+ if (schema_name = relation.last&.fetch(:schema, nil))
1567
+ schema_tag['tags'] = [schema_name]
1568
+ end
1569
+ all_actions = relation.last.key?(:isView) ? [:index, :show] : ::Brick::ALL_API_ACTIONS
1570
+ (api_ver_paths || { relation.first => all_actions }).each do |api_ver_path, actions|
1571
+ relation_name = (api_ver_path || relation.first).tr('.', '/')
1568
1572
  table_description = relation.last[:description]
1569
- unless actions&.exclude?(:index)
1570
- s["#{current_api_root}#{relation_name}"] = {
1571
- 'get': {
1572
- 'summary': "list #{relation.first}",
1573
+ { :index => [:get, 'list'], :create => [:post, 'create a'] }.each do |k, v|
1574
+ unless actions&.exclude?(k)
1575
+ this_resource = (s["#{current_api_root}#{relation_name}"] ||= {})
1576
+ this_resource[v.first] = {
1577
+ 'summary': "#{v[1]} #{relation.first}",
1573
1578
  'description': table_description,
1574
- 'parameters': relation.last[:cols].map do |k, v|
1575
- param = { in: 'query', 'name' => k, 'schema': { 'type': v.first } }
1576
- if (col_descrip = relation.last.fetch(:col_descrips, nil)&.fetch(k, nil))
1579
+ 'parameters': relation.last[:cols].map do |k2, v2|
1580
+ param = { in: 'query', 'name': k2, 'schema': { 'type': v2.first } }
1581
+ if (col_descrip = relation.last.fetch(:col_descrips, nil)&.fetch(k2, nil))
1577
1582
  param['description'] = col_descrip
1578
1583
  end
1579
1584
  param
1580
1585
  end,
1581
1586
  'responses': { '200': { 'description': 'successful' } }
1582
- }
1583
- }
1587
+ }.merge(schema_tag)
1588
+ end
1584
1589
  end
1585
1590
 
1586
- unless actions&.exclude?(:update)
1587
- s["#{current_api_root}#{relation_name}/{id}"] = {
1588
- 'patch': {
1589
- 'summary': "update a #{relation.first.singularize}",
1590
- 'description': table_description,
1591
- 'parameters': relation.last[:cols].reject { |k, v| Brick.config.metadata_columns.include?(k) }.map do |k, v|
1592
- param = { 'name' => k, 'schema': { 'type': v.first } }
1593
- if (col_descrip = relation.last.fetch(:col_descrips, nil)&.fetch(k, nil))
1594
- param['description'] = col_descrip
1595
- end
1596
- param
1597
- end,
1598
- 'responses': { '200': { 'description': 'successful' } }
1599
- }
1600
- } unless relation.last.fetch(:isView, nil)
1591
+ # We have not yet implemented the #show action
1592
+ if (id_col = relation.last[:pkey]&.values&.first&.first) # ... ID-dependent stuff
1593
+ { :update => [:patch, 'update'], :destroy => [:delete, 'delete'] }.each do |k, v|
1594
+ unless actions&.exclude?(k)
1595
+ this_resource = (s["#{current_api_root}#{relation_name}/{#{id_col}}"] ||= {})
1596
+ this_resource[v.first] = {
1597
+ 'summary': "#{v[1]} a #{relation.first.singularize}",
1598
+ 'description': table_description,
1599
+ 'parameters': relation.last[:cols].reject { |k1, _v1| Brick.config.metadata_columns.include?(k1) }.map do |k2, v2|
1600
+ param = { 'name': k2, 'schema': { 'type': v2.first } }
1601
+ if (col_descrip = relation.last.fetch(:col_descrips, nil)&.fetch(k2, nil))
1602
+ param['description'] = col_descrip
1603
+ end
1604
+ param
1605
+ end,
1606
+ 'responses': { '200': { 'description': 'successful' } }
1607
+ }.merge(schema_tag)
1608
+ end
1609
+ end
1601
1610
  end
1602
1611
  end # Do multiple api_ver_paths
1603
1612
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 106
8
+ TINY = 107
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
data/lib/brick.rb CHANGED
@@ -678,7 +678,10 @@ In config/initializers/brick.rb appropriate entries would look something like:
678
678
  ::Brick.relations.each do |k, v|
679
679
  next if !(controller_name = v.fetch(:resource, nil)&.pluralize) || existing_controllers.key?(controller_name)
680
680
 
681
- schema_name = v.fetch(:schema, nil)
681
+ object_name = k.split('.').last # Take off any first schema part
682
+ if (schema_name = v.fetch(:schema, nil))
683
+ schema_prefix = "#{schema_name}."
684
+ end
682
685
  options = {}
683
686
  options[:only] = [:index, :show] if v.key?(:isView)
684
687
  # First do the API routes if necessary
@@ -687,14 +690,11 @@ In config/initializers/brick.rb appropriate entries would look something like:
687
690
  api_done_views = (versioned_views[api_root] ||= {})
688
691
  found = nil
689
692
  view_relation = nil
690
- actions = ::Brick::ALL_API_ACTIONS # By default all actions are allowed
691
693
  # If it's a view then see if there's a versioned one available by searching for resource names
692
694
  # versioned with the closest number (equal to or less than) compared with our API version number.
693
- if v.key?(:isView) && (ver = k.match(/^v([\d_]*)/).captures.first)[-1] == '_'
694
- next if api_done_views.key?(unversioned = k[ver.length + 1..-1])
695
-
696
- # # if ().length.positive? # Does it have a version number?
697
- # try_num = (ver_num = (ver = ver[1..-1].gsub('_', '.')).to_d)
695
+ if v.key?(:isView) && (ver = object_name.match(/^v([\d_]*)/)&.captures&.first) && ver[-1] == '_'
696
+ core_object_name = object_name[ver.length + 1..-1]
697
+ next if api_done_views.key?(unversioned = "#{schema_prefix}v_#{core_object_name}")
698
698
 
699
699
  # Expect that the last item in the path generally holds versioning information
700
700
  api_ver = api_root.split('/')[-1]&.gsub('_', '.')
@@ -707,79 +707,95 @@ In config/initializers/brick.rb appropriate entries would look something like:
707
707
 
708
708
  test_ver_num -= 1 until test_ver_num.zero? ||
709
709
  (view_relation = ::Brick.relations.fetch(
710
- found = "v#{test_ver_num}_#{k[ver.length + 1..-1]}", nil
710
+ found = "#{schema_prefix}v#{test_ver_num}_#{core_object_name}", nil
711
711
  ))
712
712
  api_done_views[unversioned] = nil # Mark that for this API version this view is done
713
713
 
714
714
  # puts "Found #{found}" if view_relation
715
715
  # If we haven't found "v3_view_name" or "v2_view_name" or so forth, at the last
716
716
  # fall back to simply looking for "v_view_name", and then finally "view_name".
717
- unversioned = "v_#{unversioned}"
718
- view_relation ||= ::Brick.relations.fetch(found = unversioned,
719
- ::Brick.relations.fetch(found = unversioned, nil)
720
- )
721
- if found && view_relation && k != unversioned
722
- # Call proc that limits which endpoints get surfaced based on version, table or view name, method (get list / get one / post / patch / delete)
723
- # Returning nil makes it do nothing, false makes it skip creating this endpoint, and an array of up to
724
- # these 3 things controls and changes the nature of the endpoint that gets built:
725
- # (updated api_name, name of different relation to route to, allowed actions such as :index, :show, :create, etc)
726
- proc_result = if (filter = ::Brick.config.api_filter).is_a?(Proc)
727
- begin
728
- filter.call(unversioned, k, actions, api_ver_num, found, test_ver_num)
729
- rescue StandardError => e
730
- puts "::Brick.api_filter Proc error: #{e.message}"
731
- end
717
+ no_v_prefix_name = "#{schema_prefix}#{core_object_name}"
718
+ else
719
+ unversioned = k
720
+ end
721
+
722
+ view_relation ||= ::Brick.relations.fetch(found = unversioned, nil) ||
723
+ (no_v_prefix_name && ::Brick.relations.fetch(found = no_v_prefix_name, nil))
724
+ if view_relation
725
+ actions = view_relation.key?(:isView) ? [:index, :show] : ::Brick::ALL_API_ACTIONS # By default all actions are allowed
726
+ # Call proc that limits which endpoints get surfaced based on version, table or view name, method (get list / get one / post / patch / delete)
727
+ # Returning nil makes it do nothing, false makes it skip creating this endpoint, and an array of up to
728
+ # these 3 things controls and changes the nature of the endpoint that gets built:
729
+ # (updated api_name, name of different relation to route to, allowed actions such as :index, :show, :create, etc)
730
+ proc_result = if (filter = ::Brick.config.api_filter).is_a?(Proc)
731
+ begin
732
+ num_args = filter.arity.negative? ? 6 : filter.arity
733
+ filter.call(*[unversioned, k, actions, api_ver_num, found, test_ver_num][0...num_args])
734
+ rescue StandardError => e
735
+ puts "::Brick.api_filter Proc error: #{e.message}"
732
736
  end
733
- # proc_result expects to receive back: [updated_api_name, to_other_relation, allowed_actions]
734
-
735
- case proc_result
736
- when NilClass
737
- # Do nothing differently than what normal behaviour would be
738
- when FalseClass # Skip implementing this endpoint
739
- view_relation[:api][api_ver_num] = nil
740
- next
741
- when Array # Did they give back an array of actions?
742
- unless proc_result.any? { |pr| ::Brick::ALL_API_ACTIONS.exclude?(pr) }
743
- proc_result = [unversioned, to_relation, proc_result]
744
- end
745
- # Otherwise don't change this array because it's probably legit
746
- when String
747
- proc_result = [proc_result] # Treat this as the surfaced api_name (path) they want to use for this endpoint
748
- else
749
- puts "::Brick.api_filter Proc warning: Unable to parse this result returned: \n #{proc_result.inspect}"
750
- proc_result = nil # Couldn't understand what in the world was returned
737
+ end
738
+ # proc_result expects to receive back: [updated_api_name, to_other_relation, allowed_actions]
739
+
740
+ case proc_result
741
+ when NilClass
742
+ # Do nothing differently than what normal behaviour would be
743
+ when FalseClass # Skip implementing this endpoint
744
+ view_relation[:api][api_ver_num] = nil
745
+ next
746
+ when Array # Did they give back an array of actions?
747
+ unless proc_result.any? { |pr| ::Brick::ALL_API_ACTIONS.exclude?(pr) }
748
+ proc_result = [unversioned, to_relation, proc_result]
751
749
  end
750
+ # Otherwise don't change this array because it's probably legit
751
+ when String
752
+ proc_result = [proc_result] # Treat this as the surfaced api_name (path) they want to use for this endpoint
753
+ else
754
+ puts "::Brick.api_filter Proc warning: Unable to parse this result returned: \n #{proc_result.inspect}"
755
+ proc_result = nil # Couldn't understand what in the world was returned
756
+ end
752
757
 
753
- if proc_result&.present?
754
- if proc_result[1] # to_other_relation
755
- if (new_view_relation = ::Brick.relations.fetch(proc_result[1], nil))
756
- k = proc_result[1] # Route this call over to this different relation
757
- view_relation = new_view_relation
758
- else
759
- puts "::Brick.api_filter Proc warning: Unable to find new suggested relation with name #{proc_result[1]} -- sticking with #{k} instead."
760
- end
761
- end
762
- if proc_result.first&.!=(k) # updated_api_name -- a different name than this relation would normally have
763
- found = proc_result.first
758
+ if proc_result&.present?
759
+ if proc_result[1] # to_other_relation
760
+ if (new_view_relation = ::Brick.relations.fetch(proc_result[1], nil))
761
+ k = proc_result[1] # Route this call over to this different relation
762
+ view_relation = new_view_relation
763
+ else
764
+ puts "::Brick.api_filter Proc warning: Unable to find new suggested relation with name #{proc_result[1]} -- sticking with #{k} instead."
764
765
  end
765
- actions &= proc_result[2] if proc_result[2] # allowed_actions
766
766
  end
767
+ if proc_result.first&.!=(k) # updated_api_name -- a different name than this relation would normally have
768
+ found = proc_result.first
769
+ end
770
+ actions &= proc_result[2] if proc_result[2] # allowed_actions
767
771
  end
768
- (view_relation[:api][api_ver_num] ||= {})[found] = actions # Add to the list of API paths this resource responds to
769
- end
770
-
771
- # view_ver_num = if (first_part = k.split('_').first) =~ /^v[\d_]+/
772
- # first_part[1..-1].gsub('_', '.').to_i
773
- # end
774
- controller_name = view_relation.fetch(:resource, nil)&.pluralize if view_relation
775
- # %%% So far we can only surface the #index action
776
- if actions.include?(:index)
777
- if schema_name
778
- full_resource = "#{schema_name}/#{found || v[:resource]}"
779
- send(:get, "#{api_root}#{full_resource}", { to: "#{controller_prefix}#{schema_name}/#{controller_name}#index" })
780
- else
781
- # Normally goes to something like: /api/v1/employees
782
- send(:get, "#{api_root}#{found || v[:resource]}", { to: "#{controller_prefix}#{controller_name}#index" })
772
+ (view_relation[:api][api_ver_num] ||= {})[unversioned] = actions # Add to the list of API paths this resource responds to
773
+
774
+ # view_ver_num = if (first_part = k.split('_').first) =~ /^v[\d_]+/
775
+ # first_part[1..-1].gsub('_', '.').to_i
776
+ # end
777
+
778
+ controller_name = if (last = view_relation.fetch(:resource, nil)&.pluralize)
779
+ "#{schema_prefix}#{last}"
780
+ else
781
+ found
782
+ end.tr('.', '/')
783
+
784
+ { :index => 'get', :create => 'post' }.each do |action, method|
785
+ if actions.include?(action)
786
+ # Normally goes to something like: /api/v1/employees
787
+ send(method, "#{api_root}#{unversioned.tr('.', '/')}", { to: "#{controller_prefix}#{controller_name}##{action}" })
788
+ end
789
+ end
790
+ # %%% We do not yet surface the #show action
791
+ if (id_col = view_relation[:pk]&.first) # ID-dependent stuff
792
+ { :update => ['put', 'patch'], :destroy => ['delete'] }.each do |action, methods|
793
+ if actions.include?(action)
794
+ methods.each do |method|
795
+ send(method, "#{api_root}#{unversioned.tr('.', '/')}/:#{id_col}", { to: "#{controller_prefix}#{controller_name}##{action}" })
796
+ end
797
+ end
798
+ end
783
799
  end
784
800
  end
785
801
  end
@@ -792,7 +808,7 @@ In config/initializers/brick.rb appropriate entries would look something like:
792
808
  else
793
809
  table_class_length = class_name.length if class_name.length > table_class_length
794
810
  tables
795
- end << [class_name, full_resource || v[:resource]]
811
+ end << [class_name, "#{schema_prefix&.tr('.', '/')}#{v[:resource]}"]
796
812
  end
797
813
 
798
814
  # Now the normal routes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.106
4
+ version: 1.0.107
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-18 00:00:00.000000000 Z
11
+ date: 2023-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord