Sha256: 66489dea1586fd8b0edb203f161993b14c7de096d12cb6f39a946f86d3c5071d

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

module Github
  module Filter

    def process_params(*args)
      yield self if block_given?
    end

    def normalize(params)
      _normalize_params_keys params
    end

    def filter(keys, params)
      _filter_params_keys(keys, params)
    end

    # Turns any keys from nested hashes including nested arrays into strings
    def _normalize_params_keys(params)  #  :nodoc:
      case params
      when Hash
        params.keys.each do |k|
          params[k.to_s] = params.delete(k)
          _normalize_params_keys(params[k.to_s])
        end
      when Array
        params.map! do |el|
          _normalize_params_keys(el)
        end
      else
        params.to_s
      end
      return params
    end

    # Removes any keys from nested hashes that don't match predefiend keys
    def _filter_params_keys(keys, params, options={:recursive => true})  # :nodoc:
      case params
      when Hash
        params.keys.each do |k, v|
          unless (keys.include?(k) or Github::Validations::VALID_API_KEYS.include?(k))
            params.delete(k)
          else
            _filter_params_keys(keys, params[k]) if options[:recursive]
          end
        end
      when Array
        params.map! do |el|
          _filter_params_keys(keys, el) if options[:recursive]
        end
      else
        params
      end
      return params
    end

  end # Filter
end # Github

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
github_api-0.5.4 lib/github_api/filter.rb
github_api-0.5.3 lib/github_api/filter.rb
github_api-0.5.2 lib/github_api/filter.rb
github_api-0.5.1 lib/github_api/filter.rb
github_api-0.5.0 lib/github_api/filter.rb
github_api-0.5.0.rc1 lib/github_api/filter.rb