Skip to content

Instantly share code, notes, and snippets.

@dataday
Last active August 24, 2017 13:47
Show Gist options
  • Save dataday/04b239ee9d48053851641c5661eb6433 to your computer and use it in GitHub Desktop.
Save dataday/04b239ee9d48053851641c5661eb6433 to your computer and use it in GitHub Desktop.
Uses RestClient to make request to secured domain
#...
def make_request(url, secure = false)
url = url(url) # Make Addressable
opts = secure ? { verify_ssl: OpenSSL::SSL::VERIFY_PEER } : {}
opts.merge!(content_type: @config[:content_type], user_agent: @config[:agent])
cert = @config[:cert_path] || ''
pass = @config[:cert_password] || ''
if File.exist?(cert) and url.host and url.path
file = IO.read(cert)
suffix = File.extname(cert)
case suffix
when '.pem'
opts.merge!(
ssl_client_cert: OpenSSL::X509::Certificate.new(file),
ssl_client_key: OpenSSL::PKey::RSA.new(file, pass),
ssl_ca_file: '/etc/pki/tls/cacert.pem'
)
when '.p12'
cert = OpenSSL::PKCS12.new(file, pass)
opts.merge!(
ssl_client_cert: cert.certificate,
ssl_client_key: cert.key
)
end
RestClient.proxy = secure ? @config[:https_proxy] : @config[:http_proxy]
RestClient::Resource.new("#{url}", opts).get
end
end
def get_data(url, callback = nil)
secure = url.scheme == 'https' ? true : false
data = make_request(url, secure)
data = callback.call(data) if callback.is_a? Proc
data
end
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment