Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc #42

Merged
merged 5 commits into from
Jun 11, 2018
Merged

Misc #42

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions lib/scorpio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,55 @@ class HTTPError < Error
define_singleton_method(:status) { |status| Scorpio.error_classes_by_status[status] = self }
attr_accessor :faraday_response, :response_object
end
class ClientError < HTTPError; end
class ServerError < HTTPError; end
# HTTP Error classes' canonical names are like Scorpio::HTTPErrors::BadRequest400Error, but can
# be referred to like Scorpio::BadRequest400Error. this is just to avoid clutter in the Scorpio
# namespace in yardoc.
module HTTPErrors
class ClientError < HTTPError; end
class ServerError < HTTPError; end

class BadRequest400Error < ClientError; status(400); end
class Unauthorized401Error < ClientError; status(401); end
class PaymentRequired402Error < ClientError; status(402); end
class Forbidden403Error < ClientError; status(403); end
class NotFound404Error < ClientError; status(404); end
class MethodNotAllowed405Error < ClientError; status(405); end
class NotAcceptable406Error < ClientError; status(406); end
class ProxyAuthenticationRequired407Error < ClientError; status(407); end
class RequestTimeout408Error < ClientError; status(408); end
class Conflict409Error < ClientError; status(409); end
class Gone410Error < ClientError; status(410); end
class LengthRequired411Error < ClientError; status(411); end
class PreconditionFailed412Error < ClientError; status(412); end
class PayloadTooLarge413Error < ClientError; status(413); end
class URITooLong414Error < ClientError; status(414); end
class UnsupportedMediaType415Error < ClientError; status(415); end
class RangeNotSatisfiable416Error < ClientError; status(416); end
class ExpectationFailed417Error < ClientError; status(417); end
class ImaTeapot418Error < ClientError; status(418); end
class MisdirectedRequest421Error < ClientError; status(421); end
class UnprocessableEntity422Error < ClientError; status(422); end
class Locked423Error < ClientError; status(423); end
class FailedDependency424Error < ClientError; status(424); end
class UpgradeRequired426Error < ClientError; status(426); end
class PreconditionRequired428Error < ClientError; status(428); end
class TooManyRequests429Error < ClientError; status(429); end
class RequestHeaderFieldsTooLarge431Error < ClientError; status(431); end
class UnavailableForLegalReasons451Error < ClientError; status(451); end
class BadRequest400Error < ClientError; status(400); end
class Unauthorized401Error < ClientError; status(401); end
class PaymentRequired402Error < ClientError; status(402); end
class Forbidden403Error < ClientError; status(403); end
class NotFound404Error < ClientError; status(404); end
class MethodNotAllowed405Error < ClientError; status(405); end
class NotAcceptable406Error < ClientError; status(406); end
class ProxyAuthenticationRequired407Error < ClientError; status(407); end
class RequestTimeout408Error < ClientError; status(408); end
class Conflict409Error < ClientError; status(409); end
class Gone410Error < ClientError; status(410); end
class LengthRequired411Error < ClientError; status(411); end
class PreconditionFailed412Error < ClientError; status(412); end
class PayloadTooLarge413Error < ClientError; status(413); end
class URITooLong414Error < ClientError; status(414); end
class UnsupportedMediaType415Error < ClientError; status(415); end
class RangeNotSatisfiable416Error < ClientError; status(416); end
class ExpectationFailed417Error < ClientError; status(417); end
class ImaTeapot418Error < ClientError; status(418); end
class MisdirectedRequest421Error < ClientError; status(421); end
class UnprocessableEntity422Error < ClientError; status(422); end
class Locked423Error < ClientError; status(423); end
class FailedDependency424Error < ClientError; status(424); end
class UpgradeRequired426Error < ClientError; status(426); end
class PreconditionRequired428Error < ClientError; status(428); end
class TooManyRequests429Error < ClientError; status(429); end
class RequestHeaderFieldsTooLarge431Error < ClientError; status(431); end
class UnavailableForLegalReasons451Error < ClientError; status(451); end

