diff --git a/.rubocop.yml b/.rubocop.yml index 7335f04..7cf7490 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,7 +6,7 @@ AllCops: SuggestExtensions: false TargetRubyVersion: 2.6 -GemSpec/RequireMFA: +Gemspec/RequireMFA: Enabled: false Layout/LineLength: diff --git a/Gemfile.lock b/Gemfile.lock index 12edb7d..fe64c21 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,6 +87,7 @@ GEM PLATFORMS arm64-darwin-22 + arm64-darwin-25 x86_64-linux DEPENDENCIES diff --git a/lib/logsnag.rb b/lib/logsnag.rb index 870a2ed..001a670 100644 --- a/lib/logsnag.rb +++ b/lib/logsnag.rb @@ -117,6 +117,8 @@ def mutate_insight(data = {}) # @param body [LogSnag::Log,LogSnag::Identify,LogSnag::Insight] The body of the request. # @return [LogSnag::Result] The formatted result from the request. def execute_post(path, body) + return Result.new(success: true, data: nil, status_code: nil, error_message: nil) unless config.enabled + response = post( path, body: body.to_json, @@ -133,6 +135,8 @@ def execute_post(path, body) # @param body [LogSnag::Insight] The body of the request. # @return [LogSnag::Result] The formatted result from the request. def execute_patch(path, body) + return Result.new(success: true, data: nil, status_code: nil, error_message: nil) unless config.enabled + response = patch( path, body: { diff --git a/lib/logsnag/configuration.rb b/lib/logsnag/configuration.rb index 40831c4..4e56491 100644 --- a/lib/logsnag/configuration.rb +++ b/lib/logsnag/configuration.rb @@ -5,6 +5,14 @@ module LogSnag class Configuration attr_accessor :logger attr_reader :api_token, :project + attr_writer :enabled + + def enabled + return @enabled unless @enabled.nil? + return false if ENV["LOGSNAG_ENABLED"]&.downcase == "false" + + true + end def api_token=(token) raise ArgumentError, "API token cannot be nil" if token.nil? || token.empty? diff --git a/spec/log_snag/configuration_spec.rb b/spec/log_snag/configuration_spec.rb index 0d4777b..2217381 100644 --- a/spec/log_snag/configuration_spec.rb +++ b/spec/log_snag/configuration_spec.rb @@ -55,4 +55,56 @@ expect(configuration.logger).to eq(logger) end end + + describe "#enabled" do + it "defaults to true" do + expect(configuration.enabled).to be(true) + end + + it "allows setting to false" do + configuration.enabled = false + expect(configuration.enabled).to be(false) + end + + it "allows setting to true" do + configuration.enabled = false + configuration.enabled = true + expect(configuration.enabled).to be(true) + end + + context "when LOGSNAG_ENABLED env var is set to 'false'" do + before do + allow(ENV).to receive(:[]).with("LOGSNAG_ENABLED").and_return("false") + end + + it "reads from the environment variable" do + expect(configuration.enabled).to be(false) + end + + it "allows explicit config to override env var" do + configuration.enabled = true + expect(configuration.enabled).to be(true) + end + end + + context "when LOGSNAG_ENABLED env var is set to 'true'" do + before do + allow(ENV).to receive(:[]).with("LOGSNAG_ENABLED").and_return("true") + end + + it "reads from the environment variable" do + expect(configuration.enabled).to be(true) + end + end + + context "when LOGSNAG_ENABLED env var is not set" do + before do + allow(ENV).to receive(:[]).with("LOGSNAG_ENABLED").and_return(nil) + end + + it "defaults to true" do + expect(configuration.enabled).to be(true) + end + end + end end diff --git a/spec/log_snag_spec.rb b/spec/log_snag_spec.rb index 6883f44..a8583ad 100644 --- a/spec/log_snag_spec.rb +++ b/spec/log_snag_spec.rb @@ -512,4 +512,101 @@ end end end + + describe "when disabled" do + let(:data) do + { + channel: "test-channel", + event: "test-event" + } + end + + before do + described_class.configure do |config| + config.api_token = "123456" + config.project = "test-project" + config.logger = Logger.new($stdout) + config.enabled = false + end + end + + after do + described_class.configure do |config| + config.enabled = true + end + end + + describe ".log" do + it "does not make an HTTP request and returns a successful result" do + result = described_class.log(data) + + expect(a_request(:post, "https://api.logsnag.com/v1/log")).not_to have_been_made + expect(result).to be_a(LogSnag::Result) + expect(result.success).to be(true) + expect(result.data).to be_nil + expect(result.error_message).to be_nil + expect(result.status_code).to be_nil + end + end + + describe ".identify" do + let(:data) do + { + user_id: "test-user-id", + properties: { email: "test-user@example.com" } + } + end + + it "does not make an HTTP request and returns a successful result" do + result = described_class.identify(data) + + expect(a_request(:post, "https://api.logsnag.com/v1/identify")).not_to have_been_made + expect(result).to be_a(LogSnag::Result) + expect(result.success).to be(true) + expect(result.data).to be_nil + expect(result.error_message).to be_nil + expect(result.status_code).to be_nil + end + end + + describe ".insight" do + let(:data) do + { + title: "test-title", + value: 123 + } + end + + it "does not make an HTTP request and returns a successful result" do + result = described_class.insight(data) + + expect(a_request(:post, "https://api.logsnag.com/v1/insight")).not_to have_been_made + expect(result).to be_a(LogSnag::Result) + expect(result.success).to be(true) + expect(result.data).to be_nil + expect(result.error_message).to be_nil + expect(result.status_code).to be_nil + end + end + + describe ".mutate_insight" do + let(:data) do + { + title: "test-title", + value: 1 + } + end + + it "does not make an HTTP request and returns a successful result" do + result = described_class.mutate_insight(data) + + expect(a_request(:patch, "https://api.logsnag.com/v1/insight")).not_to have_been_made + expect(result).to be_a(LogSnag::Result) + expect(result.success).to be(true) + expect(result.data).to be_nil + expect(result.error_message).to be_nil + expect(result.status_code).to be_nil + end + end + end end