Skip to content

Commit

Permalink
Merge pull request #19 from telebugs/config
Browse files Browse the repository at this point in the history
Add Telebugs.config
  • Loading branch information
kyrylo committed Jun 15, 2024
2 parents 55db9d5 + 44db93a commit 5326d2e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/telebugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def configure
yield Config.instance
end

def config
Config.instance
end

def report(error)
Reporter.instance.report(error)
end
Expand Down
10 changes: 5 additions & 5 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

class TestConfig < Minitest::Test
def teardown
Telebugs::Config.instance.reset
Telebugs.config.reset
end

def test_api_key
Telebugs.configure { |c| c.api_key = "12345:abcdef" }

assert_equal "12345:abcdef", Telebugs::Config.instance.api_key
assert_equal "12345:abcdef", Telebugs.config.api_key
end

def test_error_api_url
Telebugs.configure { |c| c.api_url = "example.com" }

assert_equal URI("example.com"), Telebugs::Config.instance.api_url
assert_equal URI("example.com"), Telebugs.config.api_url
end

def test_root_directory
Telebugs.configure { |c| c.root_directory = "/tmp" }

assert_equal "/tmp", Telebugs::Config.instance.root_directory
assert_equal "/tmp", Telebugs.config.root_directory
end

def test_middleware
Expand All @@ -32,6 +32,6 @@ def test_middleware
c.middleware.use middleware_class.new
end

assert_equal 1, Telebugs::Config.instance.middleware.middlewares.size
assert_equal 1, Telebugs.config.middleware.middlewares.size
end
end
8 changes: 4 additions & 4 deletions test/test_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ def call(report)
class TestReporter < Minitest::Test
def teardown
WebMock.reset!
Telebugs::Config.instance.reset
Telebugs.config.reset
end

def test_report_returns_a_promise_that_resolves_to_a_hash
stub = stub_request(:post, Telebugs::Config.instance.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

p = Telebugs::Reporter.new.report(StandardError.new)
Expand All @@ -25,7 +25,7 @@ def test_report_returns_a_promise_that_resolves_to_a_hash
end

def test_reporter_returns_a_promise_that_rejects_on_http_error
stub = stub_request(:post, Telebugs::Config.instance.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 500)

p = Telebugs::Reporter.new.report(StandardError.new)
Expand All @@ -36,7 +36,7 @@ def test_reporter_returns_a_promise_that_rejects_on_http_error
end

def test_reporter_does_not_send_ignored_errors
stub = stub_request(:post, Telebugs::Config.instance.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

Telebugs.configure do |c|
Expand Down
6 changes: 3 additions & 3 deletions test/test_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class TestSender < Minitest::Test
def teardown
Telebugs::Config.instance.reset
Telebugs.config.reset
WebMock.reset!
end

def test_send_attaches_correct_authorization_headers
Telebugs.configure { |c| c.api_key = "12345:abcdef" }

stub = stub_request(:post, Telebugs::Config.instance.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

Telebugs::Sender.new.send({"errors" => []})
Expand All @@ -27,7 +27,7 @@ def test_send_attaches_correct_authorization_headers
end

def test_send_raises_http_error_when_response_code_is_not_created
stub = stub_request(:post, Telebugs::Config.instance.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 500, body: {"error" => "oops"}.to_json)

assert_raises(Telebugs::HTTPError) do
Expand Down
12 changes: 8 additions & 4 deletions test/test_telebugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestTelebugs < Minitest::Test
def teardown
Telebugs::Config.instance.reset
Telebugs.config.reset
WebMock.reset!
end

Expand All @@ -19,11 +19,11 @@ def test_configure_configures_project_key
config.api_key = key
end

assert_equal key, Telebugs::Config.instance.api_key
assert_equal key, Telebugs.config.api_key
end

def test_report_returns_a_fullfilled_promise_when_request_succeeds
stub_request(:post, Telebugs::Config.instance.api_url)
stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

p = Telebugs.report(StandardError.new)
Expand All @@ -33,11 +33,15 @@ def test_report_returns_a_fullfilled_promise_when_request_succeeds
end

def test_report_returns_a_rejected_promise_when_request_fails
stub_request(:post, Telebugs::Config.instance.api_url).to_return(status: 500)
stub_request(:post, Telebugs.config.api_url).to_return(status: 500)

p = Telebugs.report(StandardError.new)
p.wait

assert p.rejected?
end

def test_config_exposes_the_default_config
assert_match(/telebugs\.com/, Telebugs.config.api_url.to_s)
end
end

0 comments on commit 5326d2e

Please sign in to comment.