-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjopilot_api.rb
More file actions
53 lines (45 loc) · 1.38 KB
/
jopilot_api.rb
File metadata and controls
53 lines (45 loc) · 1.38 KB
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
require 'net/http'
require 'json'
module JoPilotAPI
API_BASE_URL = 'https://api.jopilot.net'
class JobSearchRequest
attr_accessor :title, :company_name, :city, :state, :country, :is_remote, :size
def initialize(title:, size:, company_name: nil, city: nil, state: nil, country: nil, is_remote: nil)
@title = title
@company_name = company_name
@city = city
@state = state
@country = country
@is_remote = is_remote
@size = size
end
def to_h
{
title: @title,
companyName: @company_name,
city: @city,
state: @state,
country: @country,
isRemote: @is_remote,
size: @size
}.compact
end
end
class JobSearchService
def self.search_jobs(request)
uri = URI("#{API_BASE_URL}/api/v1/search")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' })
req.body = request.to_h.to_json
begin
response = http.request(req)
raise "Error: #{response.message}" unless response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body, symbolize_names: true)
rescue StandardError => e
puts "Error searching jobs: #{e.message}"
raise 'There was an error connecting to the job search service.'
end
end
end
end