简体   繁体   中英

Read JSON file in git repo without checkout

I have the following method:

def getEndpointContent(url)
        return JSON.parse(open(url).read)
end

I want to use this to return the contents of a json file located in a git repo without checking out the repository.

However, if I pass in, for example, https://github.com/MyRep/myFile.json for the url parameter, I get the following error:

`connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

Is what I'm trying to do possible, and if so, how?

You won't be able to access the file using that URL.

GitHub provides raw file access using a different domain, and you haven't included your user or organization name. Also remember that a Git repository isn't simply a directory; you'll also have to provide a branch name or commit hash or something similar to tell GitHub which version of the file you want to see.

Something like this should work:

https://raw.githubusercontent.com/MyUser/MyRepo/master/myFile.json

You can find the raw link for a file by browsing to it in the GitHub UI and clicking the "Raw" link in the file's header.

Ruby doesn't trust the Github SSL certificate probably because it's too new for your version of Ruby and/or your OS.

Try the following from the command-line (assuming linux/OSX):

wget http://curl.haxx.se/ca/cacert.pem

Now in your Ruby code:

ENV['SSL_CERT_FILE'] = "/path/to/your/download/cacert.pem" # where you downloaded file to
require 'open-uri'  # ensure this is after the above line.

def getEndpointContent(url)
    return JSON.parse(open(url).read)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM