Skip to content

Commit

Permalink
replaced all instances of #find_or_create_company
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sussman committed Jun 25, 2024
1 parent 6f7bfdd commit 02ca4d5
Show file tree
Hide file tree
Showing 14 changed files with 2,729 additions and 133 deletions.
2 changes: 1 addition & 1 deletion app/jobs/url/create_company_from_url_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CreateCompanyFromUrlJob < ApplicationJob
retry_on StandardError, attempts: 0

def perform(url)
Importer::URL::CreateCompanyFromUrl.new(url).create_company
Importer::Url::CreateCompanyFromUrl.new(url).create_company
end
end
end
35 changes: 2 additions & 33 deletions app/models/concerns/ats/company_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,16 @@ module CompanyCreator
# TODO: Refactor this to simplify down
# TODO: Refactor into a service object according to the Single Responsibility Principle

def find_or_create_company_by_data(data)
p "Finding or creating company by data"
ats_identifier = fetch_company_id(data)
find_or_create_company(ats_identifier, data)
rescue StandardError => e
Rails.logger.error "Error creating company: #{e.message}"
nil
end

def find_or_create_company(ats_identifier, data = nil)
p "Finding or creating company with ATS identifier #{ats_identifier}"
return unless ats_identifier

company = Company.find_or_initialize_by(ats_identifier:) do |new_company|
company_data = company_details(ats_identifier)

new_company.applicant_tracking_system = self
new_company.assign_attributes(company_data)
end

if data
p "Supplementary data found"
supplementary_data = company_details_from_data(data)
company.assign_attributes(supplementary_data)
end

Rails.logger.info "Company created - #{company.name}" if company.new_record? && company.save

return company
end

def company_details(ats_identifier, data = nil)
refer_to_module(defined?(super) ? super : nil, __method__)
end

private

def company_details_from_data(data)
refer_to_module(defined?(super) ? super : nil, __method__)
end

private

def fetch_company_id(data)
refer_to_module(defined?(super) ? super : nil, __method__)
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/importer/api/job_postings_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JobPostingsApi

def initialize(ats_name)
@ats = ApplicantTrackingSystem.find_by(name: ats_name)
@local_storage = LocalDataStorage.new(ats_name)
@local_storage = LocalDataStorer.new(ats_name)
set_initial_counts
end

Expand All @@ -23,6 +23,8 @@ def call(url)
process
end

private

def processable
@ats.present?
end
Expand All @@ -39,8 +41,6 @@ def process
log_final_counts
end

private

def set_initial_counts
@initial_companies = Company.count
@initial_jobs = Job.count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Class responsible for storing API JSON data locally to prevent the need for multiple API calls
# Works by saving JSON data to a file and fetching it when needed
class LocalDataStorage
class LocalDataStorer
attr_reader :local_storage_path

def initialize(source)
Expand Down
2 changes: 1 addition & 1 deletion app/services/updater/existing_company_jobs_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fetch_jobs_and_companies
puts "Looking at jobs with #{ats_identifier}..."

# Find or create the company
unless (company = @ats.find_or_create_company(ats_identifier))
unless (company = CompanyCreator.call(ats: @ats, ats_identifier:))
puts "Problem with #{ats_identifier}"
add_to_invalid_ids(ats_name, ats_identifier)
next
Expand Down
2 changes: 1 addition & 1 deletion app/services/url/create_company_from_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_company
# CompanyCreator
# ---------------

company = ats.find_or_create_company(ats_identifier)
company = CompanyCreator.call(ats:, ats_identifier:)
if company&.persisted?
puts "Created company - #{company.name}"
else
Expand Down
18 changes: 8 additions & 10 deletions app/tasks/company_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,10 @@ def processable

def process
create_company
assign_attributes_from_supplementary_data
log_and_save_new_company
update_apply_with_cheddar
end

def assign_attributes_from_supplementary_data
return unless @data

p "Supplementary data found"
supplementary_data = @ats.company_details_from_data(@data)
@company.assign_attributes(supplementary_data)
end

def create_company
@company = Company.find_or_initialize_by(ats_identifier: @ats_identifier) do |new_company|
new_company.assign_attributes(company_params)
Expand All @@ -43,13 +34,20 @@ def create_company

