Sha256: 1865e5a55de03c4e85a95d265a27aa0f29bfb9382c0f45b8d5e8e54bb50784d9

Contents?: true

Size: 1.91 KB

Versions: 11

Compression:

Stored size: 1.91 KB

Contents

module HTTParty
  class Parser
    def json
      Bugsnag::Helpers.load_json(body)
    end
  end
end

module Bugsnag
  module Helpers
    MAX_STRING_LENGTH = 4096

    def self.cleanup_obj(obj, filters = nil)
      return nil unless obj

      if obj.is_a?(Hash)
        clean_hash = {}
        obj.each do |k,v| 
          if filters && filters.any? {|f| k.to_s.include?(f.to_s)}
            clean_hash[k] = "[FILTERED]"
          else
            clean_obj = cleanup_obj(v, filters)
            clean_hash[k] = clean_obj unless clean_obj.nil?
          end
        end
        clean_hash
      elsif obj.is_a?(Array) || obj.is_a?(Set)
        obj.map { |el| cleanup_obj(el, filters) }.compact
      elsif obj.is_a?(Integer) || obj.is_a?(Float) || obj.is_a?(String)
        obj
      else
        obj.to_s unless obj.to_s =~ /#<.*>/
      end
    end
    
    def self.reduce_hash_size(hash)
      return {} unless hash.is_a?(Hash)
      hash.inject({}) do |h, (k,v)|
        if v.is_a?(Hash)
          h[k] = reduce_hash_size(v)
        elsif v.is_a?(Array) || v.is_a?(Set)
          h[k] = v.map {|el| reduce_hash_size(el) }
        else
          h[k] = v.to_s.slice(0, MAX_STRING_LENGTH) + "[TRUNCATED]"
        end

        h
      end
    end
    
    def self.flatten_meta_data(overrides)
      return nil unless overrides

      meta_data = overrides.delete(:meta_data)
      if meta_data.is_a?(Hash)
        overrides.merge(meta_data) 
      else
        overrides
      end
    end

    # Helper functions to work around MultiJson changes in 1.3+
    def self.dump_json(object, options={})
      if MultiJson.respond_to?(:adapter)
        MultiJson.dump(object, options)
      else
        MultiJson.encode(object, options)
      end
    end

    def self.load_json(json, options={})
      if MultiJson.respond_to?(:adapter)
        MultiJson.load(json, options)
      else
        MultiJson.decode(json, options)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bugsnag-1.3.6 lib/bugsnag/helpers.rb
bugsnag-1.3.5 lib/bugsnag/helpers.rb
bugsnag-1.3.4 lib/bugsnag/helpers.rb
bugsnag-1.3.3 lib/bugsnag/helpers.rb
bugsnag-1.3.2 lib/bugsnag/helpers.rb
bugsnag-1.3.1 lib/bugsnag/helpers.rb
bugsnag-1.3.0 lib/bugsnag/helpers.rb
bugsnag-1.2.18 lib/bugsnag/helpers.rb
bugsnag-1.2.17 lib/bugsnag/helpers.rb
bugsnag-1.2.16 lib/bugsnag/helpers.rb
bugsnag-1.2.15 lib/bugsnag/helpers.rb