Comparing changes

v0.1.4 v0.1.5
4 commits 17 files changed

Commits

27be594 fix rubocop errors. mo 2018-05-07 22:32:04
f828908 assign passphrase. mo 2018-05-07 22:20:11
a7ff82b rubocop -a mo 2018-05-07 22:17:53
31500bd hook up CI. mo 2018-05-07 22:16:53
bin/cibuild
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# script/cibuild: Setup environment for CI to run tests. This is primarily
+#                 designed to run on the continuous integration server.
+
+set -e
+
+cd "$(dirname "$0")/.."
+
+echo [$(date "+%H:%M:%S")] "==> Started at…"
+
+# GC customizations
+export RUBY_GC_MALLOC_LIMIT=79000000
+export RUBY_GC_HEAP_INIT_SLOTS=800000
+export RUBY_HEAP_FREE_MIN=100000
+export RUBY_HEAP_SLOTS_INCREMENT=400000
+export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
+
+ruby -v
+gem install bundler --no-ri --no-rdoc --conservative
+bin/test
+bin/lint
bin/console
@@ -1,7 +1,8 @@
 #!/usr/bin/env ruby
+# frozen_string_literal: true
 
-require "bundler/setup"
-require "net/hippie"
+require 'bundler/setup'
+require 'net/hippie'
 
 # You can add fixtures and/or initialization code here to make experimenting
 # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "net/hippie"
 # require "pry"
 # Pry.start
 
-require "irb"
+require 'irb'
 IRB.start(__FILE__)
bin/lint
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+set -e
+
+[ -z "$DEBUG" ] || set -x
+
+echo [$(date "+%H:%M:%S")] "==> Running linters…"
+bundle exec rake lint
bin/test
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+# script/test: Run test suite for application. Optionally pass in a path to an
+#              individual test file to run a single test.
+
+
+set -e
+
+cd "$(dirname "$0")/.."
+
+[ -z "$DEBUG" ] || set -x
+
+echo [$(date "+%H:%M:%S")] "==> Running setup…"
+bin/setup
+
+echo [$(date "+%H:%M:%S")] "==> Running tests…"
+bundle exec rake test
exe/net-hippie
@@ -1,5 +1,5 @@
 #!/usr/bin/env ruby
 
-require "net/hippie"
+require 'net/hippie'
 
-puts "☮️"
+puts '☮️'
lib/net/hippie/client.rb
@@ -1,11 +1,12 @@
 module Net
   module Hippie
