diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache index a6ad16d376a..e9a8af9c162 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache @@ -1,29 +1,29 @@ module {{moduleName}} -module Swagger -class Configuration -attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl + module Swagger + class Configuration + attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl -# Defaults go in here.. -def initialize -@format = 'json' -@scheme = '{{scheme}}' -@host = '{{host}}' -@base_path = '{{contextPath}}' -@user_agent = "ruby-swagger-#{Swagger::VERSION}" -@inject_format = false -@force_ending_format = false -@camelize_params = true + # Defaults go in here.. + def initialize + @format = 'json' + @scheme = '{{scheme}}' + @host = '{{host}}' + @base_path = '{{contextPath}}' + @user_agent = "ruby-swagger-#{Swagger::VERSION}" + @inject_format = false + @force_ending_format = false + @camelize_params = true -# keys for API key authentication (param-name => api-key) -@api_key = {} -# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix) -@api_key_prefix = {} + # keys for API key authentication (param-name => api-key) + @api_key = {} + # api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix) + @api_key_prefix = {} -# whether to verify SSL certificate, default to true -# Note: do NOT set it to false in production code, otherwise you would -# face multiple types of cryptographic attacks -@verify_ssl = true -end -end -end + # whether to verify SSL certificate, default to true + # Note: do NOT set it to false in production code, otherwise you would + # face multiple types of cryptographic attacks + @verify_ssl = true + end + end + end end diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache index 2c7d985f134..99b58d58be8 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/request.mustache @@ -1,272 +1,272 @@ module {{moduleName}} -module Swagger -class Request -require 'uri' -require 'addressable/uri' -require 'typhoeus' + module Swagger + class Request + require 'uri' + require 'addressable/uri' + require 'typhoeus' -attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names + attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names -# All requests must have an HTTP method and a path -# Optionals parameters are :params, :headers, :body, :format, :host -def initialize(http_method, path, attributes={}) -attributes[:format] ||= Swagger.configuration.format -attributes[:params] ||= {} + # All requests must have an HTTP method and a path + # Optionals parameters are :params, :headers, :body, :format, :host + def initialize(http_method, path, attributes={}) + attributes[:format] ||= Swagger.configuration.format + attributes[:params] ||= {} -# Set default headers -default_headers = { -'Content-Type' => "application/#{attributes[:format].downcase}", -'User-Agent' => Swagger.configuration.user_agent -} + # Set default headers + default_headers = { + 'Content-Type' => "application/#{attributes[:format].downcase}", + 'User-Agent' => Swagger.configuration.user_agent + } -# Merge argument headers into defaults -attributes[:headers] = default_headers.merge(attributes[:headers] || {}) + # Merge argument headers into defaults + attributes[:headers] = default_headers.merge(attributes[:headers] || {}) -# Stick in the auth token if there is one -if Swagger.authenticated? -attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token}) -end + # Stick in the auth token if there is one + if Swagger.authenticated? + attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token}) + end -self.http_method = http_method.to_sym -self.path = path -attributes.each do |name, value| -send("#{name.to_s.underscore.to_sym}=", value) -end + self.http_method = http_method.to_sym + self.path = path + attributes.each do |name, value| + send("#{name.to_s.underscore.to_sym}=", value) + end -update_params_for_auth! -end + update_params_for_auth! + end -# Update hearder and query params based on authentication settings. -def update_params_for_auth! -(@auth_names || []).each do |auth_name| -case auth_name -{{#authMethods}}when '{{name}}' -{{#isApiKey}}{{#isKeyInHeader}}@headers ||= {} -@headers['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}@params ||= {} -@params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}@headers ||= {} -http_auth_header = 'Basic ' + ["#{Swagger.configuration.username}:#{Swagger.configuration.password}"].pack('m').delete("\r\n") -@headers['Authorization'] = http_auth_header{{/isBasic}}{{#isOAuth}}# TODO: support oauth{{/isOAuth}} -{{/authMethods}} -end -end -end + # Update hearder and query params based on authentication settings. + def update_params_for_auth! + (@auth_names || []).each do |auth_name| + case auth_name + {{#authMethods}}when '{{name}}' + {{#isApiKey}}{{#isKeyInHeader}}@headers ||= {} + @headers['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInHeader}}{{#isKeyInQuery}}@params ||= {} + @params['{{keyParamName}}'] = get_api_key_with_prefix('{{keyParamName}}'){{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}@headers ||= {} + http_auth_header = 'Basic ' + ["#{Swagger.configuration.username}:#{Swagger.configuration.password}"].pack('m').delete("\r\n") + @headers['Authorization'] = http_auth_header{{/isBasic}}{{#isOAuth}}# TODO: support oauth{{/isOAuth}} + {{/authMethods}} + end + end + end -# Get API key (with prefix if set). -# @param [String] param_name the parameter name of API key auth -def get_api_key_with_prefix(param_name) -if Swagger.configuration.api_key_prefix[param_name].present? -"#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}" -else -Swagger.configuration.api_key[param_name] -end -end + # Get API key (with prefix if set). + # @param [String] param_name the parameter name of API key auth + def get_api_key_with_prefix(param_name) + if Swagger.configuration.api_key_prefix[param_name].present? + "#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}" + else + Swagger.configuration.api_key[param_name] + end + end -# Construct a base URL -def url(options = {}) -u = Addressable::URI.new( -:scheme => Swagger.configuration.scheme, -:host => Swagger.configuration.host, -:path => self.interpreted_path, -:query => self.query_string.sub(/\?/, '') -).to_s + # Construct a base URL + def url(options = {}) + u = Addressable::URI.new( + :scheme => Swagger.configuration.scheme, + :host => Swagger.configuration.host, + :path => self.interpreted_path, + :query => self.query_string.sub(/\?/, '') + ).to_s -# Drop trailing question mark, if present -u.sub! /\?$/, '' + # Drop trailing question mark, if present + u.sub! /\?$/, '' -u -end + u + end -# Iterate over the params hash, injecting any path values into the path string -# e.g. /word.{format}/{word}/entries => /word.json/cat/entries -def interpreted_path -p = self.path.dup + # Iterate over the params hash, injecting any path values into the path string + # e.g. /word.{format}/{word}/entries => /word.json/cat/entries + def interpreted_path + p = self.path.dup -# Stick a .{format} placeholder into the path if there isn't -# one already or an actual format like json or xml -# e.g. /words/blah => /words.{format}/blah -if Swagger.configuration.inject_format -unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s } -p = p.sub(/^(\/?\w+)/, "\\1.#{format}") -end -end + # Stick a .{format} placeholder into the path if there isn't + # one already or an actual format like json or xml + # e.g. /words/blah => /words.{format}/blah + if Swagger.configuration.inject_format + unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s } + p = p.sub(/^(\/?\w+)/, "\\1.#{format}") + end + end -# Stick a .{format} placeholder on the end of the path if there isn't -# one already or an actual format like json or xml -# e.g. /words/blah => /words/blah.{format} -if Swagger.configuration.force_ending_format -unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s } -p = "#{p}.#{format}" -end -end + # Stick a .{format} placeholder on the end of the path if there isn't + # one already or an actual format like json or xml + # e.g. /words/blah => /words/blah.{format} + if Swagger.configuration.force_ending_format + unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s } + p = "#{p}.#{format}" + end + end -p = p.sub("{format}", self.format.to_s) + p = p.sub("{format}", self.format.to_s) -URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/') -end + URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/') + end -# Massage the request body into a state of readiness -# If body is a hash, camelize all keys then convert to a json string -def body=(value) -if value.is_a?(Hash) -value = value.inject({}) do |memo, (k,v)| -memo[k.to_s.camelize(:lower).to_sym] = v -memo -end -end -@body = value -end + # Massage the request body into a state of readiness + # If body is a hash, camelize all keys then convert to a json string + def body=(value) + if value.is_a?(Hash) + value = value.inject({}) do |memo, (k,v)| + memo[k.to_s.camelize(:lower).to_sym] = v + memo + end + end + @body = value + end -# If body is an object, JSONify it before making the actual request. -# For form parameters, remove empty value -def outgoing_body -# http form -if headers['Content-Type'] == 'application/x-www-form-urlencoded' -data = form_params.dup -data.each do |key, value| -data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter -end -data -elsif @body # http body is JSON -@body.is_a?(String) ? @body : @body.to_json -else -nil -end -end + # If body is an object, JSONify it before making the actual request. + # For form parameters, remove empty value + def outgoing_body + # http form + if headers['Content-Type'] == 'application/x-www-form-urlencoded' + data = form_params.dup + data.each do |key, value| + data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter + end + data + elsif @body # http body is JSON + @body.is_a?(String) ? @body : @body.to_json + else + nil + end + end -# Construct a query string from the query-string-type params -def query_string -# Iterate over all params, -# .. removing the ones that are part of the path itself. -# .. stringifying values so Addressable doesn't blow up. -query_values = {} -self.params.each_pair do |key, value| -next if self.path.include? "{#{key}}" # skip path params -next if value.blank? && value.class != FalseClass # skip empties -if Swagger.configuration.camelize_params -key = key.to_s.camelize(:lower).to_sym -end -query_values[key] = value.to_s -end + # Construct a query string from the query-string-type params + def query_string + # Iterate over all params, + # .. removing the ones that are part of the path itself. + # .. stringifying values so Addressable doesn't blow up. + query_values = {} + self.params.each_pair do |key, value| + next if self.path.include? "{#{key}}" # skip path params + next if value.blank? && value.class != FalseClass # skip empties + if Swagger.configuration.camelize_params + key = key.to_s.camelize(:lower).to_sym + end + query_values[key] = value.to_s + end -# We don't want to end up with '?' as our query string -# if there aren't really any params -return "" if query_values.blank? + # We don't want to end up with '?' as our query string + # if there aren't really any params + return "" if query_values.blank? -# Addressable requires query_values to be set after initialization.. -qs = Addressable::URI.new -qs.query_values = query_values -qs.to_s -end + # Addressable requires query_values to be set after initialization.. + qs = Addressable::URI.new + qs.query_values = query_values + qs.to_s + end -def make -#TODO use configuration setting to determine if debugging -#logger = Logger.new STDOUT -#logger.debug self.url + def make + #TODO use configuration setting to determine if debugging + #logger = Logger.new STDOUT + #logger.debug self.url -request_options = { -:ssl_verifypeer => Swagger.configuration.verify_ssl, -:headers => self.headers.stringify_keys -} -response = case self.http_method.to_sym -when :get,:GET -Typhoeus::Request.get( -self.url, -request_options -) + request_options = { + :ssl_verifypeer => Swagger.configuration.verify_ssl, + :headers => self.headers.stringify_keys + } + response = case self.http_method.to_sym + when :get,:GET + Typhoeus::Request.get( + self.url, + request_options + ) -when :post,:POST -Typhoeus::Request.post( -self.url, -request_options.merge(:body => self.outgoing_body) -) + when :post,:POST + Typhoeus::Request.post( + self.url, + request_options.merge(:body => self.outgoing_body) + ) -when :patch,:PATCH -Typhoeus::Request.patch( -self.url, -request_options.merge(:body => self.outgoing_body) -) + when :patch,:PATCH + Typhoeus::Request.patch( + self.url, + request_options.merge(:body => self.outgoing_body) + ) -when :put,:PUT -Typhoeus::Request.put( -self.url, -request_options.merge(:body => self.outgoing_body) -) + when :put,:PUT + Typhoeus::Request.put( + self.url, + request_options.merge(:body => self.outgoing_body) + ) -when :delete,:DELETE -Typhoeus::Request.delete( -self.url, -request_options.merge(:body => self.outgoing_body) -) -end -Response.new(response) -end + when :delete,:DELETE + Typhoeus::Request.delete( + self.url, + request_options.merge(:body => self.outgoing_body) + ) + end + Response.new(response) + end -def response -self.make -end + def response + self.make + end -def response_code_pretty -return unless @response.present? -@response.code.to_s -end + def response_code_pretty + return unless @response.present? + @response.code.to_s + end -def response_headers_pretty -return unless @response.present? -# JSON.pretty_generate(@response.headers).gsub(/\n/, '
') # <- This was for RestClient -@response.headers.gsub(/\n/, '
') # <- This is for Typhoeus -end + def response_headers_pretty + return unless @response.present? + # JSON.pretty_generate(@response.headers).gsub(/\n/, '
') # <- This was for RestClient + @response.headers.gsub(/\n/, '
') # <- This is for Typhoeus + end -# return 'Accept' based on an array of accept provided -# @param [Array] header_accept_array Array fo 'Accept' -# @return String Accept (e.g. application/json) -def self.select_header_accept header_accept_array -if header_accept_array.empty? -return -elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 } -'application/json' # look for json data by default -else -header_accept_array.join(',') -end -end + # return 'Accept' based on an array of accept provided + # @param [Array] header_accept_array Array fo 'Accept' + # @return String Accept (e.g. application/json) + def self.select_header_accept header_accept_array + if header_accept_array.empty? + return + elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 } + 'application/json' # look for json data by default + else + header_accept_array.join(',') + end + end -# return the content type based on an array of content-type provided -# @param [Array] content_type_array Array fo content-type -# @return String Content-Type (e.g. application/json) -def self.select_header_content_type content_type_array -if content_type_array.empty? -'application/json' # use application/json by default -elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 } -'application/json' # use application/json if it's included -else -content_type_array[0]; # otherwise, use the first one -end -end + # return the content type based on an array of content-type provided + # @param [Array] content_type_array Array fo content-type + # @return String Content-Type (e.g. application/json) + def self.select_header_content_type content_type_array + if content_type_array.empty? + 'application/json' # use application/json by default + elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 } + 'application/json' # use application/json if it's included + else + content_type_array[0]; # otherwise, use the first one + end + end -# static method to convert object (array, hash, object, etc) to JSON string -# @param model object to be converted into JSON string -# @return string JSON string representation of the object -def self.object_to_http_body model -return if model.nil? -_body = nil -if model.is_a?(Array) -_body = model.map{|m| object_to_hash(m) } -else -_body = object_to_hash(model) -end -_body.to_json -end + # static method to convert object (array, hash, object, etc) to JSON string + # @param model object to be converted into JSON string + # @return string JSON string representation of the object + def self.object_to_http_body model + return if model.nil? + _body = nil + if model.is_a?(Array) + _body = model.map{|m| object_to_hash(m) } + else + _body = object_to_hash(model) + end + _body.to_json + end -# static method to convert object(non-array) to hash -# @param obj object to be converted into JSON string -# @return string JSON string representation of the object -def self.object_to_hash obj -if obj.respond_to?(:to_hash) -obj.to_hash -else -obj -end -end + # static method to convert object(non-array) to hash + # @param obj object to be converted into JSON string + # @return string JSON string representation of the object + def self.object_to_hash obj + if obj.respond_to?(:to_hash) + obj.to_hash + else + obj + end + end -end -end + end + end end diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache index 7f9ad4f3281..e7bb482fb66 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache @@ -1,70 +1,70 @@ module {{moduleName}} -module Swagger -class Response -require 'json' + module Swagger + class Response + require 'json' -attr_accessor :raw + attr_accessor :raw -def initialize(raw) -self.raw = raw + def initialize(raw) + self.raw = raw -case self.code -when 500..510 then raise(ServerError, self.error_message) -when 299..426 then raise(ClientError, self.error_message) -end -end + case self.code + when 500..510 then raise(ServerError, self.error_message) + when 299..426 then raise(ClientError, self.error_message) + end + end -def code -raw.code -end + def code + raw.code + end -# Account for error messages that take different forms... -def error_message -body['message'] -rescue -body -end + # Account for error messages that take different forms... + def error_message + body['message'] + rescue + body + end -# If body is JSON, parse it -# Otherwise return raw string -def body -JSON.parse(raw.body, :symbolize_names => true) -rescue -raw.body -end + # If body is JSON, parse it + # Otherwise return raw string + def body + JSON.parse(raw.body, :symbolize_names => true) + rescue + raw.body + end -# `headers_hash` is a Typhoeus-specific extension of Hash, -# so simplify it back into a regular old Hash. -def headers -h = {} -raw.headers_hash.each {|k,v| h[k] = v } -h -end + # `headers_hash` is a Typhoeus-specific extension of Hash, + # so simplify it back into a regular old Hash. + def headers + h = {} + raw.headers_hash.each {|k,v| h[k] = v } + h + end -# Extract the response format from the header hash -# e.g. {'Content-Type' => 'application/json'} -def format -headers['Content-Type'].split("/").last.downcase -end + # Extract the response format from the header hash + # e.g. {'Content-Type' => 'application/json'} + def format + headers['Content-Type'].split("/").last.downcase + end -def json? -format == 'json' -end + def json? + format == 'json' + end -def xml? -format == 'xml' -end + def xml? + format == 'xml' + end -def pretty_body -return unless body.present? -case format -when 'json' then JSON.pretty_generate(body).gsub(/\n/, '
') -end -end + def pretty_body + return unless body.present? + case format + when 'json' then JSON.pretty_generate(body).gsub(/\n/, '
') + end + end -def pretty_headers -JSON.pretty_generate(headers).gsub(/\n/, '
') -end -end -end + def pretty_headers + JSON.pretty_generate(headers).gsub(/\n/, '
') + end + end + end end diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/version.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/version.mustache index a3c42972c3d..332a5e66f45 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/version.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/version.mustache @@ -1,5 +1,5 @@ module {{moduleName}} -module Swagger -VERSION = "{{appVersion}}" -end + module Swagger + VERSION = "{{appVersion}}" + end end