-
Notifications
You must be signed in to change notification settings - Fork 3
Configure the recipe table
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
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.
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:
- IP address or CIDR block. (Ex.
192.168.0.0/16
) - selected ifinput[:ip]
is non-null and matches the IP or CIDR block given. -
ip_default
- selected ifinput[:ip]
is non-null (and doesn't match #1). -
mac_default
- selected ifinput[:mac]
is non-null (andinput[:ip]
is null). -
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.
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