我正在尝试将一些数据作为块传递给一些外部API。适应它接受其他参数会很麻烦。如果是javascript
,我可能会这样做:
var callback = function() {
// do something
}
callback['__someData'] = options;
someExternalAPI(callback);
这在 Ruby 中可能吗?或者我应该如何将一些数据与块相关联?
不确定对问题的编辑是否正确。首先,如果可能的话,我想专门传递一些数据以及一个块。不知道是不是。在ruby
中执行此操作的唯一方法可能是将一些数据作为块传递。
此外,这里可能是一些有用的信息。
好的,展示整个画面可能是有意义的。我正在努力使webmock
适应我的需求。我有一个函数,它检查请求的参数(无论是 POST
的还是GET
的)是否符合指定的条件:
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
它并不完美,但目前足以满足我的特定需求。我是这样称呼它的:
stub_request(:post, 'http://example.com/')
.with { |request|
check_params Rack::Utils.parse_query(request.body), options
}
总的来说,我认为没有明智的方法来输出with block
条件。但在我的特殊情况下,人们可以只输出options
哈希。取而代之的是:
registered request stubs:
stub_request(:post, "http://example.com")
要有这个:
stub_request(:post, "http://example.com").
with(block: {"year"=>2015})
这就是我正在努力做的。
好吧,我最终这样做了:
p = Proc.new {}
p.class.module_eval { attr_accessor :__options }
p.__options = {a: 1}
# ...
pp p.__options
或者更具体地说:
def mk_block_cond options, &block_cond
options = options.select { |k,v| ! k.is_a?(Symbol) }
return block_cond if options.empty?
block_cond.class.module_eval { attr_accessor :__options }
block_cond.__options = options
block_cond
end
module WebMock
class RequestPattern
attr_reader :with_block
end
end
module StubRequestSnippetExtensions
def to_s(with_response = true)
request_pattern = @request_stub.request_pattern
string = "stub_request(:#{request_pattern.method_pattern.to_s},"
string << " "#{request_pattern.uri_pattern.to_s}")"
with = ""
if (request_pattern.body_pattern)
with << ":body => #{request_pattern.body_pattern.to_s}"
end
if (request_pattern.headers_pattern)
with << ",n " unless with.empty?
with << ":headers => #{request_pattern.headers_pattern.to_s}"
end
if request_pattern.with_block
&& request_pattern.with_block.respond_to?('__options')
&& request_pattern.with_block.__options
with << ",n " unless with.empty?
with << "block: #{request_pattern.with_block.__options}"
end
string << ".n with(#{with})" unless with.empty?
if with_response
string << ".n to_return(:status => 200, :body => "", :headers => {})"
end
string
end
end
module WebMock
class StubRequestSnippet
prepend StubRequestSnippetExtensions
end
end
module RequestPatternExtensions
def to_s
string = "#{@method_pattern.to_s.upcase}"
string << " #{@uri_pattern.to_s}"
string << " with body #{@body_pattern.to_s}" if @body_pattern
string << " with headers #{@headers_pattern.to_s}" if @headers_pattern
if @with_block
if @with_block.respond_to?('__options')
&& @with_block.__options
string << " with block: %s" % @with_block.__options.inspect
else
string << " with given block"
end
end
string
end
end
module WebMock
class RequestPattern
prepend RequestPatternExtensions
end
end
现在我以这种方式存根请求:
stub_request(:post, 'http://example.com/')
.with &mk_block_cond(options) { |request|
check_params Rack::Utils.parse_query(request.body), options
}
附言 github
问题