Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract endpoints into config file #27

Closed
Show file tree
Hide file tree
Changes from 6 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 lib/cronitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.job(key, &block)
end

def self.monitor_api_url
'https://cronitor.io/api/monitors'
"#{Cronitor.monitor_url}/api/monitors"
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/cronitor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Cronitor
YAML_KEYS = MONITOR_TYPES.map { |t| "#{t}s" }

class << self
attr_accessor :api_key, :api_version, :environment, :logger, :config, :timeout, :ping_timeout, :auto_discover_sidekiq
attr_accessor :api_key, :api_version, :environment, :logger, :config, :timeout, :ping_timeout, :auto_discover_sidekiq, :ping_url, :monitor_url

def configure(&block)
block.call(self)
Expand All @@ -23,6 +23,8 @@ def configure(&block)
self.ping_timeout = ENV.fetch('CRONITOR_PING_TIMEOUT', nil) || 5
self.config = ENV.fetch('CRONITOR_CONFIG', nil)
self.auto_discover_sidekiq = ENV.fetch('CRONITOR_AUTO_DISCOVER_SIDEKIQ', 'true').casecmp('true').zero? # https://github.com/cronitorio/cronitor-sidekiq
self.ping_url = ENV.fetch('CRONITOR_PING_URL', 'https://cronitor.link')
self.monitor_url = ENV.fetch('CRONITOR_MONITOR_URL', 'https://cronitor.io')
self.logger = Logger.new($stdout)
logger.level = Logger::INFO
end
2 changes: 1 addition & 1 deletion lib/cronitor/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def unpause
end

def ping_api_url
"https://cronitor.link/p/#{api_key}/#{key}"
"#{Cronitor.ping_url}/p/#{api_key}/#{key}"
end

def fallback_ping_api_url
Expand Down
34 changes: 34 additions & 0 deletions spec/cronitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@
end
end

describe '#ping_url and #monitor_url' do
let(:env_vars) { ENV }

before do
stub_const('ENV', env_vars)
# We need to reset the ping_url and monitor_url since the previous spec sets them globally
Cronitor.configure do |cronitor|
cronitor.ping_url = cronitor.default_ping_url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willzoltan thank you for these! I think the specs need to be updated to not reference default_XXX since those aren't defined on the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I forgot to commit all my changes... Fixed!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed again. Sorry I don't have the test suite set up on my computer

cronitor.monitor_url = cronitor.default_monitor_url
end
end

context 'when custom CRONITOR_PING_URL and CRONITOR_MONITOR_URL ENV variables are set' do
let(:env_vars) do
ENV.to_hash.merge(
'CRONITOR_PING_URL' => 'https://ping.com',
'CRONITOR_MONITOR_URL' => 'https://monitor.com'
)
end

it 'returns the custom Cronitor URLs' do
expect(Cronitor.ping_url).to eq('https://ping.com')
expect(Cronitor.monitor_url).to eq('https://monitor.com')
end
end

context 'when no CRONITOR_PING_URL or CRONITOR_MONITOR_URL ENV variables are set' do
it 'returns the default Cronitor URLs' do
expect(Cronitor.ping_url).to eq('https://cronitor.link')
expect(Cronitor.monitor_url).to eq('https://cronitor.io')
end
end
end

describe 'YAML configuration' do
before(:all) do
Cronitor.configure do |cronitor|
Expand Down
Loading