我试图将一些数据作为块传递给某些外部API。容纳它以接受其他参数将是一件麻烦事。如果是javascript
,我可能会这样做:
var callback = function() {
// do something
}
callback['__someData'] = options;
someExternalAPI(callback);
这可能与Ruby有关吗?或者我应该如何将一些数据与一个块相关联?
不确定问题的编辑是否正确。首先,如果可能的话,我想特意传递一些数据和块。不确定是不是。可能在ruby
中执行此操作的唯一方法是将一些数据作为块传递。
此外,here可能是一些有用的信息。
好吧,展示整个画面可能很有意义。我试图让webmock
适应我的需求。我有一个函数,用于检查请求的params(属于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})
我正在尝试做的事。
答案 0 :(得分:0)
好的,我最终这样做了:
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
}
P.S。 github
issue