我遇到了从Builder :: XmlMarkup对象访问原始xml的问题.
irb> xml = Builder::XmlMarkup.new(:target => '')
=> <pretty_inspect/>
irb> xml.foo("bar")
=> "<pretty_inspect/><foo>bar</foo>"
irb> puts xml
TypeError: can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)
from (pry):122:in `puts'
Run Code Online (Sandbox Code Playgroud)
在我使用Builder创建XML的脚本中,我将@xml传递给POST:
response = HTTParty.post(API_ENDPOINT, :body => @xml)
Run Code Online (Sandbox Code Playgroud)
这给出了同样的错误:
TypeError (can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)):
Run Code Online (Sandbox Code Playgroud)
当然,如果我执行@ xml.to_xml,它不会返回错误,但它会添加</to_xml>到xml,这意味着它实际上并没有将XML对象转换为xml.那不是我想要的.
那么如何才能获得对xml的访问权限,以便我可以将它传递给我的帖子,而无需在xml中添加额外的节点?
编辑:可能的解决方案
做@xml.target!似乎解决了这个问题,但我不确定我理解为什么.
response = HTTParty.post(API_ENDPOINT, :body => @xml.target!)
Run Code Online (Sandbox Code Playgroud)
也许有人可以帮助我理解这里发生的事情.
我想做这样的事情:
class A(object):
def __init__(self, **kwargs):
"""
return exception if certain arguments not set
"""
class B(A):
def __init__(self, **kwargs):
super(B, self).__init__(**kwargs)
Run Code Online (Sandbox Code Playgroud)
基本上,每个子类都需要正确实例化某些参数.它们是相同的参数.我只想一次检查这些参数.如果我可以从父init()做到这一点- 那就更好了.
是否有可能做到这一点?
我正在为一个流行的API工作的客户端库.目前,我对所述客户端的所有单元测试都是针对测试帐户进行实际的API调用.
这是一个例子:
def test_get_foo_settings(self):
client = MyCustomClient(token, account)
results = client.get_foo_settings()
assert_is(type(results), list)
Run Code Online (Sandbox Code Playgroud)
我想停止针对我的测试帐户进行实际的API调用.
我应该怎么解决这个问题?我应该使用模拟来模拟对客户端的响应和响应吗?
此外,我对使用此客户端库测试的内容的理念感到困惑.我对测试实际的API不感兴趣,但是当涉及到不同的因素时,例如调用的方法,可能的返回结果的排列等等 - 我不确定我应该测试什么和/或何时安全做出假设(例如模拟的回应).
在我的场景类型中如何使用Mock的任何方向和/或样本将不胜感激.
我正在研究一个不需要选择器的jQuery插件,所以我只是将它添加到全局jQuery命名空间.
我在插件的公共方法之间共享内部实例变量时遇到问题.例如,我希望能够从.init方法访问$ .MyPlugin.settings.
这是插件代码:
(function($) {
$.MyPlugin = function(options) {
var defaults = {
username: '',
password: ''
}
var plugin = this;
plugin.settings = {};
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
}
plugin.init();
}
$.MyPlugin.init = function(callback) {
console.log(this.settings); // undefined
callback();
}
}(jQuery));
Run Code Online (Sandbox Code Playgroud)
在main.js中,我有:
$(document).ready(function() {
$.MyPlugin({
username: 'myuser',
password: 'mypass'
});
$.MyPlugin.init(function() {
console.log('hi');
});
Run Code Online (Sandbox Code Playgroud)
返回:
undefined
hi
Run Code Online (Sandbox Code Playgroud) 我正在尝试制定一个POST使用请求,但是在我尝试添加to对象的任何时候我都会收到错误formData.
var fs = require('fs');
var request = require('request');
var file = './test/assets/test.pdf';
var opts = {
url: 'my_service',
method: 'POST',
auth: { user: 'username', password: 'password' },
json: true,
formData: {
front: fs.createReadStream(file),
to: {
name: 'joe bob',
address_1: '123 main st',
...
}
}
};
request(opts, function(err, resp, body) {
console.log(err, body);
});
Run Code Online (Sandbox Code Playgroud)
这是错误:
/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
source.on('error', function() {});
^
TypeError: undefined is not a function
at Function.DelayedStream.create (/Users/me/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append …Run Code Online (Sandbox Code Playgroud) 我正在开发一个支持脱机的应用程序,我想在$ .get()上调用一个ajax调用我将要缓存的域名文件.
如果我离线,通话仍然有效吗?我会假设没有,但我想知道我是否将静态页面包含在离线缓存中,如果它仍然可以工作.
说我有一个对象:
userInfo
Run Code Online (Sandbox Code Playgroud)
我想搜索userInfo的每个节点以查看键“ username”是否具有等于foo的值。
userInfo[x].username == "foo"
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来执行以下操作?
var matchFound = false;
for (var i = 0, len = userInfo.length; i < len; i++)
matchFound = userInfo[i].username == "foo";
Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用t并在test.js中使用它的脚本。我将把输出通过电子邮件发送给我和我的同事。
% mongo my_db --eval 't=9999;' --quiet test.js
9999
------------------------------------------------
Info about stuff going back 9999 days to 2012-08-17.
------------------------------------------------
Stuff x: 433321 (12.43%)
Stuff y: 2723426 (81.57%)
Total: 4524524524
Run Code Online (Sandbox Code Playgroud)
有没有办法不将我传递给--eval的内容输出到控制台,因此我的结果顶部没有悬挂的“ 9999”?
编辑:这可能是--quiet选项的错误
我们有一些数据库调用可以轻松地传输10万条记录。我们遇到的问题是,每当我们使用这些流之一时,它就会挂住CPU,并似乎阻塞了所有其他进程。
我尝试了几种不同的技巧来减轻这种情况,但是现在有点卡住了。这是我最近一次尝试将流传输到使用它的Transform的尝试process.nextTick。
var stream = require('stream');
var util = require('util');
function StreamThrottler() {
stream.Transform.call(this, { objectMode: true });
}
util.inherits(StreamThrottler, stream.Transform);
StreamThrottler.prototype._transform = function(chunk, encoding, cb) {
process.nextTick(function() {
console.log('chunk');
// note: I'm intentionally not pushing the chunk
// onto the stream for testing
cb();
});
};
StreamThrottler.prototype._flush = function(cb) {
cb();
};
var streamThrottler = new StreamThrottler();
// now the db call
this.largeDatabaseResultStream().pipe(streamThrottler);
Run Code Online (Sandbox Code Playgroud)
我注意到这个 Node.js问题可能相关,也可能不相关。
有人对如何解决这个问题有其他想法吗?
我们正在实施 PostgreSQL 触发器来监视多个表上的插入/更新/删除,以便另一个监听这些事件的应用程序可以使我们的关系数据库与我们的全文搜索数据库保持同步。
下面是触发器函数的样子:
CREATE FUNCTION notification() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('search', TG_TABLE_NAME || ',id,' || NEW.id);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
这是我们向每个表添加触发器的方式:
CREATE TRIGGER foo_trigger AFTER INSERT OR UPDATE or DELETE ON foo
FOR EACH ROW EXECUTE PROCEDURE notification();
Run Code Online (Sandbox Code Playgroud)
这是一个非常基本的示例,说明我们如何让节点应用程序(工作程序)侦听这些触发事件:
var pg = require('pg');
var connString = "postgres://user@localhost/foo_local";
pg.connect(connString, function(err, client, done) {
client.on('notification', function(msg) {
//get the added / updated / deleted record
//sync it with the search database
});
var query = client.query('LISTEN search'); …Run Code Online (Sandbox Code Playgroud) javascript ×6
node.js ×3
jquery ×2
python ×2
ajax ×1
builder ×1
events ×1
html5 ×1
kwargs ×1
loops ×1
mocking ×1
mongodb ×1
offline-mode ×1
postgresql ×1
ruby ×1
stream ×1
streaming ×1
triggers ×1
unit-testing ×1
xml ×1