module EasyPost::InternalUtilities::Json

Public Class Methods

convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject) click to toggle source
# File lib/easypost/utilities/json.rb, line 4
def self.convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject)
  data = parse_json(data) if data.is_a?(String) # Parse JSON to a Hash or Array if it's a string
  if data.is_a?(Array)
    # Deserialize array data into an array of objects
    data.map { |i| convert_json_to_object(i, cls) }
  elsif data.is_a?(Hash)
    # Deserialize hash data into a new object instance
    cls.new(data)
  else
    # data is neither a Hash nor Array (but somehow was parsed as JSON? This should never happen)
    data
  end
end
http_response_is_json?(response) click to toggle source
# File lib/easypost/utilities/json.rb, line 26
def self.http_response_is_json?(response)
  response['Content-Type'] ? response['Content-Type'].start_with?('application/json') : false
end
parse_json(data) click to toggle source
# File lib/easypost/utilities/json.rb, line 18
def self.parse_json(data)
  return if data.nil?

  JSON.parse(data)
rescue JSON::ParserError
  data # Not JSON, return the original data (used mostly when dealing with final values like strings, booleans, etc.)
end