oas_rails 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a82d1365d6a90a9901af44fba038995b964bee27d3b55455b064a05250da23da
4
- data.tar.gz: 243af6009f85f7d82a46111a608befab333492ea0e4f24a608f6570e4bfc74eb
3
+ metadata.gz: dff52d1f5445bbcd000de4c14988e919905c7e5ea1bd179b7ac7c458fde9570e
4
+ data.tar.gz: adba7ada7bc1658bf3b65f85731eef40c5f72ce930b3b087da5be23b4abd45a8
5
5
  SHA512:
6
- metadata.gz: 85637d8fd3026357294de1f2577784e6e3789fe17852c5295c5cdc6be2940abb9f65c021549a8ba5d40c5cc44d40cfd38045b8a337ff474da63b540b2425fcfe
7
- data.tar.gz: 322d955002a1885b6aa4f5e62a934d71ab2a3600de1f27bc9aa55c164d3d601c2e237be29dc4407c0519c53c7e1a9263262e896598ddbd1bac1051b2ecd88681
6
+ metadata.gz: 331be1bcbe64381095ab5f62b94154d394340740fe61a43c6463a544c8bc302ae2a89e30b30feedc698673ae4c7eca268b10d59b68e1f607b8eecfdb64917e9b
7
+ data.tar.gz: edfefc2472d3419045df61ce6ca9a37c48e87d61055b6225af54d7633c8cf08c0471ce15392967c564a866f1e24e322e10defed30ac05736d2b77bd1770fa629
@@ -20,23 +20,42 @@ module OasRails
20
20
  new(media_type: "", schema:, examples:)
21
21
  end
22
22
 
23
- def search_for_examples_in_tests(klass:)
24
- case Utils.detect_test_framework
23
+ # Searches for examples in test files based on the provided class and test framework.
24
+ #
25
+ # This method handles different test frameworks to fetch examples for the given class.
26
+ # Currently, it supports FactoryBot and fixtures.
27
+ #
28
+ # @param klass [Class] the class to search examples for.
29
+ # @param utils [Module] a utility module that provides the `detect_test_framework` method. Defaults to `Utils`.
30
+ # @return [Hash] a hash containing examples data or an empty hash if no examples are found.
31
+ # @example Usage with FactoryBot
32
+ # search_for_examples_in_tests(klass: User)
33
+ #
34
+ # @example Usage with fixtures
35
+ # search_for_examples_in_tests(klass: Project)
36
+ #
37
+ # @example Usage with a custom utils module
38
+ # custom_utils = Module.new do
39
+ # def self.detect_test_framework
40
+ # :factory_bot
41
+ # end
42
+ # end
43
+ # search_for_examples_in_tests(klass: User, utils: custom_utils)
44
+ def search_for_examples_in_tests(klass:, utils: Utils)
45
+ case utils.detect_test_framework
25
46
  when :factory_bot
26
47
  {}
27
48
  # TODO: create examples with FactoryBot
28
49
  when :fixtures
29
- # Handle fixtures scenario
30
50
  fixture_file = Rails.root.join('test', 'fixtures', "#{klass.to_s.pluralize.downcase}.yml")
31
- fixture_data = YAML.load_file(fixture_file).with_indifferent_access
32
51
 
33
- users_hash = {}
34
-
35
- # Convert the fixture data to a hash
36
- fixture_data.each do |name, attributes|
37
- users_hash[name] = { value: { klass.to_s.downcase => attributes } }
52
+ begin
53
+ fixture_data = YAML.load_file(fixture_file).with_indifferent_access
54
+ rescue Errno::ENOENT
55
+ return {}
38
56
  end
39
- users_hash
57
+
58
+ fixture_data.transform_values { |attributes| { value: { klass.to_s.downcase => attributes } } }
40
59
  else
41
60
  {}
42
61
  end
@@ -76,13 +76,36 @@ module OasRails
76
76
  end
77
77
 
78
78
  def valid_api_route?(route)
79
- return false if route.defaults[:controller].nil?
79
+ return false unless valid_route_implementation?(route)
80
80
  return false if RAILS_DEFAULT_CONTROLLERS.any? { |default| route.defaults[:controller].start_with?(default) }
81
81
  return false if RAILS_DEFAULT_PATHS.any? { |path| route.path.spec.to_s.include?(path) }
82
82
  return false unless route.path.spec.to_s.start_with?(OasRails.config.api_path)
83
83
 
84
84
  true
85
85
  end
86
+
87
+ # Checks if a route has a valid implementation.
88
+ #
89
+ # This method verifies that both the controller and the action specified
90
+ # in the route exist. It checks if the controller class is defined and
91
+ # if the action method is implemented within that controller.
92
+ #
93
+ # @param route [ActionDispatch::Journey::Route] The route to check.
94
+ # @return [Boolean] true if both the controller and action exist, false otherwise.
95
+ def valid_route_implementation?(route)
96
+ controller_name = route.defaults[:controller]&.camelize
97
+ action_name = route.defaults[:action]
98
+
99
+ return false if controller_name.blank? || action_name.blank?
100
+
101
+ controller_class = "#{controller_name}Controller".safe_constantize
102
+
103
+ if controller_class.nil?
104
+ false
105
+ else
106
+ controller_class.instance_methods.include?(action_name.to_sym)
107
+ end
108
+ end
86
109
  end
87
110
  end
88
111
  end
@@ -1,3 +1,3 @@
1
1
  module OasRails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - a-chacon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-30 00:00:00.000000000 Z
11
+ date: 2024-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: esquema