Class: RSpec::Rails::Api::Metadata
- Inherits:
-
Object
- Object
- RSpec::Rails::Api::Metadata
- Defined in:
- lib/rspec/rails/api/metadata.rb
Overview
Handles contexts and examples metadatas.
Instance Attribute Summary collapse
-
#current_resource ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#entities ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#resources ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
- #add_action(method, url, description) ⇒ Object
- #add_entity(type, fields) ⇒ Object
-
#add_path_params(fields) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#add_request_example(url: nil, action: nil, status_code: nil, response: nil, path_params: nil, params: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#add_request_params(fields) ⇒ Object
Fields should be something like: id: :number, description: ‘Something’, name: string, description: ‘Something’ Ex.
- #add_resource(name, description) ⇒ Object
-
#add_status_code(status_code, description) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#initialize ⇒ Metadata
constructor
A new instance of Metadata.
-
#to_h ⇒ Object
rubocop:enable Metrics/ParameterLists.
Constructor Details
#initialize ⇒ Metadata
Returns a new instance of Metadata.
14 15 16 17 18 19 20 21 22 |
# File 'lib/rspec/rails/api/metadata.rb', line 14 def initialize @resources = {} @entities = {} # Only used when building metadata during RSpec boot @current_resource = nil @current_method = nil @current_url = nil @current_code = nil end |
Instance Attribute Details
#current_resource ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/rspec/rails/api/metadata.rb', line 12 def current_resource @current_resource end |
#entities ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/rspec/rails/api/metadata.rb', line 12 def entities @entities end |
#resources ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/rspec/rails/api/metadata.rb', line 12 def resources @resources end |
Instance Method Details
#add_action(method, url, description) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rspec/rails/api/metadata.rb', line 71 def add_action(method, url, description) check_current_context :resource Utils.deep_set(@resources, "#{@current_resource}.paths.#{url}.actions.#{method}", description: description, statuses: {}, params: []) @current_url = url @current_method = method end |
#add_entity(type, fields) ⇒ Object
29 30 31 32 33 |
# File 'lib/rspec/rails/api/metadata.rb', line 29 def add_entity(type, fields) Utils.deep_set(@resources, "#{@current_resource}.entities.#{type}", EntityConfig.new(fields)) end |
#add_path_params(fields) ⇒ Object
rubocop:disable Metrics/MethodLength
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rspec/rails/api/metadata.rb', line 35 def add_path_params(fields) # rubocop:disable Metrics/MethodLength check_current_context :resource, :url chunks = @current_url.split('?') fields.each do |name, field| valid_attribute = Utils.check_attribute_type(field[:type], except: i[array object]) raise "Field type not allowed: #{field[:type]}" unless valid_attribute scope = path_param_scope(chunks, name) Utils.deep_set(@resources, "#{@current_resource}.paths.#{@current_url}.path_params.#{name}", description: field[:description] || nil, type: field[:type] || nil, required: field[:required] || true, scope: scope) end end |
#add_request_example(url: nil, action: nil, status_code: nil, response: nil, path_params: nil, params: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rspec/rails/api/metadata.rb', line 96 def add_request_example(url: nil, action: nil, status_code: nil, response: nil, path_params: nil, params: nil) resource = nil @resources.each do |key, res| resource = key if Utils.deep_get(res, "paths.#{url}.actions.#{action}.statuses.#{status_code}") end raise "Resource not found for #{action.upcase} #{url}" unless resource Utils.deep_set(@resources, "#{resource}.paths.#{url}.actions.#{action}.statuses.#{status_code}.example", path_params: path_params, params: params, response: response) end |
#add_request_params(fields) ⇒ Object
Fields should be something like:
id: {type: :number, description: 'Something'},
name: {type: string, description: 'Something'}
Ex. with sub elements:
id: {type: :number, description: 'Something'},
something: {type: :object, description: 'Something', properties: {
property: {type: :string, description: 'Something'},
...
}}
62 63 64 65 66 67 68 69 |
# File 'lib/rspec/rails/api/metadata.rb', line 62 def add_request_params(fields) check_current_context :resource, :url, :method params = organize_params fields Utils.deep_set(@resources, "#{@current_resource}.paths.#{@current_url}.actions.#{@current_method}.params", params) end |
#add_resource(name, description) ⇒ Object
24 25 26 27 |
# File 'lib/rspec/rails/api/metadata.rb', line 24 def add_resource(name, description) @resources[name.to_sym] = { description: description, paths: {} } @current_resource = name.to_sym end |
#add_status_code(status_code, description) ⇒ Object
rubocop:disable Metrics/LineLength
84 85 86 87 88 89 90 91 92 |
# File 'lib/rspec/rails/api/metadata.rb', line 84 def add_status_code(status_code, description) check_current_context :resource, :url, :method Utils.deep_set(@resources, "#{@current_resource}.paths.#{@current_url}.actions.#{@current_method}.statuses.#{status_code}", description: description, example: { response: nil }) @current_code = status_code end |
#to_h ⇒ Object
rubocop:enable Metrics/ParameterLists
112 113 114 115 116 117 |
# File 'lib/rspec/rails/api/metadata.rb', line 112 def to_h { resources: @resources, entities: @entities, } end |