-
Notifications
You must be signed in to change notification settings - Fork 36
/
jobs.rb
75 lines (66 loc) · 2.75 KB
/
jobs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
module Jobs
SIMPLE_SEARCHES = '(job|employment|internship)s?'
JOB_RELATED_KEYWORDS = '((position|opening|posting|job|employment|career|' \
'intern(ship)?|seasonal|trabajo|puesto|empleo|' \
'carrera|vacante+?)(s\b|\b)|(opportunit|vacanc)' \
'(y|ies))|(posicion|ocupacion|oportunidad)' \
'(es)?'
SCRUB_KEYWORDS = JOB_RELATED_KEYWORDS.
remove(/\|intern\(ship\)|\|seasonal/)
SIMPLE_SINGULARS = %w[
statistic number level rate description trend growth
projection survey forecast figure report verification record
authorization card classification form hazard poster fair board
outlook grant funding factor other cut
application
].freeze
BLOCKED_PHRASES = '(job|employment) (contract|law|training|safety)s?'
BLOCKED_KEYWORDS =
'data|at will|equal|status|eligibility|analysis|300 log|delayed' \
'|(histor)(y|ies)' \
"|#{Date.current.year.to_s}" \
"|#{BLOCKED_PHRASES}" \
"|(#{SIMPLE_SINGULARS.join('|')})s?"
SEARCH_RADIUS = 75
def self.establish_connection!
@endpoint = ENV['USAJOBS_ENDPOINT'] || Rails.application.secrets.dig(:jobs, :endpoint)
host = ENV['USAJOBS_HOST'] || Rails.application.secrets.dig(:jobs, :host)
key = ENV['USAJOBS_AUTHORIZATION_KEY'] || Rails.application.secrets.dig(:jobs, :authorization_key)
user_agent = ENV['USAJOBS_USER_AGENT'] || Rails.application.secrets.dig(:jobs, :user_agent)
adapter = ENV['USAJOBS_ADAPTER'] || Rails.application.secrets.dig(:jobs, :adapter)
@usajobs_api_connection = Faraday.new(host) do |conn|
conn.headers['Authorization-Key'] = key
conn.headers['User-Agent'] = user_agent
conn.request(:json)
conn.response(:rashify)
conn.response(:json)
conn.use(:instrumentation)
conn.adapter(adapter || Faraday.default_adapter)
end
end
def self.scrub_query(query)
query.remove(/\b#{SCRUB_KEYWORDS}\b/i).squish
end
def self.search(job_options)
if query_eligible?(job_options[:query])
@usajobs_api_connection.get(@endpoint, params(job_options)).body
end
rescue => error
Rails.logger.error("Trouble fetching jobs information: #{error}")
nil
end
def self.query_eligible?(query)
((query =~ /\b#{JOB_RELATED_KEYWORDS}\b/i) && \
(query !~ /\b#{BLOCKED_KEYWORDS}\b/i) && \
(query !~ /["():]|^-| -\S+/))
end
def self.params(options)
{ Keyword: scrub_query(options[:query]),
Organization: options[:organization_codes],
LocationName: options[:location_name],
ResultsPerPage: options[:results_per_page],
Radius: SEARCH_RADIUS }
end
end
Jobs.establish_connection!