I'm trying to pass some data as a block to some external API. It would be a hassle to accommodate it to accepting additional parameters. If it were javascript
, I might make it like so:
var callback = function() {
// do something
}
callback['__someData'] = options;
someExternalAPI(callback);
Is this possible with Ruby? Or how should I go about associating some data with a block?
Not sure if the edits to the question were correct. First, I'd like to specifically pass some data along with a block if that is possible. Not sure if it is though. And probably the only way to do it in ruby
is to pass some data as a block.
Additionally, here might be some useful info.
Okay, it probably makes sense to show the whole picture. I'm trying to adapt webmock
to my needs. I have a function, which checks if request's params (be them of POST
, or of GET
) match specified criteria:
def check_params params, options
options.all? do |k,v|
return true unless k.is_a? String
case v
when Hash
return false unless params[k]
int_methods = ['<', '<=', '>', '>=']
v1 = int_methods.include?(v.first[0]) ? params[k].to_i : params[k]
v2 = int_methods.include?(v.first[0]) \
? v.first[1].to_i : v.first[1].to_s
v1.send(v.first[0], v2)
when TrueClass, FalseClass
v ^ ! params.key?(k)
else
params[k] == v.to_s
end
end
end
It's not perfect, but it suffices for my particular needs, for now. I'm calling it like this:
stub_request(:post, 'http://example.com/')
.with { |request|
check_params Rack::Utils.parse_query(request.body), options
}
And the thing is generally I see no sensible way to output with block
conditions. But in my particular case one can just output options
hash. And instead of this:
registered request stubs:
stub_request(:post, "http://example.com")
to have this:
stub_request(:post, "http://example.com").
with(block: {"year"=>2015})
Which is what I'm trying to do.