Skip to content
Open
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
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.0)
backports (3.17.0)
backports (3.17.1)
byebug (10.0.2)
coderay (1.1.2)
concurrent-ruby (1.1.6)
Expand All @@ -58,9 +58,9 @@ GEM
nokogiri (1.9.1)
mini_portile2 (~> 2.4.0)
parallel (1.19.1)
parser (2.7.1.0)
parser (2.7.1.1)
ast (~> 2.4.0)
pry (0.13.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (3.1.1)
Expand Down
18 changes: 11 additions & 7 deletions lib/openc_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@ def statsd_namespace
StatsD.server = "sys1:8125"
StatsD.logger = Logger.new("/dev/null") if bot_env != :production

if respond_to?(:inferred_jurisdiction_code) && inferred_jurisdiction_code
"fetcher_bot.#{bot_env}.#{inferred_jurisdiction_code}"
elsif is_a?(Module)
"fetcher_bot.#{bot_env}.#{name.downcase}"
else
"fetcher_bot.#{bot_env}.#{self.class.name.downcase}"
end
"fetcher_bot.#{bot_env}.#{jurisdiction_code}"
.sub("companiesfetcher", "")
end
end

def jurisdiction_code
if respond_to?(:inferred_jurisdiction_code) && inferred_jurisdiction_code
inferred_jurisdiction_code
elsif is_a?(Module)
name.downcase
else
self.class.name.downcase
end
end

def db_name
if is_a?(Module)
"#{name.downcase}.db"
Expand Down
2 changes: 2 additions & 0 deletions lib/openc_bot/company_fetcher_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
require "openc_bot/helpers/incremental_search"
require "openc_bot/helpers/alpha_search"
require "openc_bot/helpers/reporting"
require "openc_bot/config"

module OpencBot
module CompanyFetcherBot
include OpencBot
include OpencBot::Helpers::IncrementalSearch
include OpencBot::Helpers::AlphaSearch
include OpencBot::Helpers::Reporting
include OpencBot::Config

STDOUT.sync = true
STDERR.sync = true
Expand Down
28 changes: 28 additions & 0 deletions lib/openc_bot/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "json"
require "net/http"
require "ostruct"

module OpencBot
# This is called from company_fetcher_bot
module Config
BOTS_JSON_URL = ENV["ANALYSIS_BOTS_JSON_URL"]

def set_variables
return if @db_config

config?.marshal_dump.each { |k, v| instance_variable_set("@#{k}", v) } unless config?.nil?
end

def db_config
return if BOTS_JSON_URL.nil?

@db_config ||= JSON.parse(Net::HTTP.get(URI(BOTS_JSON_URL))).select { |bot| bot["bot_id"] == "#{jurisdiction_code}_companies" }.first
end

def config?
OpenStruct.new(db_config["bot_config"])
rescue NoMethodError
nil
end
end
end
62 changes: 62 additions & 0 deletions spec/lib/openc_bot/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "spec_helper"
require "openc_bot"
require "openc_bot/company_fetcher_bot"
require "openc_bot/config"

module ModuleThatIncludesConfig
extend OpencBot
extend OpencBot::CompanyFetcherBot
extend OpencBot::Config
end

describe OpencBot::Config do
let(:expected_output) { OpenStruct.new(bot_config: { foo: "bar" }) }

describe "#set_variables" do
before do
allow(ModuleThatIncludesConfig).to receive(:config?).and_return(expected_output)
end

it "sends a request to config def" do
ModuleThatIncludesConfig.set_variables
expect(ModuleThatIncludesConfig).to have_received(:config?).twice
end
end

describe "#db_config" do
context "when BOTS_JSON_URL is not set" do
it "returns nil" do
expect(ModuleThatIncludesConfig.db_config).to eq(nil)
end
end

context "when BOTS_JSON_URL is set" do
before do
stub_const("OpencBot::Config::BOTS_JSON_URL", "someendpoint")
ModuleThatIncludesConfig.instance_variable_set(:@db_config, expected_output)
end

it "returns an object" do
expect(ModuleThatIncludesConfig.db_config).to eq(expected_output)
end
end
end

describe "#config?" do
context "when db_config returns an object" do
before do
allow(ModuleThatIncludesConfig).to receive(:db_config).and_return(expected_output)
end

it "returns the bot_config struct from the db_config object that is queryable" do
expect(ModuleThatIncludesConfig.config?.foo).to eq("bar")
end
end

context "when db_config does not return an object" do
it "returns nil" do
expect(ModuleThatIncludesConfig.config?).to eq(nil)
end
end
end
end