Skip to content

Configure the recipe table

Jess edited this page Jul 13, 2016 · 3 revisions

The recipe table matches client input to a recipe, allowing the client to simply call Whowas.search without having to worry about selecting a recipe. It exists as a configuration setting in the app/config/initializers/whowas.rb file.

First, if you have not generated the configuration file already, run:

rails generate whowas:install

The simplest example

If you only have one recipe, set it as the default recipe:

config.recipe_table = {
  default: "my_recipe"
end

The recipes themselves are messages sent to the Whowas module. Thus, "my_recipe" gets transformed to Whowas.my_recipe, which should define the correct middleware stack.

A complex example (all bundled options)

There are four types of keys for the recipe table hash, from most specific to most general. Code blocks indicate that exact string should be used as the key:

  1. IP address or CIDR block. (Ex. 192.168.0.0/16) - selected if input[:ip] is non-null and matches the IP or CIDR block given.
  2. ip_default - selected if input[:ip] is non-null (and doesn't match #1).
  3. mac_default - selected if input[:mac] is non-null (and input[:ip] is null).
  4. default - selected if no other conditions match.
config.recipe_table = {
  "192.168.1.0/24": "home_wireless",
  "10.0.0.0/8": "internal_wired",
  ip_default: "other_ips",
  mac_default: "search_by_mac",
  default: "other_ips"
}

Technically, ip_default in the above example is superfluous, but is included for completeness.

Extensibility

Although Whowas was developed primarily for looking up IP and mac addresses, it could easily be used for other purposes. If you want to create your own custom logic for selecting recipes using other inputs, extend Whowas::Recipes, add your logic, and set the resulting class as the recipes_class in the initializer:

config.recipe_class = Whowas::MyCustomRecipes