justinweiss/robut — Issue #22: Routing, Transforming and Action DSL
Author: burtlo
Created: 2011-12-01 17:54:43+00:00
State: open

Working with Robut reminds me of all the hours I put into Cucumber. In a similar way a plugin defines criteria for matching incoming messages (similar to a `step definition`). Then when the message is matched to a particular regex I transform the parameters found within the message to the types/objects I need (similar to a `step transform`).

So I was toying around with the idea of some sort of DSL that would stream-line these operations but wanted to get some feedback.

```
register_route :play,
:sent_only_to_me,
:usage => "#{at_nick} play - queues from results for playing",
:match => /^(?:play)?\s?((?:\d[\s,-]*)+)$/,
:transform => lambda {|track_request|
Array(track_request).join(' ')[play_results_regex,-1].to_s.split(/(?:\s|,\s?)/).map do |track|
tracks = track.split("-")
(tracks.first.to_i..tracks.last.to_i).to_a
end.flatten
}

route_to_action :play => :play_result

def play_result(track_numbers)
time # => would return the time of the message
sender # => would return who sent the message
message # => would return the original message
# do some queuing and playing...
end
```

So a developer could:

1. Create / Register Routes
2. Tie routes to a method or methods that they would execute