我把它一起作为一种表面上看起来很健壮的方式来调用一个不稳定的web服务,它会给出超时和偶尔的名称解析或套接字错误等等.我想我会把它放在这里以防它有用,或者更有可能被告知更好的方法来做到这一点.
require 'net/http'
retries = 5
begin
url = URI.parse('http://api.flakywebservice.com')
http = Net::HTTP.new(url.host, url.port)
http.read_timeout = 600 # be very patient
res = nil
http.start{|http|
req = Net::HTTP::Post.new(url.path)
req.set_form_data(params) # send a hash of the POST parameters
res = http.request(req)
}
rescue Exception # should really list all the possible http exceptions
sleep 3
retry if (retries -= 1) > 0
end
# finally, do something with res.body, like JSON.parse(res.body)
Run Code Online (Sandbox Code Playgroud)
这个问题的核心是:在打电话给这样的网络服务时,我应该寻找所有例外情况?这是尝试收集所有这些,但似乎必须有一个比这更好的方法:http: //tammersaleh.com/posts/rescuing-net-http-exceptions
我正在使用GitPython计算git中的暂存文件.
对于修改过的文件,我可以使用
repo = git.Repo()
modified_files = len(repo.index.diff(None))
Run Code Online (Sandbox Code Playgroud)
但对于分阶段文件,我找不到解决方案.
我知道,git status --porcelain
但我正在寻找更好的其他解决方案.(我希望使用gitpython
不是git命令,脚本会更快)
我正在使用Jekyll创建一个博客/网站.我有它设置,以便它显示如下的URL:
http://example.com/blog/title-of-post
Run Code Online (Sandbox Code Playgroud)
我想修改它以更改URL的"博客"部分.例如:
http://example.com/writing/title-of-post
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我无法找到如何将所选文本用作AppleScript和Automator的变量.
有任何想法吗?
网站http://example.com的根目录正确识别index.html并呈现它.以类似的方式,我希望http://example.com/foo获取目录根目录中的foo.html.使用此功能的网站是www.zachholman.com.我在Github看过他的代码.但我仍然无法找到它是如何完成的.请帮忙.
我正在创建一个基本的Ruby on Rails 4项目,允许用户创建帐户,登录等...我正在使用内置has_secure_password
来管理密码.我不希望用户必须输入两次密码(即需要password_confirmation
输入表单字段和相应的模型属性).所以,我正在寻找一种方法来关闭password_confirmation
检查/要求.
我发现这个答案提供了一个潜在的解决方案,但原始问题不同,我想单独验证它.它建议更新用户模型以添加以下内容:
class User < ActiveRecord::Base
# ...
has_secure_password validations: false
validates :password, presence: true, length: { minimum: 6 }
end
Run Code Online (Sandbox Code Playgroud)
这似乎工作,并允许我的RSpec测试通过.我的两个问题是:
password_confirmation
that are safer or more inline with "The Ruby Way"?在Perl中,我使用以下一行语句通过正则表达式从字符串中提取匹配项并分配它们.这个找到一个匹配并将其分配给一个字符串:
my $string = "the quick brown fox jumps over the lazy dog.";
my $extractString = ($string =~ m{fox (.*?) dog})[0];
Run Code Online (Sandbox Code Playgroud)
结果: $extractString == 'jumps over the lazy'
这个从多个匹配创建一个数组:
my $string = "the quick brown fox jumps over the lazy dog.";
my @extractArray = $string =~ m{the (.*?) fox .*?the (.*?) dog};
Run Code Online (Sandbox Code Playgroud)
结果: @extractArray == ['quick brown', 'lazy']
是否有相同的方法在Ruby中创建这些单行?
我已经根据文章如何使用 LocalStack 在本地伪造 AWS设置了localstack安装。我已经测试过将文件复制到模拟的 S3 服务,并且效果很好。
我开始寻找我上传的测试文件。我看到我上传的文件有一个编码版本.localstack/data/s3_api_calls.json
,但我在其他任何地方都找不到。
鉴于:DATA_DIR=/tmp/localstack/data
我期待在那里找到它,但事实并非如此。
我可以直接在文件系统上访问它并不重要,但这会很好。
我的问题是:是否有任何地方/方式可以查看上传到 localstack 的模拟 S3 服务的文件?
如何将值推送到 Rust 中枚举结构内的 vec?
我试图弄清楚如何将值推送到定义为结构的枚举内的 vec 。
这是设置以及我尝试过的一些内容:
enum Widget {
Alfa { strings: Vec<String> },
}
fn main() {
let wa = Widget::Alfa { strings: vec![] };
// wa.strings.push("a".to_string());
// no field `strings` on type `Widget`
// wa.Alfa.strings.push("a".to_string());
// no field `Alfa` on type `Widget`
// wa.alfa.strings.push("a".to_string());
// no field `alfa` on type `Widget`
// wa.Widget::Alfa.strings.push("a".to_string());
// expected one of `(`, `.`, `;`, `?`, `}`, or an operator, found `::`
// wa["strings"].push("a".to_string());
// cannot index into a value of …
Run Code Online (Sandbox Code Playgroud) ruby ×3
jekyll ×2
amazon-s3 ×1
api ×1
applescript ×1
automator ×1
git ×1
gitpython ×1
http ×1
localstack ×1
passwords ×1
python ×1
regex ×1
rest ×1
routes ×1
rust ×1
validation ×1
web-services ×1