Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AllCops:
SuggestExtensions: false
TargetRubyVersion: 2.6

GemSpec/RequireMFA:
Gemspec/RequireMFA:
Enabled: false

Layout/LineLength:
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ GEM

PLATFORMS
arm64-darwin-22
arm64-darwin-25
x86_64-linux

DEPENDENCIES
Expand Down
4 changes: 4 additions & 0 deletions lib/logsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions lib/logsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
52 changes: 52 additions & 0 deletions spec/log_snag/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
97 changes: 97 additions & 0 deletions spec/log_snag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading