Skip to content

Commit

Permalink
default getter for base url uses openapi document's scheme, host, bas…
Browse files Browse the repository at this point in the history
…ePath. also, doc.
  • Loading branch information
notEthan committed Apr 19, 2018
1 parent 7b1deb9 commit a202dd2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/scorpio/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,28 @@ def define_inheritable_accessor(accessor, options = {})
define_inheritable_accessor(:schemas_by_path)
define_inheritable_accessor(:schemas_by_id, default_value: {})
define_inheritable_accessor(:models_by_schema, default_value: {})
define_inheritable_accessor(:base_url)
# the base url to which paths are appended.
# by default this looks at the openapi document's schemes, picking https or http first.
# it looks at the openapi_document's host and basePath.
# a model overriding this MUST include the openapi document's basePath if defined, e.g.
# class MyModel
# self.base_url = File.join('https://example.com/', openapi_document.basePath)
# end
define_inheritable_accessor(:base_url, default_getter: -> {
if openapi_document.schemes.nil?
scheme = 'https'
elsif openapi_document.schemes.respond_to?(:to_ary)
# prefer https, then http, then anything else since we probably don't support.
scheme = openapi_document.schemes.sort_by { |s| ['https', 'http'].index(s) || (1.0 / 0) }.first
end
if openapi_document.host && scheme
Addressable::URI.new(
scheme: scheme,
host: openapi_document.host,
path: openapi_document.basePath,
).to_s
end
})

define_inheritable_accessor(:faraday_request_middleware, default_value: [])
define_inheritable_accessor(:faraday_adapter, default_getter: proc { Faraday.default_adapter })
Expand Down

0 comments on commit a202dd2

Please sign in to comment.