Class: SwaggerClient::Swagger::Request
- Defined in:
- lib/swagger_client/swagger/request.rb
Instance Attribute Summary collapse
-
#auth_names ⇒ Object
Returns the value of attribute auth_names.
-
#body ⇒ Object
Returns the value of attribute body.
-
#form_params ⇒ Object
Returns the value of attribute form_params.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.object_to_hash(obj) ⇒ Object
static method to convert object(non-array) to hash.
-
.object_to_http_body(model) ⇒ Object
static method to convert object (array, hash, object, etc) to JSON string.
-
.select_header_accept(header_accept_array) ⇒ Object
return ‘Accept’ based on an array of accept provided.
-
.select_header_content_type(content_type_array) ⇒ Object
return the content type based on an array of content-type provided.
Instance Method Summary collapse
-
#get_api_key_with_prefix(param_name) ⇒ Object
Get API key (with prefix if set).
-
#initialize(http_method, path, attributes = {}) ⇒ Request
constructor
All requests must have an HTTP method and a path Optionals parameters are :params, :headers, :body, :format, :host.
-
#interpreted_path ⇒ Object
Iterate over the params hash, injecting any path values into the path string e.g.
- #make ⇒ Object
-
#outgoing_body ⇒ Object
If body is an object, JSONify it before making the actual request.
-
#query_string ⇒ Object
Construct a query string from the query-string-type params.
- #response ⇒ Object
- #response_code_pretty ⇒ Object
- #response_headers_pretty ⇒ Object
-
#update_params_for_auth! ⇒ Object
Update hearder and query params based on authentication settings.
-
#url(options = {}) ⇒ Object
Construct a base URL.
Constructor Details
#initialize(http_method, path, attributes = {}) ⇒ Request
All requests must have an HTTP method and a path Optionals parameters are :params, :headers, :body, :format, :host
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/swagger_client/swagger/request.rb', line 12 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 } # 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 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 |
Instance Attribute Details
#auth_names ⇒ Object
Returns the value of attribute auth_names.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def auth_names @auth_names end |
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def body @body end |
#form_params ⇒ Object
Returns the value of attribute form_params.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def form_params @form_params end |
#format ⇒ Object
Returns the value of attribute format.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def format @format end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def host @host end |
#http_method ⇒ Object
Returns the value of attribute http_method.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def http_method @http_method end |
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/swagger_client/swagger/request.rb', line 8 def path @path end |
Class Method Details
.object_to_hash(obj) ⇒ Object
static method to convert object(non-array) to hash
254 255 256 257 258 259 260 |
# File 'lib/swagger_client/swagger/request.rb', line 254 def self.object_to_hash obj if obj.respond_to?(:to_hash) obj.to_hash else obj end end |
.object_to_http_body(model) ⇒ Object
static method to convert object (array, hash, object, etc) to JSON string
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/swagger_client/swagger/request.rb', line 240 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 |
.select_header_accept(header_accept_array) ⇒ Object
return ‘Accept’ based on an array of accept provided
214 215 216 217 218 219 220 221 222 |
# File 'lib/swagger_client/swagger/request.rb', line 214 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 |
.select_header_content_type(content_type_array) ⇒ Object
return the content type based on an array of content-type provided
227 228 229 230 231 232 233 234 235 |
# File 'lib/swagger_client/swagger/request.rb', line 227 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 |
Instance Method Details
#get_api_key_with_prefix(param_name) ⇒ Object
Get API key (with prefix if set).
48 49 50 51 52 53 54 |
# File 'lib/swagger_client/swagger/request.rb', line 48 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 |
#interpreted_path ⇒ Object
Iterate over the params hash, injecting any path values into the path string e.g. /word.#format/word/entries => /word.json/cat/entries
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/swagger_client/swagger/request.rb', line 73 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 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) URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/') end |
#make ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/swagger_client/swagger/request.rb', line 153 def make #TODO use configuration setting to determine if debugging #logger = Logger.new STDOUT #logger.debug self.url = { :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, ) when :post,:POST Typhoeus::Request.post( self.url, .merge(:body => self.outgoing_body) ) when :patch,:PATCH Typhoeus::Request.patch( self.url, .merge(:body => self.outgoing_body) ) when :put,:PUT Typhoeus::Request.put( self.url, .merge(:body => self.outgoing_body) ) when :delete,:DELETE Typhoeus::Request.delete( self.url, .merge(:body => self.outgoing_body) ) end Response.new(response) end |
#outgoing_body ⇒ Object
If body is an object, JSONify it before making the actual request. For form parameters, remove empty value
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/swagger_client/swagger/request.rb', line 113 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 |
#query_string ⇒ Object
Construct a query string from the query-string-type params
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/swagger_client/swagger/request.rb', line 129 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? # Addressable requires query_values to be set after initialization.. qs = Addressable::URI.new qs.query_values = query_values qs.to_s end |
#response ⇒ Object
196 197 198 |
# File 'lib/swagger_client/swagger/request.rb', line 196 def response self.make end |
#response_code_pretty ⇒ Object
200 201 202 203 |
# File 'lib/swagger_client/swagger/request.rb', line 200 def response_code_pretty return unless @response.present? @response.code.to_s end |
#response_headers_pretty ⇒ Object
205 206 207 208 209 |
# File 'lib/swagger_client/swagger/request.rb', line 205 def response_headers_pretty return unless @response.present? # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus end |
#update_params_for_auth! ⇒ Object
Update hearder and query params based on authentication settings.
40 41 42 43 44 |
# File 'lib/swagger_client/swagger/request.rb', line 40 def update_params_for_auth! (@auth_names || []).each do |auth_name| end end |
#url(options = {}) ⇒ Object
Construct a base URL
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/swagger_client/swagger/request.rb', line 57 def url( = {}) 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! /\?$/, '' u end |