Sha256: 78e58e805e74e34a3143e27347f874efb5205f90bc18b3b81ee0b89fb9c794f4

Contents?: true

Size: 1.35 KB

Versions: 8

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::Validation::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

8 entries across 8 versions & 1 rubygems

Version Path
github_api-0.4.11 lib/github_api/filter.rb
github_api-0.4.10 lib/github_api/filter.rb
github_api-0.4.9 lib/github_api/filter.rb
github_api-0.4.8 lib/github_api/filter.rb
github_api-0.4.7 lib/github_api/filter.rb
github_api-0.4.6 lib/github_api/filter.rb
github_api-0.4.5 lib/github_api/filter.rb
github_api-0.4.4 lib/github_api/filter.rb