<commit_before>= ParamChecker

ParamChecker is a small Ruby library for validating and casting string parameters. It is for example a handy way to check GET/POST +params+ in Ruby On Rails.

== Installation

  gem install param_checker

or add ParamChecker to your Gemfile

  gem 'param_checker'

and afterwards (when using the Gemfile) execute

  bundle install

== Usage

Include the ParamChecker module where ever you like. I usually put it into my Rails <tt>ApplicationController.rb</tt>

  class ApplicationController < ActionController::Base
    include ParamChecker
  end

You can then simply call for example <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.

Instead of including the module you could also call all functions of the module directly, like

  ParamChecker.check_string(param_to_check, "my default", ["foo", "bar"])

There are currently 5 supported functions:

  check_integer(param, default, min, max)
  check_float(param, default, min, max)
  check_string(param, default, allowed)
  check_symbol(param, default, allowed)
  check_boolean(param, default)

+param+ is always the string parameter to check. +default+ is a value that is returned when +param+ does not succeed the check.
+min+ and +max+ in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. Both +min+ and +max+ options are optional. If they are not provided then no range is checked at all.
+allowed+ in +check_string+ and +check_symbol+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol+), a regular expression, or an array of strings (resp. symbols for +check_symbol+).
+check_boolean+ evaluates "1" or "true" string as true and "0" or "false" string to false.
All functions return the parsed and type cast value (check_integer returns an integer, check_symbol returns a symbol, and so on).

== Examples

Below are some simple examples how I use those function in my controllers.

  # +max+ is not provided in this case. I just want to ensure that page is bigger than 1 and otherwise return 1.
  page = check_integer(params[:page], 1, 1)

  # Returns "name" if params[:field] is not "name" or "address"
  field = check_string(params[:field], "name", ["name", "address"])

  # Return the boolean if params[:accepted] is a valid boolean representation, and the default false otherwise.
  accepted = check_boolean(params[:accepted], false)

== Testing

ParamChecker uses RSpec for testing and has a rake task for executing the provided specs

  rake spec

Copyright © 2010-2011 <NAME> (http://www.medihack.org), released under the MIT license
<commit_msg>Updated docs.<commit_after>= ParamChecker

ParamChecker is a small Ruby library for validating and casting string parameters. It is for example a handy way to check GET/POST +params+ in Ruby On Rails.

== Installation

  gem install param_checker

or add ParamChecker to your Gemfile

  gem 'param_checker'

and afterwards (when using the Gemfile) execute

  bundle install

== Usage

Include the ParamChecker module where ever you like. I usually put it into my Rails <tt>ApplicationController.rb</tt>

  class ApplicationController < ActionController::Base
    include ParamChecker
  end

You can then simply call for example <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.

Instead of including the module you could also call all functions of the module directly, like

  ParamChecker.check_string(param_to_check, "my default", ["foo", "bar"])

There are currently 5 supported functions:

  check_integer(param, default, min, max)
  check_float(param, default, min, max)
  check_string(param, default, allowed)
  check_symbol(param, default, allowed)
  check_boolean(param, default)

+param+ (_required_) is the string parameter to check.
+default+ (_required_) is the value that is returned when +param+ does not pass the check.
+min+ (_optional_), +max+ (_optional_) in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. (If not provided then no range is checked at all.)
+allowed+ (_optional_) in +check_string+ and +check_symbol+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol+), a regular expression, or an array of strings (resp. symbols for +check_symbol+).
+check_boolean+ evaluates "1" or "true" string as true and "0" or "false" string to false.

All functions return the the cast value (check_integer returns an integer, check_symbol returns a symbol, and so on).

== Examples

Below are some simple examples how I use those function in my controllers.

  # +max+ is not provided in this case. I just want to ensure that page is bigger than 1 and otherwise return 1.
  page = check_integer(params[:page], 1, 1)

  # Returns "name" if params[:field] is not "name" or "address"
  field = check_string(params[:field], "name", ["name", "address"])

  # Return the boolean if params[:accepted] is a valid boolean representation, and the default false otherwise.
  accepted = check_boolean(params[:accepted], false)

== Testing

ParamChecker uses RSpec for testing and has a rake task for executing the provided specs

  rake spec

Copyright © 2010-2011 <NAME> (http://www.medihack.org), released under the MIT license

{'fraction_non_alphanumeric': 0.08203491646330018, 'fraction_numerical': 0.005631687629059508, 'mean_word_length': 4.7976060935799785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697128', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1442, 'n_words': 707}