Class: Nexmo::OAS::Renderer::Presenters::RequestBodyRaw
- Inherits:
-
Object
- Object
- Nexmo::OAS::Renderer::Presenters::RequestBodyRaw
- Defined in:
- lib/nexmo/oas/renderer/presenters/request_body_raw.rb
Instance Method Summary collapse
- #array?(parameter) ⇒ Boolean
- #collection?(parameter) ⇒ Boolean
- #example(parameter) ⇒ Object
- #generate_request(parameters = nil, options = nil) ⇒ Object
-
#initialize(parameters, options = {}, endpoint = nil) ⇒ RequestBodyRaw
constructor
A new instance of RequestBodyRaw.
- #items(parameter) ⇒ Object
- #name(parameter) ⇒ Object
- #optional?(parameter, allow_list) ⇒ Boolean
- #properties(parameter) ⇒ Object
- #properties?(parameter) ⇒ Boolean
- #to_format(format) ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_urlencoded ⇒ Object
Constructor Details
#initialize(parameters, options = {}, endpoint = nil) ⇒ RequestBodyRaw
Returns a new instance of RequestBodyRaw.
8 9 10 11 12 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 8 def initialize(parameters, = {}, endpoint = nil) @parameters = parameters @options = @endpoint = endpoint end |
Instance Method Details
#array?(parameter) ⇒ Boolean
110 111 112 113 114 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 110 def array?(parameter) return parameter['items'] unless parameter.respond_to?(:array?) parameter.array? end |
#collection?(parameter) ⇒ Boolean
116 117 118 119 120 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 116 def collection?(parameter) return parameter['properties'] unless parameter.respond_to?(:collection?) parameter.collection? end |
#example(parameter) ⇒ Object
92 93 94 95 96 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 92 def example(parameter) return parameter['example'] unless parameter.respond_to?(:example) parameter.example end |
#generate_request(parameters = nil, options = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 41 def generate_request(parameters = nil, = nil) parameters ||= @parameters ||= @options output = {} parameters.each do |parameter| next if ['required_only'] && optional?(parameter, ['required']) parameter_name = name(parameter) param = parameter # This is all required to handle an edge case where parameter.name is an OasParser::Property # Which happens when you use a oneOf inside items in a property. # I believe this is a bug, but it's a BC break in the parser if parameter_name.instance_of?(OasParser::Property) parameter_name = parameter.owner.name param = OasParser::Parameter.new(parameter_name, parameter.schema) end if param.raw['items'] && param.raw['example'] output[parameter_name] = param.raw['example'] elsif param.raw['items'] && param.raw['items']['oneOf'] param = param.raw['items']['oneOf'][0] output[parameter_name] = [generate_request(properties(param).map(&:name))] elsif collection?(param) && properties?(param) nested_output = generate_request(properties(param)) next unless nested_output.keys.length.positive? nested_output = [nested_output] if param.array? output[parameter_name] = nested_output else ex = example(param) next unless ex if ex.is_a?(String) # Remove line breaks ex = ex.gsub('<br />', ' ') end output[parameter_name] = ex end end output end |
#items(parameter) ⇒ Object
86 87 88 89 90 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 86 def items(parameter) return parameter['items'] unless parameter.respond_to?(:items) parameter.items end |
#name(parameter) ⇒ Object
98 99 100 101 102 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 98 def name(parameter) return parameter['name'] unless parameter.respond_to?(:name) parameter.name end |
#optional?(parameter, allow_list) ⇒ Boolean
128 129 130 131 132 133 134 135 136 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 128 def optional?(parameter, allow_list) return false if allow_list&.include?(name(parameter)) return false unless parameter.respond_to?(:required) return false unless parameter.schema !parameter.required end |
#properties(parameter) ⇒ Object
104 105 106 107 108 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 104 def properties(parameter) return parameter['properties'] unless parameter.respond_to?(:properties) parameter.properties end |
#properties?(parameter) ⇒ Boolean
122 123 124 125 126 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 122 def properties?(parameter) return parameter['properties'] unless parameter.respond_to?(:collection?) parameter.properties.size.positive? end |
#to_format(format) ⇒ Object
14 15 16 17 18 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 14 def to_format(format) return to_urlencoded if format == 'application/x-www-form-urlencoded' to_json end |
#to_json(*_args) ⇒ Object
37 38 39 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 37 def to_json(*_args) JSON.pretty_generate(generate_request) end |
#to_urlencoded ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nexmo/oas/renderer/presenters/request_body_raw.rb', line 20 def to_urlencoded example = '' body = URI.encode_www_form(generate_request) if @endpoint servers = @endpoint.path.servers || @endpoint.definition.servers path = @endpoint.path.path.gsub(/\{(.+?)\}/, ':\1') uri = URI("#{servers[0]['url']}#{path}") example += "#{@endpoint.method.upcase} #{uri.path} HTTP/1.1\n" example += "Host: #{uri.host} \n" example += "Content-Type: application/x-www-form-urlencoded\n" example += "Content-Length: #{body.length}\n" example += "\n" end example += body example end |