Skip to content

Commit 4f40a87

Browse files
authored
Feat/try ox for xml (#349)
2 parents 62b52c4 + 47aa67e commit 4f40a87

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ gem "importmap-rails"
1414
gem "jbuilder"
1515
gem "kamal", require: false
1616
gem "mission_control-jobs"
17+
gem "ox"
1718
gem "pagy"
1819
gem "pg", "~> 1.6"
1920
gem "propshaft"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ GEM
301301
nokogiri (1.18.9-x86_64-linux-musl)
302302
racc (~> 1.4)
303303
ostruct (0.6.3)
304+
ox (2.14.23)
305+
bigdecimal (>= 3.0)
304306
pagy (9.3.5)
305307
parallel (1.27.0)
306308
parser (3.3.9.0)
@@ -543,6 +545,7 @@ DEPENDENCIES
543545
kamal
544546
letter_opener
545547
mission_control-jobs
548+
ox
546549
pagy
547550
pg (~> 1.6)
548551
propshaft

app/services/xml_generator/all_providers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def initialize(language, **args)
88

99
def xml_content(xml)
1010
language.providers.includes(:topics)
11-
.map do |provider|
12-
provider_xml(xml, provider)
11+
.each do |provider|
12+
xml << provider_xml(provider)
1313
end
1414
end
1515
end

app/services/xml_generator/base.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
class XmlGenerator::Base
22
def perform
3-
builder.to_xml
3+
Ox.dump(builder)
44
end
55

66
private
77

88
def builder
9-
Nokogiri::XML::Builder.new do |xml|
10-
xml.cmes { xml_content(xml) }
9+
Ox::Document.new.tap do |doc|
10+
instruct = Ox::Instruct.new(:xml)
11+
instruct[:version] = "1.0"
12+
doc << instruct
13+
14+
xml = Ox::Element.new("cmes")
15+
xml_content(xml)
16+
doc << xml
1117
end
1218
end
1319
end

app/services/xml_generator/single_provider.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,39 @@ def initialize(provider, **args)
99
attr_reader :provider, :args
1010

1111
def xml_content(xml)
12-
provider_xml(xml, provider)
12+
xml << provider_xml(provider)
1313
end
1414

15-
def provider_xml(xml, provider)
16-
xml.content_provider(name: provider.name) {
15+
def provider_xml(provider)
16+
Ox::Element.new("content_provider").tap do |xml|
17+
xml[:name] = provider.name
18+
1719
grouped_topics(provider).each do |(year, month), topics|
18-
xml.topic_year(year: year) {
19-
xml.topic_month(month: month) {
20+
xml << Ox::Element.new("topic_year").tap do |year_element|
21+
year_element[:year] = year.to_s
22+
year_element << Ox::Element.new("topic_month").tap do |month_element|
23+
month_element[:month] = month
2024
topics.each do |topic|
21-
xml.title(name: topic.title) {
22-
xml.topic_id topic.id
23-
xml.topic_files(files: "Files") {
25+
month_element << Ox::Element.new("title").tap do |title_element|
26+
title_element[:name] = topic.title
27+
title_element << Ox::Element.new("topic_id").tap { |id| id << topic.id.to_s }
28+
title_element << Ox::Element.new("topic_files").tap do |files|
29+
files[:files] = "Files"
2430
topic.documents.each_with_index do |document, index|
2531
next if document.content_type == "video/mp4"
26-
xml.send("file_name_#{index + 1}", document.filename, file_size: document.byte_size)
32+
files << Ox::Element.new("file_name_#{index + 1}").tap do |file_name|
33+
file_name << document.filename.to_s
34+
file_name[:file_size] = document.byte_size
35+
end
2736
end
28-
}
29-
xml.topic_tags topic.current_tags_list.join(", ")
30-
}
37+
end
38+
title_element << Ox::Element.new("topic_tags").tap { |tags| tags << topic.current_tags_list.join(", ") }
39+
end
3140
end
32-
}
33-
}
41+
end
42+
end
3443
end
35-
}
44+
end
3645
end
3746

3847
def grouped_topics(prov)

db/seeds.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,21 @@
107107
end
108108

109109
puts "Users created!"
110+
111+
if ENV["SEED_MODE_HEAVY_LOAD"]
112+
times = ENV["SEED_MODE_HEAVY_LOAD"].to_i
113+
times.times do
114+
provider = Provider.find_or_create_by!(name: Faker::Company.name, provider_type: "government")
115+
times.times do
116+
Topic.find_or_create_by!(
117+
title: Faker::Book.title,
118+
description: Faker::Lorem.paragraph,
119+
language_id: Language.first.id,
120+
provider_id: provider.id,
121+
uid: SecureRandom.uuid,
122+
published_at: Time.now - rand(1..365).days,
123+
state: [ :active, :archived ].sample,
124+
)
125+
end
126+
end
127+
end

0 commit comments

Comments
 (0)