def company_params
params = @ats.company_details(@ats_identifier)
params.merge(applicant_tracking_system: @ats)
params.merge(supplementary_attributes_from_data)
.merge(applicant_tracking_system: @ats)
end

def log_and_save_new_company
Rails.logger.info "Company created - #{@company.name}" if @company.new_record? && @company.save
end

def supplementary_attributes_from_data
return {} unless @data

@ats.company_details_from_data(@data)
end

def update_apply_with_cheddar
@company.update(apply_with_cheddar: @apply_with_cheddar) if @apply_with_cheddar
@company
Expand Down
2 changes: 1 addition & 1 deletion app/tasks/devit_job_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def process

def process_as_devit_job
ats = ApplicantTrackingSystem.find_by(name: 'DevITJobs')
company = CompanyCreator.new(ats:, data: @job_data, apply_with_cheddar: false).call
company = CompanyCreator.call(ats:, data: @job_data, apply_with_cheddar: false)
p "Company: #{company&.name}"
job = ats.find_or_create_job_by_data(company, @job_data)
p "Job: #{job&.title}"
Expand Down
2 changes: 1 addition & 1 deletion lib/builders/company_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def build
next
end

company = ats.find_or_create_company(ats_identifier)
company = CompanyCreator.call(ats:, ats_identifier:)

if company
puts "Created Company - #{company.name}"
Expand Down
38 changes: 19 additions & 19 deletions spec/models/applicant_tracking_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,72 +80,72 @@
COMPANIES.each_key do |ats_name|
puts "Trying #{ats_name}"
ats = ApplicantTrackingSystem.find_by(name: ats_name)
company = ats.find_or_create_company('zzzzz')
company = CompanyCreator.call(ats:, ats_identifier: 'zzzzz')
expect(company.persisted?).to be_falsey
end
end
end

it 'can create a company with AshbyHQ' do
VCR.use_cassette('create_company_ashbyhq') do
@ashbyhq.find_or_create_company('lightdash')
CompanyCreator.call(ats: @ashbyhq, ats_identifier: 'lightdash')
expect(Company.last.name).to eq('Lightdash')
end
end

it 'can create a company with BambooHR' do
VCR.use_cassette('create_company_bamboohr') do
@bamboohr.find_or_create_company('avidbots')
CompanyCreator.call(ats: @bamboohr, ats_identifier: 'avidbots')
expect(Company.last.name).to eq('Avidbots')
end
end

it 'can create a company with Greenhouse' do
VCR.use_cassette('create_company_greenhouse') do
@gh.find_or_create_company('codepath')
CompanyCreator.call(ats: @gh, ats_identifier: 'codepath')
expect(Company.last.name).to eq('CodePath')
end
end

it 'can create a company with Lever' do
VCR.use_cassette('create_company_lever') do
@lever.find_or_create_company('GoToGroup')
CompanyCreator.call(ats: @lever, ats_identifier: 'GoToGroup')
expect(Company.last.name).to eq('GoTo Group')
end
end

it 'can create a company with Manatal' do
VCR.use_cassette('create_company_manatal') do
@manatal.find_or_create_company('ptc-group')
CompanyCreator.call(ats: @manatal, ats_identifier: 'ptc-group')
expect(Company.last.name).to eq('PTC Group')
end
end

it 'can create a company with PinpointHQ' do
VCR.use_cassette('create_company_pinpointhq') do
@pinpointhq.find_or_create_company('bathspa')
CompanyCreator.call(ats: @pinpointhq, ats_identifier: 'bathspa')
expect(Company.last.name).to eq('Bath Spa University')
end
end

it 'can create a company with Recruitee' do
VCR.use_cassette('create_company_recruitee') do
@recruitee.find_or_create_company(RECRUITEE_COMPANY.first)
CompanyCreator.call(ats: @recruitee, ats_identifier: RECRUITEE_COMPANY.first)
expect(Company.last.name).to eq(RECRUITEE_COMPANY.second)
end
end

it 'can create a company with SmartRecruiters' do
VCR.use_cassette('create_company_smartrecruiters') do
@smartrecruiters.find_or_create_company('Gousto1')
CompanyCreator.call(ats: @smartrecruiters, ats_identifier: 'Gousto1')
expect(Company.last.name).to eq('Gousto')
end
end

