diff options
| author | mo <mo@mokhan.ca> | 2018-05-07 14:55:40 -0600 |
|---|---|---|
| committer | mo <mo@mokhan.ca> | 2018-05-07 14:55:40 -0600 |
| commit | 8fa9c4e921b87c6d7b3b8d34a4506653ad5117c9 (patch) | |
| tree | 6c8841d23808e0d27f89c10492192915ea41893d | |
| parent | 9014e187ce3591a3faf1075efac830893f023e0d (diff) | |
support a passphrase.v0.1.0
| -rw-r--r-- | README.md | 40 | ||||
| -rw-r--r-- | lib/net/hippie/client.rb | 19 |
2 files changed, 51 insertions, 8 deletions
@@ -1,8 +1,7 @@ # Net::Hippie -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/net/hippie`. To experiment with that code, run `bin/console` for an interactive prompt. - -TODO: Delete this and the text above, and describe your gem +Net::Hippie is a light weight wrapper around `net/http` that defaults to +sending JSON messages. ## Installation @@ -22,7 +21,38 @@ Or install it yourself as: ## Usage -TODO: Write usage instructions here +```ruby +require 'net/hippie' + +Net::Hippie.logger = Rails.logger + +client = Net::Hippie::Client.new(headers: { + 'Accept' => 'application/vnd.haveibeenpwned.v2+json' +}) + +response = client.get(URI.parse('https://haveibeenpwned.com/api/breaches')) + +puts JSON.parse(response.body) +``` + +Net::Hippie also supports TLS with client authentication. + +```ruby +client = Net::Hippie::Client.new( + certificate: ENV['CLIENT_CERTFICIATE'], + key: ENV['CLIENT_KEY'] +) +``` + +If your private key is encrypted you may include a passphrase to decrypt it. + +```ruby +client = Net::Hippie::Client.new( + certificate: ENV['CLIENT_CERTFICIATE'], + key: ENV['CLIENT_KEY'], + passphrase: ENV['CLIENT_KEY_PASSPHRASE'] +) +``` ## Development @@ -32,7 +62,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/net-hippie. +Bug reports and pull requests are welcome on GitHub at https://github.com/mokhan/net-hippie. ## License diff --git a/lib/net/hippie/client.rb b/lib/net/hippie/client.rb index dd1f228..b43e036 100644 --- a/lib/net/hippie/client.rb +++ b/lib/net/hippie/client.rb @@ -7,7 +7,13 @@ module Net 'User-Agent' => "net/hippie #{Net::Hippie::VERSION}", } - def initialize(headers: DEFAULT_HEADERS, certificate: nil, key: nil, mapper: JsonMapper.new) + def initialize( + headers: DEFAULT_HEADERS, + certificate: nil, + key: nil, + passphrase: nil, + mapper: JsonMapper.new + ) @certificate = certificate @default_headers = headers @key = key @@ -50,7 +56,8 @@ module Net private - attr_reader :default_headers, :certificate, :key + attr_reader :default_headers + attr_reader :certificate, :key, :passphrase attr_reader :mapper def http_for(uri) @@ -59,7 +66,13 @@ module Net http.use_ssl = uri.is_a?(URI::HTTPS) http.set_debug_output(Net::Hippie.logger) http.cert = OpenSSL::X509::Certificate.new(certificate) if certificate - http.key = OpenSSL::PKey::RSA.new(key) if key + if key + if passphrase + http.key = OpenSSL::PKey::RSA.new(key, passphrase) + else + http.key = OpenSSL::PKey::RSA.new(key) + end + end http end |
