-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
390 lines (346 loc) · 9.04 KB
/
api.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require 'sequel'
require 'csv'
require 'ffaker'
require 'json'
require 'sinatra'
require "sinatra/namespace"
require 'similar_text'
require 'mongoid'
require 'presume'
require 'metainspector'
require 'dotenv'
require 'bigdecimal/math'
Dotenv.load
$match_counter = 0
Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))
Mongoid.load!('config/mongoid.yml')
# connect to an in-memory database
DB = Sequel.sqlite
# create an items table
DB.create_table :matches do
primary_key :id
String :email, null: false
Integer :job_id, null: false
Float :score, null: false
end
# create a dataset from the items table
matches = DB[:matches]
class Website
include Mongoid::Document
field :url, type: String
field :domain, type: String
field :title, type: String
field :description, type: String
validates :url, presence: true
end
class Job
include Mongoid::Document
field :email, type: String
field :title, type: String
field :company, type: String
field :phone, type: String
field :url, type: String
field :description, type: String
validates :email, presence: true
validates :title, presence: true
validates :description, presence: true
index({ email: 'text' })
index({ company: 'text' })
index({description:1 }, { name: "description_index" })
scope :email, -> (email) { where(email: /^#{email}/) }
scope :description, -> (description) { where(description: description) }
scope :title, -> (title) { where(title: title) }
scope :company, -> (company) { where(company: company) }
scope :url, -> (url) { where(url: url) }
end
class Candidate
include Mongoid::Document
field :email, type: String
field :name, type: String
field :resume, type: String
field :phone, type: String
validates :email, presence: true
validates :resume, presence: true
index({ email: 'text' }, { unique: true })
index({ phone: 'text' })
scope :email, -> (email) { where(email: /^#{email}/) }
scope :resume, -> (resume) { where(resume: resume) }
scope :name, -> (name) { where(name: name) }
scope :phone, -> (phone) { where(phone: phone) }
end
class JobSerializer
def initialize(job)
@job = job
end
def as_json(*)
data = {
id:@job.id.to_s,
title:@job.title,
company:@job.company,
email:@job.email,
description:@job.description
}
data[:errors] = @job.errors if@job.errors.any?
data
end
end
class CandidateSerializer
def initialize(candidate)
@candidate = candidate
end
def as_json(*)
data = {
id:@candidate.id.to_s,
name:@candidate.name,
email:@candidate.email,
phone:@candidate.phone,
resume:@candidate.resume
}
data[:errors] = @candidate.errors if@candidate.errors.any?
data
end
end
configure do
set :protection, :except => [:json_csrf]
# set :sessions, true
set :bind, '0.0.0.0'
set :logging, true
set :dump_errors, true
set :port, 4567
end
before do
content_type 'application/json'
headers['Access-Control-Allow-Origin'] = '*'
error 401 unless params[:key] == ENV['SECRET_API_KEY']
end
helpers do
def base_url
@base_url ||= "#{request.env['rack.url_scheme']}://{request.env['HTTP_HOST']}"
end
def json_params
begin
JSON.parse(request.body.read)
rescue
halt 400, { message:'Invalid JSON' }.to_json
end
end
def candidate
@candidate ||= Candidate.where(id: params[:id]).first
end
def pi(digits=20)
result = BigMath.PI(digits)
result = result.truncate(digits).to_s
result = result[2..-1]
result = result.split('e').first
result = result.insert(1, '.')
result
end
def job
@job ||= Job.where(id: params[:id]).first
end
def candidate_serialize(candidate)
CandidateSerializer.new(candidate).to_json
end
def job_serialize(job)
JobSerializer.new(job).to_json
end
end
get '/' do
"OK".to_json
end
namespace '/api/v1' do
get '/pi/:digits' do
result = pi(params[:digits])
if result
status 200
body result.to_json
else
status 500
body "ERROR".to_json
end
end
get '/inspect/:url' do
page = MetaInspector.new(self.url)
website = Website.new(json_params)
if page and website
website.description = page.best_description.to_json
website.title = page.title
website.save
website.domain = page.host
if website.save
status 200
body website.to_json
end
end
end
get '/hotdog/:test' do
score = params[:test].similar("hotdog")
if score and score > 10.0
status 200
body "#{score.round(2).to_json}% HOTDOG!".to_json
else
status 200
body "NOT HOTDOG!".to_json
end
end
post '/candidate/:resume' do
record = Candidate.new(json_params)
if record.save
status 200
body record.to_json
else
status 500
body "COULD NOT SAVE: #{json_params.to_s}".to_json
end
end
get '/candidate/:email' do
record = Candidate.where(:email => params[:email]).first
if record
status 200
body record.to_json
else
status 500
body "NOT FOUND: #{json_params.to_s}".to_json
end
end
get '/job/:id' do
record = Job.where(id: params[:id]).first
if record
status 200
body record.to_json
else
status 500
body "NOT FOUND: #{json_params.to_s}".to_json
end
end
get '/matches/:email' do
person = Candidate.where(email:params[:email]).first
results = []
Job.all.each do |item|
score = person.resume.similar(item.description)
if score and score > 61.0
email = person.email.to_s
job_id = item.id.to_s
if matches.insert(email: email, job_id: job_id, score:score)
results << {email:email,job_id:job_id,score:score}
puts "+ #{person.name}: #{score}% #{item.title}"
end
end
end
status 200
body results.to_json
end
get '/score/:email/:job_id' do
@candidate = Candidate.where(email: params[:email]).first
@job = Job.where(id: params[:job_id]).first
@score = @job.description.similar(@candidate.resume)
if @score
status 200
body @score.to_json
else
status 200
body "NO RESULTS FOR #{params}".to_json
end
end
get '/jobs' do
jobs = Job.all
[:title, :company, :description, :email].each do |filter|
jobs = jobs.send(filter, params[filter]) if params[filter]
end
jobs.map { |job| JobSerializer.new(job) }.to_json
end
get '/jobs/:id' do |id|
halt_if_not_found!
job_serialize(job)
end
post '/jobs ' do
job = Job.new(json_params)
halt 422, job_serialize(job) unless job.save
response.headers['Location'] = "#{base_url}/jobs/#{job.id}"
status 201
end
patch '/jobs/:id' do |id|
halt_if_not_found!
halt 422, serialize(job) unless job.update_attributes(json_params)
job_serialize(job)
end
delete '/jobs/:id' do |id|
job.destroy if job
status 204
end
get '/candidates' do
candidates = Candidate.all
[:name, :resume, :email].each do |filter|
candidates = candidates.send(filter, params[filter]) if params[filter]
end
candidates.map { |candidate| CandidateSerializer.new(candidate) }.to_json
end
get '/candidates/:id' do |id|
# halt_if_not_found!
candidate_serialize(candidate)
end
post '/candidates' do
candidate = Candidate.new(json_params)
candidate.parse_resume
halt 422, serialize(candidate) unless candidate.save
response.headers['Location'] = "#{base_url}/candidates/#{candidate.id}"
status 201
end
patch '/candidates/:id' do |id|
# halt_if_not_found!
halt 422, serialize(candidate) unless candidate.update_attributes(json_params)
candidate_serialize(candidate)
end
delete '/candidates/:id' do |id|
candidate.destroy if candidate
status 204
end
end
if ENV['SEED']
csvs = ["data/ai.csv", "data/ml.csv", "data/dsci.csv"]
csvs.each do |filename|
csv = CSV.open(filename)
csv.each do |row|
job = Job.new(description: row.last, title: row[1], email: FFaker::Internet.email, phone: FFaker::PhoneNumber.phone_number)
if rand(11) == 1
candidate = Candidate.new(:email => FFaker::Internet.email, :name => FFaker::Name.name, :resume => FFaker::HipsterIpsum.paragraph, :phone => FFaker::PhoneNumber.phone_number)
if candidate.save
puts candidate.name
end
end
if job.save
puts job.title
end
end
end
end
if ENV['MATCH_JOBS']
# puts "[INFO] >> (#{Time.now}) Begin processing #{Candidate.all.count * Job.all.count} possible scores of #{Candidate.all.count} resumes for #{Job.all.count} open positions in 5 seconds..."
$scores = []
time_start = Time.now.to_i
$all_matches = []
$match_limit = 1000
Candidate.all.each do |person|
$match_limit -= 1
Job.all.each do |item|
break if $match_limit < 1
@score = person.to_s.similar(item.to_s)
$match_limit -= 1
if @score and @score > 61.0
email = person.email.to_s
job_id = item.id.to_s
if matches.insert(email: email, job_id: job_id, score: @score)
$all_matches << {email:email,job_id:job_id,score:@score}
$match_counter += 1
puts "+ #{person.name}: #{@score}% #{item.title}"
end
end
end
end
time_end = Time.now.to_i
puts "!------DONE--------!"
puts "MATCHES: #{$match_counter} total"
puts "RUN TIME: #{time_end - time_start} seconds"
puts "AVG SCORE: #{matches.avg(:score)}%"
puts "<------------------>"
end