lib/httpx/transcoder/json.rb in httpx-0.16.1 vs lib/httpx/transcoder/json.rb in httpx-0.17.0
- old
+ new
@@ -3,10 +3,14 @@
require "forwardable"
require "json"
module HTTPX::Transcoder
module JSON
+ JSON_REGEX = %r{\bapplication/(?:vnd\.api\+)?json\b}i.freeze
+
+ using HTTPX::RegexpExtensions unless Regexp.method_defined?(:match?)
+
module_function
class Encoder
extend Forwardable
@@ -24,9 +28,17 @@
end
end
def encode(json)
Encoder.new(json)
+ end
+
+ def decode(response)
+ content_type = response.content_type.mime_type
+
+ raise Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
+
+ ::JSON.method(:parse)
end
end
register "json", JSON
end