开发者

What is the right syntax for Ruby to do a system call to curl?

To refresh Redmine, I need SVN to ping our Redmine installation from our post-commit hook. Our post-commit hook is a Ruby script that generates an email. I'd like to insert a call do this:

curl --insecure https://redmineserver+webappkey

This call works from the command line but when I try to do this:

#!/usr/bin/ruby -w

REFRESH_DRADIS_URL = "https://redmineserver+webappkey"
system("/usr/bin/curl", "--insecure", "#{REFRESH_DRADIS_URL}")

It doesn't work. How do I do this in ruby开发者_运维知识库? I googled 'ruby system curl' but I just got a bunch of links to integrate curl into ruby (which is NOT what I'm interested in).


There are many ways

REFRESH_DRADIS_URL = "https://redmineserver+webappkey"
result = `/usr/bin/curl --insecure #{REFRESH_DRADIS_URL}`

but I don't think you have to use curl at all. Try this

require 'open-uri'
open(REFRESH_DRADIS_URL)

If the certificate isn't valid then it gets a little more complicated

require 'net/https'
http = Net::HTTP.new("amazon.com", 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp, data = http.get("/")


system ("curl --insecure #{url}")


For such a simple problem, I wouldn't bother with shelling out to curl, I'd simply do

require 'net/https'

http = Net::HTTP.new('redmineserver+webappkey', 443)

http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

http.get('/')

And for more complex problems, I'd still not shell out to curl, but rather use one of the many Ruby libcurl bindings.


I had to do this recently and tried the following and it worked:

test.rb

class CurlTest
   def initialize()
   end
   def dumpCurl
      test = `curl -v https://google.com 2>&1`
      puts test
   end
end

curlTest = CurlTest.new()
curlTest.dumpCurl
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