Creating get wrappers that can use helpers

12 views
Skip to first unread message

Rob Napier

unread,
Oct 27, 2009, 2:31:30 PM10/27/09
to sinatrarb
I just started playing with Sinatra today in the effort to create a
very simple database application. My goal is a simple REST service
that you hand hierarchical keys like "/Service/LDAP" and it hands you
back a value like "ldap.example.com." I want some of these keys to be
user-specific, so I wired in LDAP authentication (amazing that this
works as easily as it does).

So I keep winding up with blocks that look like this (protected! is
based on the code from the FAQ; ignore the fact that "system version"
probably shouldn't require login):

get '/System/version' do
protected!
{ '/System/version' => '0.1' }.to_json
end

I'd like to pull out the common code in something like this:

def value(key, &block)
get key do
{ key => block.call }.to_json
end
end

value '/System/version' do
protected!
'0.1'
end

But if I do this, then the block won't be able to see protected! when
it executes. If I move value() into helpers, then my main code can't
see it.

Similarly, is there any way to get the name of the current route in
the block? There's params, but I want the entire string, and I can't
find a value in params that gives me that.

Thanks,
-Rob

Nicolás Sanguinetti

unread,
Oct 27, 2009, 2:56:07 PM10/27/09
to sina...@googlegroups.com
Have you tried using instance_eval(&block) instead of block.call? That should work.

Rob Napier

unread,
Oct 27, 2009, 3:11:55 PM10/27/09
to sinatrarb
On Oct 27, 2:56 pm, Nicolás Sanguinetti <godf...@gmail.com> wrote:
Works perfectly.

Thanks,
-Rob
Reply all
Reply to author
Forward
0 new messages