Skip to content

Commit e52694c

Browse files
committed
Regenerate Ruby Petstore sample
1 parent 9581371 commit e52694c

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

samples/client/petstore/ruby/lib/swagger_client/swagger.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class << self
1616
#
1717
# @example
1818
# Swagger.configure do |config|
19-
# config.api_key = '1234567890abcdef' # required
20-
# config.username = 'wordlover' # optional, but needed for user-related functions
21-
# config.password = 'i<3words' # optional, but needed for user-related functions
19+
# config.api_key['api_key'] = '1234567890abcdef' # api key authentication
20+
# config.username = 'wordlover' # http basic authentication
21+
# config.password = 'i<3words' # http basic authentication
2222
# config.format = 'json' # optional, defaults to 'json'
2323
# end
2424
#

samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module SwaggerClient
22
module Swagger
33
class Configuration
4-
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
5-
4+
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
5+
66
# Defaults go in here..
77
def initialize
88
@format = 'json'
@@ -13,6 +13,8 @@ def initialize
1313
@inject_format = false
1414
@force_ending_format = false
1515
@camelize_params = true
16+
@api_key = {}
17+
@api_key_prefix = {}
1618
end
1719
end
1820
end

samples/client/petstore/ruby/lib/swagger_client/swagger/request.rb

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,14 @@ def initialize(http_method, path, attributes={})
1313
attributes[:format] ||= Swagger.configuration.format
1414
attributes[:params] ||= {}
1515

16+
update_params_for_auth(attributes)
17+
1618
# Set default headers
1719
default_headers = {
1820
'Content-Type' => "application/#{attributes[:format].downcase}",
19-
:api_key => Swagger.configuration.api_key,
2021
'User-Agent' => Swagger.configuration.user_agent
2122
}
2223

23-
# api_key from headers hash trumps the default, even if its value is blank
24-
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
25-
default_headers.delete(:api_key)
26-
end
27-
28-
# api_key from params hash trumps all others (headers and default_headers)
29-
if attributes[:params].present? && attributes[:params].has_key?(:api_key)
30-
default_headers.delete(:api_key)
31-
attributes[:headers].delete(:api_key) if attributes[:headers].present?
32-
end
33-
3424
# Merge argument headers into defaults
3525
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
3626

@@ -46,6 +36,33 @@ def initialize(http_method, path, attributes={})
4636
end
4737
end
4838

39+
def update_params_for_auth(attributes)
40+
(attributes[:auth_names] || []).each do |auth_name|
41+
case auth_name
42+
43+
when 'api_key'
44+
attributes[:headers] ||= {}
45+
attributes[:headers]['api_key'] = get_api_key_with_prefix('api_key')
46+
47+
48+
49+
when 'petstore_auth'
50+
51+
52+
# TODO: support oauth
53+
54+
end
55+
end
56+
end
57+
58+
def get_api_key_with_prefix(param_name)
59+
if Swagger.configuration.api_key_prefix[param_name].present?
60+
"#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}"
61+
else
62+
Swagger.configuration.api_key[param_name]
63+
end
64+
end
65+
4966
# Construct a base URL
5067
def url(options = {})
5168
u = Addressable::URI.new(
@@ -58,9 +75,6 @@ def url(options = {})
5875
# Drop trailing question mark, if present
5976
u.sub! /\?$/, ''
6077

61-
# Obfuscate API key?
62-
u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
63-
6478
u
6579
end
6680

@@ -129,7 +143,7 @@ def query_string
129143
next if self.path.include? "{#{key}}" # skip path params
130144
next if value.blank? && value.class != FalseClass # skip empties
131145
if Swagger.configuration.camelize_params
132-
key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
146+
key = key.to_s.camelize(:lower).to_sym
133147
end
134148
query_values[key] = value.to_s
135149
end

0 commit comments

Comments
 (0)