# frozen_string_literal: true module OMCMS module Response class Body attr_reader :body def initialize(client, host, response) @client = client @body = response.body @host = host end def to_json(*_args) parse(@body) end private def parse(data = "") if data.instance_of?(Hash) return data.keys.to_h do |attribute| key = snakecase(attribute) [key, parse(data[attribute])] end elsif data.instance_of?(Array) return data.map do |object| object.keys.to_h do |attribute| key = snakecase(attribute) [key, parse(object[attribute])] end end end data end def snakecase(data = "") data.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .gsub(/\s/, "_") .gsub(/__+/, "_") .downcase end end end end