+    # A simple client for connecting with http resources.
     class Client
       DEFAULT_HEADERS = {
         'Accept' => 'application/json',
         'Content-Type' => 'application/json',
-        'User-Agent' => "net/hippie #{Net::Hippie::VERSION}",
-      }
+        'User-Agent' => "net/hippie #{Net::Hippie::VERSION}"
+      }.freeze
 
       def initialize(
         certificate: nil,
@@ -18,6 +19,7 @@ module Net
         @default_headers = headers
         @key = key
         @mapper = mapper
+        @passphrase = passphrase
       end
 
       def execute(uri, request)
@@ -35,7 +37,8 @@ module Net
       end
 
       def post(uri, headers: {}, body: {})
-        request = request_for(Net::HTTP::Post, uri, headers: headers, body: body)
+        type = Net::HTTP::Post
+        request = request_for(type, uri, headers: headers, body: body)
         response = execute(uri, request)
         if block_given?
           yield request, response
@@ -66,13 +69,9 @@ module Net
         http.read_timeout = 30
         http.use_ssl = uri.is_a?(URI::HTTPS)
         http.set_debug_output(Net::Hippie.logger)
-        http.cert = OpenSSL::X509::Certificate.new(certificate) if certificate
-        if key
-          if passphrase
-            http.key = OpenSSL::PKey::RSA.new(key, passphrase)
-          else
-            http.key = OpenSSL::PKey::RSA.new(key)
-          end
+        if certificate && key
+          http.cert = OpenSSL::X509::Certificate.new(certificate) if certificate
+          http.key = private_key
         end
         http
       end
@@ -86,6 +85,14 @@ module Net
       def normalize_uri(uri)
         uri.is_a?(URI) ? uri : URI.parse(uri)
       end
+
+      def private_key
+        if passphrase
+          OpenSSL::PKey::RSA.new(key, passphrase)
+        else
+          OpenSSL::PKey::RSA.new(key)
+        end
+      end
     end
   end
 end
lib/net/hippie/json_mapper.rb
@@ -1,5 +1,6 @@
 module Net
   module Hippie
+    # Converts a ruby hash into a JSON string
     class JsonMapper
       def map_from(hash)
         JSON.generate(hash)
lib/net/hippie/version.rb
@@ -1,5 +1,5 @@
 module Net
   module Hippie
-    VERSION = "0.1.4"
+    VERSION = '0.1.5'.freeze
   end
 end
lib/net/hippie.rb
@@ -1,13 +1,14 @@
-require "json"
-require "logger"
-require "net/http"
-require "openssl"
+require 'json'
+require 'logger'
+require 'net/http'
+require 'openssl'
 
-require "net/hippie/version"
-require "net/hippie/json_mapper"
-require "net/hippie/client"
+require 'net/hippie/version'
+require 'net/hippie/json_mapper'
+require 'net/hippie/client'
 
 module Net
+  # net/http for hippies.
   module Hippie
     def self.logger
       @logger ||= Logger.new(STDOUT)
test/net/client_test.rb
@@ -1,6 +1,6 @@
 require 'test_helper'
 
-class Net::Hippie::ClientTest < Minitest::Test
+class ClientTest < Minitest::Test
   attr_reader :subject
 
   def initialize(*args)
@@ -9,7 +9,7 @@ class Net::Hippie::ClientTest < Minitest::Test
   end
 
   def test_get
-    VCR.use_cassette("get_breaches") do
+    VCR.use_cassette('get_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
       response = subject.get(uri)
       refute_nil response
@@ -18,7 +18,7 @@ class Net::Hippie::ClientTest < Minitest::Test
   end
 
   def test_get_with_string_uri
-    VCR.use_cassette("get_breaches") do
+    VCR.use_cassette('get_breaches') do
       response = subject.get('https://haveibeenpwned.com/api/breaches')
       refute_nil response
       assert_equal(283, JSON.parse(response.body).count)
@@ -26,9 +26,9 @@ class Net::Hippie::ClientTest < Minitest::Test
   end
 
   def test_get_with_block_syntax
-    VCR.use_cassette("get_breaches") do
+    VCR.use_cassette('get_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
-      subject.get(uri) do |request, response|
+      subject.get(uri) do |_request, response|
         @response = response
       end
       refute_nil @response
@@ -39,8 +39,8 @@ class Net::Hippie::ClientTest < Minitest::Test
   def test_get_with_headers
     headers = { 'Accept' => 'application/vnd.haveibeenpwned.v2+json' }
     WebMock.stub_request(:get, 'https://haveibeenpwned.com/api/breaches')
-      .with(headers: headers)
-      .to_return(status: 201, body: {}.to_json)
+           .with(headers: headers)
+           .to_return(status: 201, body: {}.to_json)
 
     uri = URI.parse('https://haveibeenpwned.com/api/breaches')
 
@@ -53,8 +53,8 @@ class Net::Hippie::ClientTest < Minitest::Test
     uri = URI.parse('https://haveibeenpwned.com/api/breaches')
     body = { 'hello' => 'world' }
     WebMock.stub_request(:get, uri.to_s)
-      .with(body: body.to_json)
-      .to_return(status: 201, body: {}.to_json)
+           .with(body: body.to_json)
+           .to_return(status: 201, body: {}.to_json)
 
     response = subject.get(uri, body: body)
 
@@ -63,44 +63,44 @@ class Net::Hippie::ClientTest < Minitest::Test
   end
 
   def test_post
-    VCR.use_cassette("post_breaches") do
+    VCR.use_cassette('post_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
       response = subject.post(uri)
       refute_nil response
-      assert_equal "Congratulations!", JSON.parse(response.body)["Message"]
+      assert_equal 'Congratulations!', JSON.parse(response.body)['Message']
     end
   end
 
   def test_post_with_block_syntax
-    VCR.use_cassette("post_breaches") do
+    VCR.use_cassette('post_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
-      subject.post(uri) do |request, response|
+      subject.post(uri) do |_request, response|
         @response = response
       end
       refute_nil @response
-      assert_equal "Congratulations!", JSON.parse(@response.body)["Message"]
+      assert_equal 'Congratulations!', JSON.parse(@response.body)['Message']
     end
   end
 
   def test_put
-    VCR.use_cassette("put_breaches") do
+    VCR.use_cassette('put_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
       body = { command: 'echo hello' }.to_json
       response = subject.put(uri, body: body)
       refute_nil response
-      assert_equal "Congratulations!", JSON.parse(response.body)["Message"]
+      assert_equal 'Congratulations!', JSON.parse(response.body)['Message']
     end
   end
 
   def test_put_with_block_syntax
-    VCR.use_cassette("put_breaches") do
+    VCR.use_cassette('put_breaches') do
       uri = URI.parse('https://haveibeenpwned.com/api/breaches')
       body = { command: 'echo hello' }.to_json
-      subject.put(uri, body: body) do |request, response|
+      subject.put(uri, body: body) do |_request, response|
         @response = response
       end
       refute_nil @response
-      assert_equal "Congratulations!", JSON.parse(@response.body)["Message"]
+      assert_equal 'Congratulations!', JSON.parse(@response.body)['Message']
     end
   end
 end
test/net/hippie_test.rb
@@ -1,6 +1,6 @@
-require "test_helper"
+require 'test_helper'
 
-class Net::HippieTest < Minitest::Test
+class HippieTest < Minitest::Test
   def test_that_it_has_a_version_number
     refute_nil ::Net::Hippie::VERSION
   end
test/test_helper.rb
@@ -1,11 +1,11 @@
-$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
-require "net/hippie"
-require "vcr"
-require "webmock"
+$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
+require 'net/hippie'
+require 'vcr'
+require 'webmock'
 
-require "minitest/autorun"
+require 'minitest/autorun'
 
 VCR.configure do |config|
-  config.cassette_library_dir = "test/fixtures"
+  config.cassette_library_dir = 'test/fixtures'
   config.hook_into :webmock
 end
.gitlab-ci.yml
@@ -0,0 +1,12 @@
+image: ruby:2.5
+
+before_script:
+  - apt-get update && apt-get install -y locales
+  - echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
+  - locale-gen
+  - export LC_ALL=en_US.UTF-8
+
+ci:
+  script:
+    - bin/cibuild
+
.travis.yml
@@ -1,5 +1,7 @@
 sudo: false
 language: ruby
+cache: bundler
 rvm:
   - 2.5.1
-before_install: gem install bundler -v 1.16.1
+script:
+  - bin/cibuild
Gemfile
@@ -1,6 +1,6 @@
-source "https://rubygems.org"
+source 'https://rubygems.org'
 
-git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
+git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
 
 # Specify your gem's dependencies in net-hippie.gemspec
 gemspec
net-hippie.gemspec
@@ -1,29 +1,30 @@
 
-lib = File.expand_path("../lib", __FILE__)
+lib = File.expand_path('lib', __dir__)
 $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
-require "net/hippie/version"
+require 'net/hippie/version'
 
 Gem::Specification.new do |spec|
-  spec.name          = "net-hippie"
+  spec.name          = 'net-hippie'
   spec.version       = Net::Hippie::VERSION
-  spec.authors       = ["mo"]
-  spec.email         = ["mo@mokhan.ca"]
+  spec.authors       = ['mo']
+  spec.email         = ['mo@mokhan.ca']
 
-  spec.summary       = %q{net/http for hippies.}
-  spec.description   = %q{net/http for hippies.}
-  spec.homepage      = "https://www.mokhan.ca/"
-  spec.license       = "MIT"
+  spec.summary       = 'net/http for hippies.'
+  spec.description   = 'net/http for hippies.'
+  spec.homepage      = 'https://www.mokhan.ca/'
+  spec.license       = 'MIT'
 
   spec.files         = `git ls-files -z`.split("\x0").reject do |f|
     f.match(%r{^(test|spec|features)/})
   end
-  spec.bindir        = "exe"
+  spec.bindir        = 'exe'
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
-  spec.require_paths = ["lib"]
+  spec.require_paths = ['lib']
 
-  spec.add_development_dependency "bundler", "~> 1.16"
-  spec.add_development_dependency "rake", "~> 10.0"
-  spec.add_development_dependency "minitest", "~> 5.0"
-  spec.add_development_dependency "vcr", "~> 4.0"
-  spec.add_development_dependency "webmock", "~> 3.4"
+  spec.add_development_dependency 'bundler', '~> 1.16'
+  spec.add_development_dependency 'minitest', '~> 5.0'
+  spec.add_development_dependency 'rake', '~> 10.0'
+  spec.add_development_dependency 'rubocop', '~> 0.55'
+  spec.add_development_dependency 'vcr', '~> 4.0'
+  spec.add_development_dependency 'webmock', '~> 3.4'
 end
Rakefile
@@ -1,10 +1,13 @@
-require "bundler/gem_tasks"
-require "rake/testtask"
+require 'bundler/gem_tasks'
+require 'rake/testtask'
+require 'rubocop/rake_task'
 
 Rake::TestTask.new(:test) do |t|
-  t.libs << "test"
-  t.libs << "lib"
-  t.test_files = FileList["test/**/*_test.rb"]
+  t.libs << 'test'
+  t.libs << 'lib'
+  t.test_files = FileList['test/**/*_test.rb']
 end
+RuboCop::RakeTask.new(:rubocop)
 
-task :default => :test
+task lint: [:rubocop]
+task default: :test