Skip to content

Commit

Permalink
[ur v0.1.0] use Ur::ContentType in Scorpio Request and Response. Ur::…
Browse files Browse the repository at this point in the history
…ContentTypeAttrs is removed; remove #content_type_attrs
  • Loading branch information
notEthan committed Dec 26, 2019
1 parent 6e3f77c commit 03fb0ac
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
22 changes: 8 additions & 14 deletions lib/scorpio/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ def base_url
def body
return @body if instance_variable_defined?(:@body)
if instance_variable_defined?(:@body_object)
# TODO handle media types like `application/schema-instance+json`
if media_type == 'application/json'
if content_type && content_type.json?
JSON.pretty_generate(JSI::Typelike.as_json(body_object))
elsif media_type == "application/x-www-form-urlencoded"
elsif content_type && content_type.form_urlencoded?
URI.encode_www_form(body_object)

# NOTE: the supported media types above should correspond to Request::SUPPORTED_REQUEST_MEDIA_TYPES
Expand All @@ -62,7 +61,7 @@ def body
if body_object.respond_to?(:to_str)
body_object
else
raise(NotImplementedError, "Scorpio does not know how to generate the request body with media_type = #{media_type.respond_to?(:to_str) ? media_type : media_type.inspect} for operation: #{operation.human_id}. Scorpio supports media types: #{SUPPORTED_REQUEST_MEDIA_TYPES.join(', ')}. body_object was: #{body_object.pretty_inspect.chomp}")
raise(NotImplementedError, "Scorpio does not know how to generate the request body with content_type = #{content_type.respond_to?(:to_str) ? content_type : content_type.inspect} for operation: #{operation.human_id}. Scorpio supports media types: #{SUPPORTED_REQUEST_MEDIA_TYPES.join(', ')}. body_object was: #{body_object.pretty_inspect.chomp}")
end
end
else
Expand All @@ -81,7 +80,7 @@ def headers
attr_writer :media_type
def media_type
return @media_type if instance_variable_defined?(:@media_type)
content_type_header ? content_type_attrs.media_type : operation.request_media_type
content_type_header ? content_type_header.media_type : operation.request_media_type
end

attr_writer :user_agent
Expand Down Expand Up @@ -190,23 +189,18 @@ def url
Addressable::URI.parse(File.join(base_url, path))
end

# @return [::Ur::ContentTypeAttrs] content type attributes for this request's Content-Type
def content_type_attrs
Ur::ContentTypeAttrs.new(content_type)
end

# @return [String] the value of the request Content-Type header
# @return [::Ur::ContentType] the value of the request Content-Type header
def content_type_header
headers.each do |k, v|
return v if k =~ /\Acontent[-_]type\z/i
return ::Ur::ContentType.new(v) if k =~ /\Acontent[-_]type\z/i
end
nil
end

# @return [String] Content-Type for this request, taken from request headers if
# @return [::Ur::ContentType] Content-Type for this request, taken from request headers if
# present, or the request media_type.
def content_type
content_type_header || media_type
content_type_header || ::Ur::ContentType.new(media_type)
end

# @return [::JSI::Schema]
Expand Down
5 changes: 2 additions & 3 deletions lib/scorpio/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def response_schema
# if supported (only application/json is currently supported) instantiated according to
# #response_schema
def body_object
# TODO handle media types like `application/schema-instance+json` or vendor things like github's
if media_type == 'application/json'
if json?
if body.empty?
# an empty body isn't valid json, of course, but we'll just return nil for it.
body_object = nil
Expand All @@ -27,7 +26,7 @@ def body_object
end

body_object
elsif media_type == 'text/plain'
elsif content_type && content_type.type_text? && content_type.subtype?('plain')
body
else
# we will return the body if we do not have a supported parsing. for now.
Expand Down
2 changes: 1 addition & 1 deletion scorpio.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "jsi", "~> 0.2.0"
spec.add_dependency "ur", "~> 0.0.4"
spec.add_dependency "ur", "~> 0.1.0"
spec.add_dependency "faraday"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
Expand Down

0 comments on commit 03fb0ac

Please sign in to comment.