Skip to content

Feature/load config from file #22

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

Closed
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
1 change: 1 addition & 0 deletions lib/influxdb-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "influxdb/rails/logger"
require "influxdb/rails/exception_presenter"
require "influxdb/rails/configuration"
require "influxdb/rails/configuration_file_loader"
require "influxdb/rails/backtrace"
require "influxdb/rails/rack"

Expand Down
1 change: 1 addition & 0 deletions lib/influxdb/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def initialize
@debug = false
@rescue_global_exceptions = false
@instrumentation_enabled = true
ConfigurationFileLoader.set_configuration_values(self)
end

def debug?
Expand Down
33 changes: 33 additions & 0 deletions lib/influxdb/rails/configuration_file_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module InfluxDB
module Rails
class ConfigurationFileLoader
MAPPINGS = {
hosts: "influxdb_hosts", port: "influxdb_port",
username: "influxdb_username", password: "influxdb_password",
database: "influxdb_database", async: true, use_ssl: true,
series_name_for_db_runtimes: true, series_name_for_view_runtimes: true,
series_name_for_controller_runtimes: true, debug: true,
instrumentation_enabled: true
}.freeze

def self.set_configuration_values(object, file = "#{::Rails.root}/config/influxdb.yml")
return unless File.exists?(file)
configs = YAML.load_file(file)
return unless configs.present? && configs.has_key?(::Rails.env)
values = configs[::Rails.env]

MAPPINGS.each do |config_name, method_name|
if values.has_key? config_name.to_s
if method_name.is_a? TrueClass
normalised_method_name = config_name
else
normalised_method_name = method_name
end
object.send("#{normalised_method_name}=", values[config_name.to_s])
end
end
end
end
end
end

13 changes: 13 additions & 0 deletions spec/support/complete_config_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test:
hosts: [localhost, otherhost]
port: 8056
username: username
password: password
database: thedatabase
async: false
use_ssl: true
debug: false
instrumentation_enabled: false
series_name_for_db_runtimes: series_for_db
series_name_for_view_runtimes: series_for_view
series_name_for_controller_runtimes: series_for_controller
6 changes: 6 additions & 0 deletions spec/support/config_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test:
hosts: [localhost, otherhost]
port: 8056
username: username
password: password
database: thedatabase
71 changes: 71 additions & 0 deletions spec/unit/configuration_file_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'spec_helper'

describe InfluxDB::Rails::ConfigurationFileLoader do
before do
@configuration = InfluxDB::Rails::Configuration.new
end

describe "#set_configuration_values" do
subject { InfluxDB::Rails::ConfigurationFileLoader.set_configuration_values(@configuration, filename) }

context "No configuration file exists" do
let(:filename) { "junk.yml" }

specify "No Yaml should be loaded" do
expect(YAML).to_not receive(:load_file)
subject
end
end

context "configuration file with all directives" do
let(:filename) { "spec/support/complete_config_file.yml" }

specify "Yaml should be loaded" do
expect(YAML).to receive(:load_file).with(filename)
subject
end

specify "the configuration should be set" do
subject
expect(@configuration.influxdb_hosts).to eq(["localhost", "otherhost"])
expect(@configuration.influxdb_port).to eq(8056)
expect(@configuration.influxdb_username).to eq("username")
expect(@configuration.influxdb_password).to eq("password")
expect(@configuration.influxdb_database).to eq("thedatabase")
expect(@configuration.async).to eq(false)
expect(@configuration.use_ssl).to eq(true)
expect(@configuration.series_name_for_db_runtimes).to eq("series_for_db")
expect(@configuration.series_name_for_view_runtimes).to eq("series_for_view")
expect(@configuration.series_name_for_controller_runtimes).to eq("series_for_controller")
expect(@configuration.debug).to eq(false)
expect(@configuration.instrumentation_enabled).to eq(false)
end
end

context "configuration file with some directives" do
let(:filename) { "spec/support/config_file.yml" }

specify "Yaml should be loaded" do
expect(YAML).to receive(:load_file).with(filename)
subject
end

specify "the configuration should be set" do
defaults = InfluxDB::Rails::Configuration::DEFAULTS
subject
expect(@configuration.influxdb_hosts).to eq(["localhost", "otherhost"])
expect(@configuration.influxdb_port).to eq(8056)
expect(@configuration.influxdb_username).to eq("username")
expect(@configuration.influxdb_password).to eq("password")
expect(@configuration.influxdb_database).to eq("thedatabase")
expect(@configuration.async).to eq(defaults[:async])
expect(@configuration.use_ssl).to eq(defaults[:use_ssl])
expect(@configuration.series_name_for_db_runtimes).to eq(defaults[:series_name_for_db_runtimes])
expect(@configuration.series_name_for_view_runtimes).to eq(defaults[:series_name_for_view_runtimes])
expect(@configuration.series_name_for_controller_runtimes).to eq(defaults[:series_name_for_controller_runtimes])
expect(@configuration.debug).to eq(false)
expect(@configuration.instrumentation_enabled).to eq(true)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/unit/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
@configuration = InfluxDB::Rails::Configuration.new
end

describe "#initialize" do
it "should call the configuration file loader" do
expect(InfluxDB::Rails::ConfigurationFileLoader).to receive(:set_configuration_values) do |object|
expect(object.class).to eq(InfluxDB::Rails::Configuration)
end
InfluxDB::Rails::Configuration.new
end
end

describe "#ignore_user_agent?" do
it "should be true for user agents that have been set as ignorable" do
@configuration.ignored_user_agents = %w{Googlebot}
Expand Down