Module: EasyPost::InternalUtilities::Json
- Defined in:
 - lib/easypost/utilities/json.rb
 
Class Method Summary collapse
- .convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject) ⇒ Object
 - .http_response_is_json?(response) ⇒ Boolean
 - .parse_json(data) ⇒ Object
 
Class Method Details
.convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject) ⇒ Object
      4 5 6 7 8 9 10 11 12 13 14 15 16  | 
    
      # 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) ⇒ Boolean
      26 27 28  | 
    
      # 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) ⇒ Object
      18 19 20 21 22 23 24  | 
    
      # 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  |