Class: ActionSpec::Doc::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/action_spec/doc/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



43
44
45
46
47
48
49
50
51
52
# File 'lib/action_spec/doc/endpoint.rb', line 43

def initialize
  @header = Location.new(:header)
  @path = Location.new(:path)
  @query = Location.new(:query)
  @cookie = Location.new(:cookie)
  @body = Location.new(:body)
  @body_media_types = {}
  @scope_options = ActiveSupport::HashWithIndifferentAccess.new
  @body_required = false
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def body
  @body
end

#body_media_typesObject (readonly)

Returns the value of attribute body_media_types.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def body_media_types
  @body_media_types
end

Returns the value of attribute cookie.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def cookie
  @cookie
end

#headerObject (readonly)

Returns the value of attribute header.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def header
  @header
end

#pathObject (readonly)

Returns the value of attribute path.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def query
  @query
end

#scope_optionsObject (readonly)

Returns the value of attribute scope_options.



41
42
43
# File 'lib/action_spec/doc/endpoint.rb', line 41

def scope_options
  @scope_options
end

Instance Method Details

#add_body(media_type, field) ⇒ Object



63
64
65
66
67
# File 'lib/action_spec/doc/endpoint.rb', line 63

def add_body(media_type, field)
  body.add(field)
  (@body_media_types[media_type.to_sym] ||= Location.new(media_type.to_sym)).add(field.copy)
  clear_custom_validation_cache!
end

#add_param(location_name, field) ⇒ Object



58
59
60
61
# File 'lib/action_spec/doc/endpoint.rb', line 58

def add_param(location_name, field)
  location(location_name).add(field)
  clear_custom_validation_cache!
end

#body_required?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/action_spec/doc/endpoint.rb', line 83

def body_required?
  @body_required
end

#copyObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/action_spec/doc/endpoint.rb', line 99

def copy
  self.class.new.tap do |request|
    request.instance_variable_set(:@header, header.copy)
    request.instance_variable_set(:@path, path.copy)
    request.instance_variable_set(:@query, query.copy)
    request.instance_variable_set(:@cookie, cookie.copy)
    request.instance_variable_set(:@body, body.copy)
    request.instance_variable_set(
      :@body_media_types,
      body_media_types.transform_values(&:copy)
    )
    request.instance_variable_set(:@scope_options, scope_options.deep_dup)
    request.instance_variable_set(:@body_required, body_required?)
  end
end

#custom_validation?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/action_spec/doc/endpoint.rb', line 115

def custom_validation?
  custom_validation_locations.any?
end

#custom_validation_locationsObject



119
120
121
# File 'lib/action_spec/doc/endpoint.rb', line 119

def custom_validation_locations
  @custom_validation_locations ||= [header, path, query, cookie, body].select(&:custom_validation?).freeze
end

#location(name) ⇒ Object



54
55
56
# File 'lib/action_spec/doc/endpoint.rb', line 54

def location(name)
  public_send(name)
end

#register_scope(name, compact: nil, compact_blank: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/action_spec/doc/endpoint.rb', line 69

def register_scope(name, compact: nil, compact_blank: nil)
  key = name.to_sym
  @scope_options[key] = scope_options.fetch(key, {}).merge(
    {
      compact:,
      compact_blank:
    }.compact
  )
end

#replace_with(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/action_spec/doc/endpoint.rb', line 87

def replace_with(other)
  @header = other.header
  @path = other.path
  @query = other.query
  @cookie = other.cookie
  @body = other.body
  @body_media_types = other.body_media_types
  @scope_options = other.scope_options
  @body_required = other.body_required?
  clear_custom_validation_cache!
end

#require_body!Object



79
80
81
# File 'lib/action_spec/doc/endpoint.rb', line 79

def require_body!
  @body_required = true
end