Method: MoneyKit::ApiClient#deserialize

Defined in:
lib/moneykit/api_client.rb

#deserialize(response, return_type) ⇒ Object

Deserialize the response to the given return type.

Parameters:

  • response (Response)

    HTTP response

  • return_type (String)

    some examples: “User”, “Array<User>”, “Hash<String, Integer>”



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/moneykit/api_client.rb', line 250

def deserialize(response, return_type)
  body = response.body
  return nil if body.nil? || body.empty?

  # return response body directly for String return type
  return body.to_s if return_type == 'String'

  # ensuring a default content type
  content_type = response.headers['Content-Type'] || 'application/json'

  raise "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)

  begin
    data = JSON.parse("[#{body}]", symbolize_names: true)[0]
  rescue JSON::ParserError => e
    raise e unless %w[String Date Time].include?(return_type)

    data = body
  end

  convert_to_type data, return_type
end