# This test will route through proxy when API rate limit is reached
it 'can create a company with Workable' do
VCR.use_cassette('create_company_workable') do
@workable.find_or_create_company('kroo')
CompanyCreator.call(ats: @workable, ats_identifier: 'kroo')
expect(Company.last.name).to eq('Kroo Bank Ltd')
end
end
Expand All @@ -165,7 +165,7 @@
feed = get_json_data(url)
title = feed.dig('jobs', 0, 'title')
job_id = feed.dig('jobs', 0, 'id')
company = @ashbyhq.find_or_create_company('lightdash')
company = CompanyCreator.call(ats: @ashbyhq, ats_identifier: 'lightdash')
job = @ashbyhq.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -177,7 +177,7 @@
feed = get_json_data(url)
title = feed.dig('result', 0, 'jobOpeningName')
job_id = feed.dig('result', 0, 'id')
company = @bamboohr.find_or_create_company('premise')
company = CompanyCreator.call(ats: @bamboohr, ats_identifier: 'premise')
job = @bamboohr.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -189,7 +189,7 @@
feed = get_json_data(url)
title = feed.dig('jobs', 0, 'title')
job_id = feed.dig('jobs', 0, 'id')
company = @gh.find_or_create_company('codepath')
company = CompanyCreator.call(ats: @gh, ats_identifier: 'codepath')
job = @gh.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -201,7 +201,7 @@
feed = get_json_data(url)
title = feed.dig(0, 'text')
job_id = feed.dig(0, 'id')
company = @lever.find_or_create_company('GoToGroup')
company = CompanyCreator.call(ats: @lever, ats_identifier: 'GoToGroup')
job = @lever.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -213,7 +213,7 @@
feed = get_json_data(url)
title = feed.dig('results', 0, 'position_name')
job_id = feed.dig('results', 0, 'hash')
company = @manatal.find_or_create_company('ptc-group')
company = CompanyCreator.call(ats: @manatal, ats_identifier: 'ptc-group')
job = @manatal.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -225,7 +225,7 @@
feed = get_json_data(url)
title = feed.dig('data', 0, 'title')
job_id = feed.dig('data', 0, 'path').sub('/en/postings/', '')
company = @pinpointhq.find_or_create_company('bathspa')
company = CompanyCreator.call(ats: @pinpointhq, ats_identifier: 'bathspa')
job = @pinpointhq.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -237,7 +237,7 @@
feed = get_json_data(url)
title = feed.dig('offers', 0, 'title')
job_id = feed.dig('offers', 0, 'slug')
company = @recruitee.find_or_create_company(RECRUITEE_COMPANY.first)
company = CompanyCreator.call(ats: @recruitee, ats_identifier: RECRUITEE_COMPANY.first)
job = @recruitee.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -249,7 +249,7 @@
feed = get_json_data(url)
title = feed.dig('content', 0, 'name')
job_id = feed.dig('content', 0, 'id')
company = @smartrecruiters.find_or_create_company('Gousto1')
company = CompanyCreator.call(ats: @smartrecruiters, ats_identifier: 'Gousto1')
job = @smartrecruiters.find_or_create_job(company, job_id)
expect(job.title).to eq(title)
end
Expand All @@ -261,7 +261,7 @@
feed = get_json_data(url)
title = feed.dig('jobs', 0, 'title')
job_id = feed.dig('jobs', 0, 'application_url').match(%r{https://apply\.workable\.com/j/(\w+)/apply})[1]
company = @workable.find_or_create_company('southern-national')
company = CompanyCreator.call(ats: @workable, ats_identifier: 'southern-national')
job = @workable.find_or_create_job(company, job_id)
p company
p job
Expand Down
6 changes: 3 additions & 3 deletions spec/models/company_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
it "can create all relevant jobs with #{ats_name}", :vcr do
VCR.use_cassette("create_all_relevant_jobs_#{ats_name}") do
ats = ApplicantTrackingSystem.find_by(name: ats_name)
company = ats.find_or_create_company(ats_id)
unless company.persisted?
company = CompanyCreator.call(ats:, ats_identifier: ats_id)
unless company.persisted? # think this block is just for DevITJobs?
data = ats.fetch_company_jobs(ats_id)&.first
company = ats.find_or_create_company_by_data(data)
company = CompanyCreator.call(ats:, data:)
end

output = StringIO.new
Expand Down
Loading

0 comments on commit 02ca4d5

Please sign in to comment.