Skip to content

Instantly share code, notes, and snippets.

@memoht

memoht/Job.rb

Last active May 10, 2020
Embed
What would you like to do?
XML file I am trying to parse, and create or update records from
class Job < ApplicationRecord
# Enum
enum status: [:pre_flight, :in_flight, :flight_change, :flight_hold, :flight_complete, :grounded, :archive]
# Scopes
scope :live, -> { where("expiration_date >= ?", Time.zone.now + 48.hours) }
# Validations
#validates :app_url, :company, :description, :end_date, :start_date,
# :location, :ref_no, :title,
# presence: true
# Methods
def self.standard_xml
@feed = Nokogiri::XML(open('https://www.example.com/feed.xml')) do |config|
config.strict.noblanks
end
set_variables
end
def self.set_variables
@jobs = @feed.xpath('//source/job')
@all_jobs = Job.pluck(:ref_no)
parse_feed_beta
end
def self.parse_feed_beta
@jobs.each do |record|
ref_no = record.xpath("referencenumber").text.squish.to_i
title = record.xpath("title").text.titleize.squish
app_url = record.xpath("url").text.squish
location = record.xpath("location").text.squish
start_date = record.xpath("date").text
end_date = record.xpath("expiration_date").text
company = record.xpath("company").text.squish
description = record.xpath("description").text.squish
employer_id = record.xpath("employer_id").text.squish.to_i
job = Job.where(ref_no: ref_no).first_or_initialize
job.update_attributes(
company: company,
start_date: start_date,
end_date: end_date,
ref_no: ref_no,
title: title,
app_url: app_url,
location: location,
description: description,
employer_id: employer_id
)
end
end
end
<?xml version="1.0" encoding="UTF-8"?>
<source> <publisher>example</publisher> <publisherurl>example.com</publisherurl> <lastBuildDate>2020-05-04 13:43:00 UTC</lastBuildDate>
<job>
<title>
<![CDATA[Starfleet Engineer]]>
</title>
<url>
<![CDATA[http://www.example.com/jobs/34172773-starfleet-engineer-at-bell-marietta]]>
</url>
<referencenumber>
<![CDATA[34172773]]>
</referencenumber>
<date>
<![CDATA[Mon, 4 May 2020 15:13:46 UTC]]>
</date>
<expiration_date>
<![CDATA[06/03/2020 15:14:00 UTC]]>
</expiration_date>
<updated>
<![CDATA[Tue, 5 May 2020 17:24:17 UTC]]>
</updated>
<company>
<![CDATA[Bell Marietta]]>
</company>
<location>
<![CDATA[Raleigh, NC, USA]]>
</location>
<city>
<![CDATA[Raleigh]]>
</city>
<state>
<![CDATA[North Carolina]]>
</state>
<zip>
</zip>
<country>
<![CDATA[United States]]>
</country>
<description>
<![CDATA[<p>Members of Starfleet must be innovative, highly skilled, and versatile.</p>
<p>Starship and starbase designs require a large number of complex systems in order to function both safely and efficiently, and are monitored and controlled by highly trained Starfleet personnel who specialise in a particular area of expertise. </p>
<p>The career path a future Starfleet member may follow can be quite varied within the command structure of starships and starbases, but whatever path you choose will be rewarding.</p>
<ul>
<li>Full Benefits and medical care</li>
<li>Sense of duty and value in your work.</li>
<li>Explore new worlds, new civilizations.</li>
</ul>]]>
</description>
<jobtype>
<![CDATA[Full Time]]>
</jobtype>
<compensation_min>
</compensation_min>
<compensation_max>
</compensation_max>
<compensation_currency>
<![CDATA[USD]]>
</compensation_currency>
<compensation_interval>
<![CDATA[Annually]]>
</compensation_interval>
<zipapply_intl>
<![CDATA[0]]>
</zipapply_intl>
<apply_url>
<![CDATA[https://www.startrek.com]]>
</apply_url>
<featured>
<![CDATA[true]]>
</featured>
<priority>
<![CDATA[Featured]]>
</priority>
<logo>
</logo>
<category>
<![CDATA[Energy / Power,Engineering]]>
</category>
<employer_id>
</employer_id>
<campaign_type>
</campaign_type>
</job>
<job>
<title>
<![CDATA[Financial Analyst]]>
</title>
<url>
<![CDATA[http://www.example.com/jobs/33297662-financial-analyst-at-gracebook]]>
</url>
<referencenumber>
<![CDATA[33297662]]>
</referencenumber>
<date>
<![CDATA[Mon, 13 Apr 2020 23:32:59 UTC]]>
</date>
<expiration_date>
<![CDATA[05/13/2020 23:32:59 UTC]]>
</expiration_date>
<updated>
<![CDATA[Mon, 13 Apr 2020 23:33:37 UTC]]>
</updated>
<company>
<![CDATA[Gracebook]]>
</company>
<location>
<![CDATA[New York, NY]]>
</location>
<city>
<![CDATA[New York]]>
</city>
<state>
<![CDATA[New York]]>
</state>
<zip>
</zip>
<country>
<![CDATA[United States]]>
</country>
<description>
<![CDATA[<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at elit non eros rutrum lobortis.
Donec libero urna, suscipit in elementum vitae, consequat viverra libero. Donec in odio et nulla laoreet
faucibus eu a massa. Nunc ac eros in erat vestibulum lobortis. Etiam varius dignissim magna, at sollicitudin
risus congue a. Phasellus posuere magna in lacus tempus rutrum. Proin bibendum commodo facilisis. Praesent id
blandit enim. Ut aliquam vehicula erat eget pharetra. Quisque dignissim viverra nunc nec sollicitudin. Donec
massa sem, dictum id pretium sit amet, porttitor sit amet velit. Nunc accumsan, ante ac euismod scelerisque,
enim massa congue sem, sed dictum mauris dolor molestie quam. In id orci a est faucibus pharetra. Nulla
facilisi.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at elit non eros rutrum lobortis. Donec libero
urna, suscipit in elementum vitae, consequat viverra libero. Donec in odio et nulla laoreet faucibus eu a
massa. Nunc ac eros in erat vestibulum lobortis. Etiam varius dignissim magna, at sollicitudin risus congue a.
Phasellus posuere magna in lacus tempus rutrum. Proin bibendum commodo facilisis. Praesent id blandit enim.
Ut aliquam vehicula erat eget pharetra. Quisque dignissim viverra nunc nec sollicitudin. Donec massa sem,
dictum id pretium sit amet, porttitor sit amet velit. Nunc accumsan, ante ac euismod scelerisque, enim
massa congue sem, sed dictum mauris dolor molestie quam. In id orci a est faucibus pharetra. Nulla
facilisi.</p>
]]>
</description>
<jobtype>
<![CDATA[Full Time]]>
</jobtype>
<compensation_min>
</compensation_min>
<compensation_max>
</compensation_max>
<compensation_currency>
</compensation_currency>
<compensation_interval>
</compensation_interval>
<zipapply_intl>
<![CDATA[0]]>
</zipapply_intl>
<apply_url>
<![CDATA[http://www.examplecorp.com/apply]]>
</apply_url>
<featured>
<![CDATA[false]]>
</featured>
<priority>
<![CDATA[Standard]]>
</priority>
<logo>
<![CDATA[https://www.example.com/uploads/job/logo/33297662/1.png]]>
</logo>
<category>
<![CDATA[]]>
</category>
<employer_id>
<![CDATA[c537bed8-0f01-4779-b00a-f9fa990af3f9]]>
</employer_id>
<campaign_type>
</campaign_type>
</job>
<job>
<title>
<![CDATA[Short term administrative assistant]]>
</title>
<url>
<![CDATA[http://www.example.com/jobs/33297663-short-term-administrative-assistant-at-smith-associates]]>
</url>
<referencenumber>
<![CDATA[33297663]]>
</referencenumber>
<date>
<![CDATA[Mon, 13 Apr 2020 23:32:59 UTC]]>
</date>
<expiration_date>
<![CDATA[05/13/2020 23:32:59 UTC]]>
</expiration_date>
<updated>
<![CDATA[Mon, 13 Apr 2020 23:33:55 UTC]]>
</updated>
<company>
<![CDATA[Smith & Associates]]>
</company>
<location>
<![CDATA[Austin, TX]]>
</location>
<city>
<![CDATA[Austin]]>
</city>
<state>
<![CDATA[Texas]]>
</state>
<zip>
</zip>
<country>
<![CDATA[United States]]>
</country>
<description>
<![CDATA[<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at elit non eros rutrum lobortis.
Donec libero urna, suscipit in elementum vitae, consequat viverra libero. Donec in odio et nulla laoreet
faucibus eu a massa. Nunc ac eros in erat vestibulum lobortis. Etiam varius dignissim magna, at sollicitudin
risus congue a. Phasellus posuere magna in lacus tempus rutrum. Proin bibendum commodo facilisis. Praesent id
blandit enim. Ut aliquam vehicula erat eget pharetra. Quisque dignissim viverra nunc nec sollicitudin. Donec
massa sem, dictum id pretium sit amet, porttitor sit amet velit. Nunc accumsan, ante ac euismod scelerisque,
enim massa congue sem, sed dictum mauris dolor molestie quam. In id orci a est faucibus pharetra. Nulla
facilisi.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at elit non eros rutrum lobortis. Donec libero
urna, suscipit in elementum vitae, consequat viverra libero. Donec in odio et nulla laoreet faucibus eu a
massa. Nunc ac eros in erat vestibulum lobortis. Etiam varius dignissim magna, at sollicitudin risus congue a.
Phasellus posuere magna in lacus tempus rutrum. Proin bibendum commodo facilisis. Praesent id blandit enim.
Ut aliquam vehicula erat eget pharetra. Quisque dignissim viverra nunc nec sollicitudin. Donec massa sem,
dictum id pretium sit amet, porttitor sit amet velit. Nunc accumsan, ante ac euismod scelerisque, enim
massa congue sem, sed dictum mauris dolor molestie quam. In id orci a est faucibus pharetra. Nulla
facilisi.</p>
]]>
</description>
<jobtype>
<![CDATA[Full Time]]>
</jobtype>
<compensation_min>
</compensation_min>
<compensation_max>
</compensation_max>
<compensation_currency>
</compensation_currency>
<compensation_interval>
</compensation_interval>
<zipapply_intl>
<![CDATA[0]]>
</zipapply_intl>
<apply_url>
<![CDATA[http://www.examplecorp.com/apply]]>
</apply_url>
<featured>
<![CDATA[false]]>
</featured>
<priority>
<![CDATA[Standard]]>
</priority>
<logo>
<![CDATA[https://www.example.com/uploads/job/logo/33297663/2.png]]>
</logo>
<category>
<![CDATA[]]>
</category>
<employer_id>
<![CDATA[de3df475-cb6f-49f8-9edb-99eb6e11eca2]]>
</employer_id>
<campaign_type>
</campaign_type>
</job>
</source>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.