brick 1.0.106 → 1.0.107
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/brick/extensions.rb +37 -28
- data/lib/brick/version_number.rb +1 -1
- data/lib/brick.rb +85 -69
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74bd7cd6539a5f048aecc3e02be8912e1bc77851ac8b9f65402892e964a024de
|
4
|
+
data.tar.gz: c32c53d0012f2a9292463d803d0c679d4f32800c8931c3361469aa22c09a3fc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f84ee2aae03622a4ca67a6921daf7e975f85601e66fc608c2357613da976414d2ceeec87276887232a832413116ec1495638c883ecc712db3706f51c34efbd44
|
7
|
+
data.tar.gz: e97fbe41eca4d839795f7567bfd3e8a3d6494d4887032fa5f0f87d88ba3c9fdc810bde8a259c3cbf0b8f944c8b345c33290aa5d1e8f7aa67737e7fe7be248a6d
|
data/lib/brick/extensions.rb
CHANGED
@@ -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
|
-
|
1566
|
-
(
|
1567
|
-
|
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
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
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 |
|
1575
|
-
param = { in: 'query', 'name'
|
1576
|
-
if (col_descrip = relation.last.fetch(:col_descrips, nil)&.fetch(
|
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
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
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
|
data/lib/brick/version_number.rb
CHANGED
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
|
-
|
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 =
|
694
|
-
|
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
|
-
|
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
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
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
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
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
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
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] ||= {})[
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
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,
|
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.
|
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-
|
11
|
+
date: 2023-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|