Sha256: c65adaab55a82006bcad928c712718fea621553be2de93bb708f9a90aa41e21a

Contents?: true

Size: 922 Bytes

Versions: 6

Compression:

Stored size: 922 Bytes

Contents

# frozen_string_literal: true

module EasyPost::InternalUtilities::Json
  def self.convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject)
    data = JSON.parse(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
  rescue JSON::ParserError
    data # Not JSON, return the original data (used mostly when dealing with final values like strings, booleans, etc.)
  end

  def self.http_response_is_json?(response)
    response['Content-Type'] ? response['Content-Type'].start_with?('application/json') : false
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
easypost-5.3.0 lib/easypost/utilities/json.rb
easypost-5.2.0 lib/easypost/utilities/json.rb
easypost-5.1.1 lib/easypost/utilities/json.rb
easypost-5.1.0 lib/easypost/utilities/json.rb
easypost-5.0.1 lib/easypost/utilities/json.rb
easypost-5.0.0 lib/easypost/utilities/json.rb