Read a private file from Github in Ruby - ruby-on-rails

Looked at this for initial solutions. However, the file I want to reference in my rails project is in a private file. When I perform the following code:
uri = URI("https://.../config.yml")
file = Net::HTTP.get(uri)
config = YAML.load(file)
The 'file' has the contents of the sign-in page of github. Is it possible to pass credentials to access this private repo's file? Additionally, is this safe to do?

You have to set the credentials via a header. Something like this should work
token = "123"
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Token #{token}"
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
Otherwise I also suggest to just use the Github client library Octokit.

Related

how do I include a header in an http request in ruby

Have the below code working:
uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
response = Net::HTTP.get_response(uri)
Now I also need to pass a header with this token hash in it:
token: "fjhKJFSDHKJHjfgsdfdsljh"
I cannot find any documentation on how to do this. How do I do it?
get_response is a shorthand for making a request, when you need more control - do a full request yourself.
There's an example in ruby standard library here:
uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
req = Net::HTTP::Get.new(uri)
req['token'] = 'fjhKJFSDHKJHjfgsdfdsljh'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
Though you surely could use Net::HTTP for this goal, gem excon allows you to do it far easier:
require 'excon'
url = 'http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/'
Excon.get(url, headers: {token: 'fjhKJFSDHKJHjfgsdfdsljh'})

Send JSON data as post method from rails controller to a web service

I am trying to send some json data from my controller written in rails to a java webservice.
On form submission i take all the input fields data do some procession on it and convert it into json object using to_json.
But how can i send it to java webservice
http://localhost:8080/exim/jsonToMapService?jsonData={"key":"value"}
You can use net/http. (as #Pierre wrote, you should create a class in lib folder, and put there your function)
url = URI.parse(service_url)
headers = {"host" => URL }
req = Net::HTTP::Post.new(url.path)
req["Content-Type"] = "application/json"
req["Accept"] = "application/json"
req.body = JSON.generate(some_data)
con = Net::HTTP.new(url.host, url.port)
# ssl for https
if full_url.include?("https")
con.use_ssl = true
con.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = con.start {|http| http.request(req) }
To do things like this I suggest using either RestClient or Faraday. Howeve I strongly suggest not doing the HTTP call in your controller.
Using RestClient, it would look like this:
RestClient.get('http://localhost:8080/exim/jsonToMapService', { key: :value })
You should create a class to extract this logic in the lib folder for example.
As #eightbitraptor mentioned it, when performing HTTP request like above, you should avoid blocking by performing them in a background process like Delayed Job, Resque or Sideqik.

Ruby Post file to server

I am new to Ruby and trying to post file from web app to another server using the code below but without any success - the file is not posted. How do to it correctly?
uri = URI("http://do.convertapi.com/word2pdf")
puts file_path
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('file'=>file_path)
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
Try this:
https://github.com/taf2/curb#http-post-file-upload
This is cURL lib in ruby.I used this many times and works very well.

How can I Authenticate a http web request with a certificate?

I need to be able to send a http get request to a web service that requires clients to authenticate with a specific certificate. The .net code looks like this:
if (certificate != null)
request.ClientCertificates.Add(certificate);
return request;
I havent been able to figure out the equivalent in rails. Any suggestions?
If using basic net/https, then it's quite simple:
require 'net/https'
require 'uri'
uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
http.key = pkey #Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
http.cert= cert #Sets an OpenSSL::X509::Certificate object as client certificate
http.start {
http.request_get(uri.path) {|res|
print res.body
}
}
If you have them combined, you'll have to massage them using some openssl utility methods.
For a custom http client you should read the docs for the ruby openssl library for the gory details.
But in a nutshell, something like this should work:
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = private_key_file
ctx.cert = certificate_file
..and then supply the context to your connection.

Rails: How to get the servers response HTTP headers?

I want to autodetect the pingback-url of remote websites, so I need to parse the HTTP response headers sent by the remote server. I don't need and don't want the contents of the remote website, I'm only looking for something like this:
X-Pingback: http://www.techcrunch.com/xmlrpc.php
Similar to using curl:
curl -I "your url"
Is there a way to do this with rails? When using open-uri I can only get the contents but not the headers.
Thank you!
Ole
This is not a Rails specific question, but it can be solved with the core Ruby API.
require 'net/http'
url = URI.parse('http://www.google.ca/')
req = Net::HTTP::Head.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res['X-Pingback']
> "http://www.techcrunch.com/xmlrpc.php"
If you don't mind using a gem, you might try Patron, which is a ruby wrapper for the libcurl library. Usage might look something like this:
sess = Patron::Session.new
sess.timeout = 10
sess.base_url = "http://myserver.com:9900"
resp = sess.get( "your url" )
headers = resp.headers

Resources