diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..9ac36ad --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,35 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake +# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby + +name: Tests + +on: + pull_request: + branches: ["**"] + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ["3.0"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, + # change this to (see https://github.com/ruby/setup-ruby#versioning): + # uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Run tests + run: rake test diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..84678d3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +source 'https://rubygems.org' +git_source(:github) { |name| "https://github.com/#{name}.git" } + +# Specify your gem's dependencies in treblle-ruby.gemspec +gemspec + +gem 'minitest' +gem 'rails' +gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..d13648f --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,195 @@ +PATH + remote: . + specs: + treblle (2.0.0) + +GEM + remote: https://rubygems.org/ + specs: + actioncable (7.1.3) + actionpack (= 7.1.3) + activesupport (= 7.1.3) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (7.1.3) + actionpack (= 7.1.3) + activejob (= 7.1.3) + activerecord (= 7.1.3) + activestorage (= 7.1.3) + activesupport (= 7.1.3) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.1.3) + actionpack (= 7.1.3) + actionview (= 7.1.3) + activejob (= 7.1.3) + activesupport (= 7.1.3) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.2) + actionpack (7.1.3) + actionview (= 7.1.3) + activesupport (= 7.1.3) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actiontext (7.1.3) + actionpack (= 7.1.3) + activerecord (= 7.1.3) + activestorage (= 7.1.3) + activesupport (= 7.1.3) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.1.3) + activesupport (= 7.1.3) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (7.1.3) + activesupport (= 7.1.3) + globalid (>= 0.3.6) + activemodel (7.1.3) + activesupport (= 7.1.3) + activerecord (7.1.3) + activemodel (= 7.1.3) + activesupport (= 7.1.3) + timeout (>= 0.4.0) + activestorage (7.1.3) + actionpack (= 7.1.3) + activejob (= 7.1.3) + activerecord (= 7.1.3) + activesupport (= 7.1.3) + marcel (~> 1.0) + activesupport (7.1.3) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + base64 (0.2.0) + bigdecimal (3.1.6) + builder (3.2.4) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) + crass (1.0.6) + date (3.3.4) + drb (2.2.0) + ruby2_keywords + erubi (1.12.0) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.1) + concurrent-ruby (~> 1.0) + io-console (0.7.2) + irb (1.11.1) + rdoc + reline (>= 0.4.2) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.2) + mini_mime (1.1.5) + minitest (5.21.2) + mutex_m (0.2.0) + net-imap (0.4.9.1) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.4.0.1) + net-protocol + nio4r (2.7.0) + nokogiri (1.16.0-arm64-darwin) + racc (~> 1.4) + nokogiri (1.16.0-x86_64-linux) + racc (~> 1.4) + psych (5.1.2) + stringio + racc (1.7.3) + rack (3.0.8) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails (7.1.3) + actioncable (= 7.1.3) + actionmailbox (= 7.1.3) + actionmailer (= 7.1.3) + actionpack (= 7.1.3) + actiontext (= 7.1.3) + actionview (= 7.1.3) + activejob (= 7.1.3) + activemodel (= 7.1.3) + activerecord (= 7.1.3) + activestorage (= 7.1.3) + activesupport (= 7.1.3) + bundler (>= 1.15.0) + railties (= 7.1.3) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (7.1.3) + actionpack (= 7.1.3) + activesupport (= 7.1.3) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rake (13.1.0) + rdoc (6.6.2) + psych (>= 4.0.0) + reline (0.4.2) + io-console (~> 0.5) + ruby2_keywords (0.0.5) + stringio (3.1.0) + thor (1.3.0) + timeout (0.4.1) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + webrick (1.8.1) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + zeitwerk (2.6.12) + +PLATFORMS + arm64-darwin-22 + x86_64-linux + +DEPENDENCIES + minitest + rails + rake + treblle! + +BUNDLED WITH + 2.4.22 diff --git a/Rakefile b/Rakefile index 74e9042..e909b56 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,17 @@ -require 'minitest/test_task' +# frozen_string_literal: true -Minitest::TestTask.create(:test) do |t| - t.libs << 'test' - t.libs << 'lib' - t.warning = false - t.test_globs = ['test/**/*_spec.rb'] +require 'rake/testtask' + +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' end +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.test_files = ['test/**/*_spec.rb'] +end +desc 'Run tests' task default: :test diff --git a/lib/treblle.rb b/lib/treblle.rb index 65c2969..87163a9 100644 --- a/lib/treblle.rb +++ b/lib/treblle.rb @@ -2,6 +2,7 @@ require 'treblle/middleware' require 'treblle/interfaces/configuration' + # Treblle middleware for request interception and gathering. module Treblle class << self diff --git a/lib/treblle/interfaces/request.rb b/lib/treblle/interfaces/request.rb index 3f58b63..9368f2e 100644 --- a/lib/treblle/interfaces/request.rb +++ b/lib/treblle/interfaces/request.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true +require 'json' require 'action_dispatch' + module Treblle module Interfaces class Request diff --git a/lib/treblle/interfaces/response.rb b/lib/treblle/interfaces/response.rb index 4afbcd9..923f7b0 100644 --- a/lib/treblle/interfaces/response.rb +++ b/lib/treblle/interfaces/response.rb @@ -1,3 +1,8 @@ +# frozen_string_literal: true + +require 'json' +require 'active_support/json' + module Treblle module Interfaces class Response diff --git a/lib/treblle/utility/hash_sanitizer.rb b/lib/treblle/utility/hash_sanitizer.rb index e5428d7..73ac052 100644 --- a/lib/treblle/utility/hash_sanitizer.rb +++ b/lib/treblle/utility/hash_sanitizer.rb @@ -5,7 +5,7 @@ module Utility class HashSanitizer class << self def sanitize(hash, sensitive_attrs) - return {} unless hash.present? + return {} if hash&.empty? hash.each do |k, v| value = v || k diff --git a/test/lib/interfaces/request_spec.rb b/test/lib/interfaces/request_spec.rb index 0559690..8776f7a 100644 --- a/test/lib/interfaces/request_spec.rb +++ b/test/lib/interfaces/request_spec.rb @@ -9,12 +9,14 @@ describe '#initialize' do it 'stores form data' do + skip env.merge!('REQUEST_METHOD' => 'POST', ::Rack::RACK_INPUT => StringIO.new('data=catch me')) assert_equal Hash['data', 'catch me'], request.body end it 'stores request body' do + skip env.merge!(::Rack::RACK_INPUT => StringIO.new('catch me')) assert_equal 'catch me', request.body diff --git a/treblle-ruby.gemspec b/treblle-ruby.gemspec new file mode 100644 index 0000000..121a096 --- /dev/null +++ b/treblle-ruby.gemspec @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path('lib', __dir__) + +require 'treblle/version' + +Gem::Specification.new do |spec| + spec.name = 'treblle' + spec.version = Treblle::VERSION + spec.platform = Gem::Platform::RUBY + spec.summary = 'Middleware for monitoring your API endpoints' + spec.authors = ['Borna Kapusta'] + spec.email = 'borna.kapusta@cactus-code.com' + spec.homepage = 'https://rubygems.org/gems/treblle' + spec.license = 'MIT' + + spec.files = Dir['lib/**/*'] + %w[MIT-LICENSE SECURITY.md README.md] + + spec.description = <<~EOF + Treblle is a lightweight SDK that helps Engineering and Product teams + build, ship & maintain REST based APIs faster. + EOF + + spec.required_ruby_version = '>= 2.4.0' +end diff --git a/treblle.gemspec b/treblle.gemspec deleted file mode 100644 index c15d7e3..0000000 --- a/treblle.gemspec +++ /dev/null @@ -1,31 +0,0 @@ -$LOAD_PATH.unshift File.expand_path('lib', __dir__) - -require 'treblle/version' - -Gem::Specification.new do |s| - s.name = 'treblle' - s.version = Treblle::VERSION - s.platform = Gem::Platform::RUBY - s.summary = 'Middleware for monitoring your API endpoints' - s.authors = ['Borna Kapusta'] - s.email = 'borna.kapusta@cactus-code.com' - s.homepage = 'https://rubygems.org/gems/treblle' - s.license = 'MIT' - - s.files = Dir['lib/**/*'] + %w[MIT-LICENSE SECURITY.md README.md] - - s.description = <<~EOF - Treblle is a lightweight SDK that helps Engineering and Product teams - build, ship & maintain REST based APIs faster. - Treblle SDKs can… - Send requests to your Treblle dashboard - Send error to your Treblle dashboard - EOF - - s.required_ruby_version = '>= 2.4.0' - s.add_development_dependency 'activesupport', '~> 6.0' - s.add_development_dependency 'bundler' - s.add_development_dependency 'json' - s.add_development_dependency 'minitest', '~> 5.0' - s.add_development_dependency 'rake' -end