1

一部のデータをブロックとして外部 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})

これが私がやろうとしていることです。

4

1 に答える 1

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
  }

PS github問題

于 2015-01-10T16:31:27.633 に答える