Sha256: 35c25977c4eaecf5f72373989dac21bfbeef123843d47a8281db30bc82b787ba

Contents?: true

Size: 1021 Bytes

Versions: 3

Compression:

Stored size: 1021 Bytes

Contents

module OMCMS
  module Response
    class Body
      attr_reader :body

      def initialize(client, response, host)
        @client = client
        @body = response.body
        @host = host
      end

      def to_json
        parse(@body)
      end

      private

      def parse(data = "")
        if data.class == Hash
          return data.keys.map do |attribute|
            key = snakecase(attribute)
            [key, parse(data[attribute])]
          end.to_h
        elsif data.class == Array
          return data.map do |object|
            hash = object.keys.map do |attribute|
              key = snakecase(attribute)
              [key, parse(object[attribute])]
            end.to_h
          end
        end
        return 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
omcms-ruby-client-1.2.0 lib/omcms/response/body.rb
omcms-ruby-client-1.1.0 lib/omcms/response/body.rb
omcms-ruby-client-1.0.3 lib/omcms/response/body.rb