class InternalServerError500Error < ServerError; status(500); end
class NotImplemented501Error < ServerError; status(501); end
class BadGateway502Error < ServerError; status(502); end
class ServiceUnavailable503Error < ServerError; status(503); end
class GatewayTimeout504Error < ServerError; status(504); end
class HTTPVersionNotSupported505Error < ServerError; status(505); end
class VariantAlsoNegotiates506Error < ServerError; status(506); end
class InsufficientStorage507Error < ServerError; status(507); end
class LoopDetected508Error < ServerError; status(508); end
class NotExtended510Error < ServerError; status(510); end
class NetworkAuthenticationRequired511Error < ServerError; status(511); end
class InternalServerError500Error < ServerError; status(500); end
class NotImplemented501Error < ServerError; status(501); end
class BadGateway502Error < ServerError; status(502); end
class ServiceUnavailable503Error < ServerError; status(503); end
class GatewayTimeout504Error < ServerError; status(504); end
class HTTPVersionNotSupported505Error < ServerError; status(505); end
class VariantAlsoNegotiates506Error < ServerError; status(506); end
class InsufficientStorage507Error < ServerError; status(507); end
class LoopDetected508Error < ServerError; status(508); end
class NotExtended510Error < ServerError; status(510); end
class NetworkAuthenticationRequired511Error < ServerError; status(511); end
end
include HTTPErrors
error_classes_by_status.freeze

autoload :ResourceBase, 'scorpio/resource_base'
Expand Down
30 changes: 14 additions & 16 deletions lib/scorpio/resource_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@ def define_inheritable_accessor(accessor, options = {})
klass.instance_exec(&options[:on_set])
end
end
if options[:update_methods]
update_dynamic_methods
end
end
end
end
# the class on which the openapi document is defined. subclasses use the openapi document set on this class
# (except in the unlikely event it is overwritten by a subclass)
define_inheritable_accessor(:openapi_document_class)
# the openapi document
define_inheritable_accessor(:tag_name, update_methods: true)
define_inheritable_accessor(:represented_schemas, default_value: [], update_methods: true, on_set: proc do
define_inheritable_accessor(:tag_name, on_set: -> { update_dynamic_methods })
define_inheritable_accessor(:represented_schemas, default_value: [], on_set: proc do
unless represented_schemas.respond_to?(:to_ary)
raise(TypeError, "represented_schemas must be an array. received: #{represented_schemas.pretty_inspect.chomp}")
end
if represented_schemas.all? { |s| s.is_a?(Scorpio::Schema) }
represented_schemas.each do |schema|
openapi_document_class.models_by_schema = openapi_document_class.models_by_schema.merge(schema => self)
end
update_dynamic_methods
else
self.represented_schemas = self.represented_schemas.map do |schema|
unless schema.is_a?(Scorpio::Schema)
Expand Down Expand Up @@ -92,6 +90,9 @@ def define_inheritable_accessor(accessor, options = {})
end
})

define_inheritable_accessor(:user_agent, default_getter: -> {
"Scorpio/#{Scorpio::VERSION} (https://github.com/notEthan/scorpio) Faraday/#{Faraday::VERSION} Ruby/#{RUBY_VERSION}"
})
define_inheritable_accessor(:faraday_request_middleware, default_value: [])
define_inheritable_accessor(:faraday_adapter, default_getter: proc { Faraday.default_adapter })
define_inheritable_accessor(:faraday_response_middleware, default_value: [])
Expand All @@ -112,6 +113,13 @@ def openapi_document=(openapi_document)
rescue NameError
end
define_singleton_method(:openapi_document) { openapi_document }
define_singleton_method(:openapi_document=) do
if self == openapi_document_class
raise(ArgumentError, "openapi_document may only be set once on #{self.inspect}")
else
raise(ArgumentError, "openapi_document may not be overridden on subclass #{self.inspect} after it was set on #{openapi_document_class.inspect}")
end
end
update_dynamic_methods

openapi_document.paths.each do |path, path_item|
Expand Down Expand Up @@ -229,18 +237,8 @@ def update_class_and_instance_api_methods
end
end

MODULES_FOR_JSON_SCHEMA_TYPES = {
'object' => [Hash],
'array' => [Array, Set],
'string' => [String],
'integer' => [Integer],
'number' => [Numeric],
'boolean' => [TrueClass, FalseClass],
'null' => [NilClass],
}

def connection
Faraday.new do |c|
Faraday.new(:headers => {'User-Agent' => user_agent}) do |c|
faraday_request_middleware.each do |m|
c.request(*m)
end
Expand Down