Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/misc' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Apr 21, 2019
2 parents ac41254 + e702eab commit ba26618
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
12 changes: 10 additions & 2 deletions lib/scorpio/openapi/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def path
end
end

# @return [Addressable::Template] the path as an Addressable::Template
def path_template
return @path_template if instance_variable_defined?(:@path_template)
@path_template = Addressable::Template.new(path)
end

def http_method
return @http_method if instance_variable_defined?(:@http_method)
@http_method = begin
Expand Down Expand Up @@ -196,18 +202,20 @@ def body_parameter
elsif body_parameters.size == 1
body_parameters.first
else
raise(Bug) # TODO BLAME
raise(Bug, "multiple body parameters on operation #{operation.pretty_inspect.chomp}") # TODO BLAME
end
end

def request_schema(media_type: nil)
if body_parameter && body_parameter['schema']
JSI::Schema.new(body_parameter['schema'])
else
nil
end
end

def request_schemas
[request_schema]
request_schema ? [request_schema] : []
end

# @return JSI::Schema
Expand Down
16 changes: 9 additions & 7 deletions lib/scorpio/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,17 @@ def logger
# the request - instance methods of Scorpio::Request::Configurables, whose values
# will be assigned for those attributes.
def initialize(operation, **configuration, &b)
configuration.each do |k, v|
settername = "#{k}="
if Configurables.public_method_defined?(settername)
Configurables.instance_method(settername).bind(self).call(v)
@operation = operation

configuration = JSI.stringify_symbol_keys(configuration)
configuration.each do |name, value|
if Configurables.public_method_defined?("#{name}=")
Configurables.instance_method("#{name}=").bind(self).call(value)
else
raise(ArgumentError, "unsupported configuration value passed: #{k.inspect} => #{v.inspect}")
raise(ArgumentError, "unrecognized configuration value passed: #{name.inspect}")
end
end

@operation = operation
if block_given?
yield self
end
Expand All @@ -146,12 +147,13 @@ def http_method
# @return [Addressable::Template] the template for the request's path, to be expanded
# with path_params and appended to the request's base_url
def path_template
Addressable::Template.new(operation.path)
operation.path_template
end

# @return [Addressable::URI] an Addressable::URI containing only the path to append to
# the base_url for this request
def path
path_params = JSI.stringify_symbol_keys(self.path_params)
missing_variables = path_template.variables - path_params.keys
if missing_variables.any?
raise(ArgumentError, "path #{operation.path} for operation #{operation.operationId} requires path_params " +
Expand Down
25 changes: 14 additions & 11 deletions lib/scorpio/resource_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ def openapi_document=(openapi_document)
end
update_dynamic_methods

openapi_document.paths.each do |path, path_item|
path_item.each do |http_method, operation|
unless operation.is_a?(Scorpio::OpenAPI::Operation)
next
end
end
end

# TODO blame validate openapi_document

update_dynamic_methods
Expand Down Expand Up @@ -221,8 +213,19 @@ def method_names_by_operation
h[operation] = begin
raise(ArgumentError, operation.pretty_inspect) unless operation.is_a?(Scorpio::OpenAPI::Operation)

if operation.tags.respond_to?(:to_ary) && operation.tags.include?(tag_name) && operation.operationId =~ /\A#{Regexp.escape(tag_name)}\.(\w+)\z/
method_name = $1
# if Pet is the Scorpio resource class
# and Pet.tag_name is "pet"
# and operation's operationId is "pet.add"
# then the operation's method name on Pet will be "add".
# if the operationId is just "addPet"
# then the operation's method name on Pet will be "addPet".
tag_name_match = tag_name &&
operation.tags.respond_to?(:to_ary) && # TODO maybe operation.tags.valid?
operation.tags.include?(tag_name) &&
operation.operationId.match(/\A#{Regexp.escape(tag_name)}\.(\w+)\z/)

if tag_name_match
method_name = tag_name_match[1]
else
method_name = operation.operationId
end
Expand All @@ -233,7 +236,7 @@ def method_names_by_operation
def update_class_and_instance_api_methods
openapi_document.paths.each do |path, path_item|
path_item.each do |http_method, operation|
next if http_method == 'parameters' # parameters is not an operation. TOOD maybe just select the keys that are http methods?
next unless operation.is_a?(Scorpio::OpenAPI::Operation)
method_name = method_names_by_operation[operation]
if method_name
# class method
Expand Down

0 comments on commit ba26618

Please sign in to comment.