Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created October 18, 2011 10:53
Show Gist options
  • Select an option

  • Save mindscratch/1295154 to your computer and use it in GitHub Desktop.

Select an option

Save mindscratch/1295154 to your computer and use it in GitHub Desktop.
Simplified HTTParty
include 'httparty'
module SimpleRest
class Request < HTTParty::Request
def attach_ssl_certificates(http)
# this method is overridden b/c its the only place we can modify the 'http' object
if http.use_ssl?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
if options[:cert]
http.cert = options[:cert]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:key]
http.cert = options[:key]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:ssl_ca_file]
http.cert = options[:ssl_ca_file]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:ssl_ca_path]
http.cert = options[:ssl_ca_path]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
# not provided by HTTParty
if options[:verify_mode]
http.cert = options[:cert]
http.verify_mode = options[:verify_mode]
end
end
end
end
require 'my_rest/request'
require 'httparty'
module MyRest
def self.included(base)
# include httparty before b/c classmethods provides custom 'perform_request'
base.send(:include, HTTParty)
base.extend ClassMethods
end
module ClassMethods
@@configured = false
def configured=(val); @@configured = val; end
def configured?; @@configured; end
def configure(&blk)
raise "already configured" if configured?
# prevent old config settings from sticking around
default_options.clear
instance_eval &blk
self.configured = true
end
def cert_file(cert_file)
contents = File.read cert_file
cert OpenSSL::X509::Certificate.new contents
end
def cert(cert)
default_options[:cert] = cert
end
def key_file(key_file, key_password=nil)
contents = File.read key_file
key OpenSSL::PKey::RSA.new contents, key_password
end
def key(key)
default_options[:key] = key
end
def verify_mode(verify_mode)
default_options[:verify_mode] = verify_mode || OpenSSL::SSL::VERIFY_NONE
end
def perform_request(http_method, path, options) #:nodoc:
options = default_options.dup.merge(options)
process_cookies(options)
MyRest::Request.new(http_method, path, options).perform
end
end
class Delegate
include SimpleRest
end
def self.method_missing(name, *args)
if name == :configure
# configure requires a block the only way I could get it was using this method here,
# it would be great not to have this "if/else"
# TODO: fix this
Delegate.send(name, &Proc.new)
else
Delegate.send(name, *args)
end
end
include 'httparty'
module MyRest
class Request < HTTParty::Request
def attach_ssl_certificates(http)
# this method is overridden b/c its the only place we can modify the 'http' object
if http.use_ssl?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
if options[:cert]
http.cert = options[:cert]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:key]
http.cert = options[:key]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:ssl_ca_file]
http.cert = options[:ssl_ca_file]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
if options[:ssl_ca_path]
http.cert = options[:ssl_ca_path]
http.verify_mode = OpenSSL:SSL::VERIFY_PEER
end
# not provided by HTTParty
if options[:verify_mode]
http.cert = options[:cert]
http.verify_mode = options[:verify_mode]
end
end
end
end
@skull-squadron
Copy link

s/OpenSSL:SSL::VERIFY_PEER/OpenSSL::SSL::VERIFY_PEER/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment