Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Aug 21, 2020
1 parent 5ec0e5e commit 8a4f505
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
inherit_from:
- https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
Rails:
Enabled: true
Enabled: false
8 changes: 8 additions & 0 deletions lib/relaton_un/editorialgroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ def to_xml(builder)
def to_hash
single_element_array(committee.map { |c| { "committee" => c } })
end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix)
pref = prefix.empty? ? prefix : prefix + "."
pref += "editorialgroup"
committee.map { |c| "#{pref}.committee:: #{c}\n" }.join
end
end
end
6 changes: 3 additions & 3 deletions lib/relaton_un/hit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def fetch
# rubocop:disable Metrics/MethodLength

# @return [RelatonUn::UnBibliographicItem]
def un_bib_item
def un_bib_item # rubocop:disable Metrics/AbcSize
UnBibliographicItem.new(
type: "standard",
fetched: Date.today.to_s,
Expand All @@ -75,7 +75,7 @@ def un_bib_item
distribution: fetch_distribution,
editorialgroup: fetch_editorialgroup,
classification: fetch_classification,
job_number: hit[:job_number],
job_number: hit[:job_number]
)
end
# rubocop:enable Metrics/MethodLength
Expand Down Expand Up @@ -131,7 +131,7 @@ def fetch_distribution
def fetch_editorialgroup
tc = hit[:ref].match(/^[\S]+/).to_s.split(/\/|-/).reduce([]) do |m, v|
if BODY[v] then m << BODY[v]
elsif v =~ /(AC|C|CN|CONF|GC|SC|Sub|WG).\d+|PC/ then m << v
elsif v.match? /(AC|C|CN|CONF|GC|SC|Sub|WG).\d+|PC/ then m << v
else m
end
end.uniq
Expand Down
6 changes: 3 additions & 3 deletions lib/relaton_un/hit_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def get_page(location = "/", deep = 0)
# @param form [Nokogiri::HTML::Document]
# @param text [String]
# @return [Array<String>]
def form_data(form, text)
def form_data(form, text) # rubocop:disable Metrics/CyclomaticComplexity
fd = form.xpath(
"//input[@type!='radio']",
"//input[@type='radio'][@checked]",
"//select[@name!='view:_id1:_id2:cbLang']",
"//textarea",
"//textarea"
).reduce([]) do |m, i|
v = case i[:name]
when "view:_id1:_id2:txtSymbol" then text
Expand Down Expand Up @@ -91,7 +91,7 @@ def hit(item)

# @param item [Nokogiri::XML::Element]
# @return [Hash]
def hit_data(item)
def hit_data(item) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
en = item.at("//span[.='ENGLISH']/../..")
{
ref: item.at("div/div/a")&.text&.sub("\u00A0", ""),
Expand Down
42 changes: 32 additions & 10 deletions lib/relaton_un/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Session
include RelatonBib

# @return [String, NilClass]
attr_reader :session_number, :collaboration, :agenda_id, :item_footnote
attr_reader :session_number, :collaborator, :agenda_id, :item_footnote

# @return [Date, NilClass]
attr_reader :session_date
Expand All @@ -16,7 +16,7 @@ class Session
# @param item_number [Array<String>]
# @pqrqm item_name [Array<String>]
# @pqrqm subitem_name [Array<String>]
# @param collaboration [String]
# @param collaborator [String]
# @param agenda_id [String]
# @param item_footnote [String]
def initialize(**args)
Expand All @@ -25,12 +25,12 @@ def initialize(**args)
@item_number = args.fetch(:item_number, [])
@item_name = args.fetch(:item_name, [])
@subitem_name = args.fetch(:subitem_name, [])
@collaboration = args[:collaboration]
@collaborator = args[:collaborator]
@agenda_id = args[:agenda_id]
@item_footnote = args[:item_footnote]
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize

# @param [Nokogiri::XML::Builder]
def to_xml(builder)
Expand All @@ -40,26 +40,48 @@ def to_xml(builder)
item_number.each { |n| b.send "item-number", n }
item_name.each { |n| b.send "item-name", n }
subitem_name.each { |n| b.send "subitem-name", n }
b.collaboration collaboration if collaboration
b.collaborator collaborator if collaborator
b.send "agenda-id", agenda_id if agenda_id
b.send "item-footnote", item_footnote if item_footnote
end
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength
# @return [Hash]
def to_hash
hash = {}
hash["session_number"] = session_number if session_number
hash["session_date"] = session_date.to_s if session_date
hash["item_number"] = single_element_array(item_number) if item_number.any?
if item_number.any?
hash["item_number"] = single_element_array(item_number)
end
hash["item_name"] = single_element_array(item_name) if item_name.any?
hash["subitem_name"] = single_element_array(subitem_name) if subitem_name.any?
hash["collaboration"] = collaboration if collaboration
if subitem_name.any?
hash["subitem_name"] = single_element_array(subitem_name)
end
hash["collaborator"] = collaborator if collaborator
hash["agenda_id"] = agenda_id if agenda_id
hash["item_footnote"] = item_footnote if item_footnote
hash
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/PerceivedComplexity

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "")
pref = prefix.empty? ? prefix : prefix + "."
pref += "session"
out = ""
out += "#{pref}.session_number:: #{session_number}\n" if session_number
out += "#{pref}.session_date:: #{session_date}\n" if session_date
item_number.each { |n| out += "#{pref}.item_number:: #{n}\n" }
item_name.each { |n| out += "#{pref}.item_name:: #{n}\n" }
subitem_name.each { |n| out += "#{pref}.subitem_name:: #{n}\n" }
out += "#{pref}.collaborator:: #{collaborator}\n" if collaborator
out += "#{pref}.agenda_id:: #{agenda_id}\n" if agenda_id
out += "#{pref}.item_footnote:: #{item_footnote}\n" if item_footnote
out
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength
end
end
19 changes: 17 additions & 2 deletions lib/relaton_un/un_bibliographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class UnBibliographicItem < RelatonBib::BibliographicItem
# @param job_number [String, nil]
def initialize(**args)
if args[:distribution] && !DISTRIBUTIONS.has_value?(args[:distribution])
warn "[relaton-un] WARNING: invalid distribution: #{args[:distribution]}"
warn "[relaton-un] WARNING: invalid distribution: "\
"#{args[:distribution]}"
end
@submissionlanguage = args.delete :submissionlanguage
@distribution = args.delete :distribution
Expand All @@ -35,7 +36,7 @@ def initialize(**args)

# @param builder [Nokogiri::XML::Builder]
# @param bibdata [TrueClasss, FalseClass, NilClass]
def to_xml(builder = nil, **opts)
def to_xml(builder = nil, **opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
super(builder, **opts) do |b|
b.ext do
b.doctype doctype if doctype
Expand All @@ -60,5 +61,19 @@ def to_hash
hash["job_number"] = job_number if job_number
hash
end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "") # rubocop:disable Metrics/AbcSize
pref = prefix.empty? ? prefix : prefix + "."
out = super
submissionlanguage.each do |sl|
out += "#{pref}submissionlanguage:: #{sl}\n"
end
out += "#{pref}distribution:: #{distribution}\n" if distribution
out += session.to_asciibib prefix if session
out += "#{pref}job_number:: #{job_number}\n" if job_number
out
end
end
end
2 changes: 1 addition & 1 deletion lib/relaton_un/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonUn
VERSION = "1.2.1".freeze
VERSION = "1.3.0".freeze
end
6 changes: 3 additions & 3 deletions lib/relaton_un/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def item_data(item)

# @param ext [Nokogiri::XML::Element]
# @return [RelatonUn::Session]
def fetch_session(ext)
def fetch_session(ext) # rubocop:disable Metrics/CyclomaticComplexity
session = ext.at "./session"
RelatonUn::Session.new(
session_number: session.at("number")&.text,
session_date: session.at("session-date")&.text,
item_number: session.xpath("item-number").map(&:text),
item_name: session.xpath("item-name").map(&:text),
subitem_name: session.xpath("subitem-name").map(&:text),
collaboration: session.at("collaboration")&.text,
collaborator: session.at("collaborator")&.text,
agenda_id: session.at("agenda-id")&.text,
item_footnote: session.at("item-footnote")&.text,
item_footnote: session.at("item-footnote")&.text
)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
Expand Down
2 changes: 1 addition & 1 deletion relaton_un.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday"
spec.add_dependency "http-cookie"
spec.add_dependency "relaton-bib", "~> 1.2.0"
spec.add_dependency "relaton-bib", "~> 1.3.0"
spec.add_dependency "unf_ext", ">= 0.0.7.7"
end
# rubocop:enable Metrics/BlockLength
72 changes: 72 additions & 0 deletions spec/fixtures/asciibib.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[%bibitem]
== {blank}
id:: TRADE/CEFACT/2004/32
fetched:: 2020-04-19
title::
title.type:: title-main
title.conten:: SECRETARIAT REVIEW OF UN/LOCODE, 19 DECEMBER 2003 / SUBMITTED BY THE SECRETARIAT
title.language:: en
title.script:: Latn
title.format:: text/plain
title::
title.type:: main
title.conten:: SECRETARIAT REVIEW OF UN/LOCODE, 19 DECEMBER 2003 / SUBMITTED BY THE SECRETARIAT
title.language:: en
title.script:: Latn
title.format:: text/plain
type:: standard
docid.type:: UN
docid.id:: TRADE/CEFACT/2004/32
docnumber:: TRADE/CEFACT/2004/32
language:: en
script:: Latn
date::
date.type:: published
date.on:: 2004-01-01
date::
date.type:: issued
date.on:: 2004-01-01
link::
link.type:: pdf
link.content:: https://documents-dds-ny.un.org/doc/UNDOC/GEN/G04/306/83/pdf/G0430683.pdf?OpenElement
link::
link.type:: word
link.content:: https://documents-dds-ny.un.org/doc/UNDOC/GEN/G04/306/83/doc/G0430683.DOC?OpenElement
classification.type:: area
classification.value:: UNDOC
doctype:: addendum
keyword::
keyword.conten:: TRADE DATA INTERCHANGE
keyword::
keyword.conten:: CODES
keyword::
keyword.conten:: PORTS
keyword::
keyword.conten:: TRANSPORT TERMINALS
keyword::
keyword.conten:: TRADE FACILITATION
keyword::
keyword.conten:: PROJECT EVALUATION
keyword::
keyword.conten:: DATABASES
keyword::
keyword.conten:: WEBSITES
editorialgroup.committee:: Committee on Trade
editorialgroup.committee:: Centre for Trade Facilitation and Electronic Business
ics.code:: 01
ics.text:: First
submissionlanguage:: en
submissionlanguage:: fr
distribution:: general
session.session_number:: 10
session.session_date:: 2019-02-20
session.item_number:: IN1
session.item_number:: IN2
session.item_name:: INM1
session.item_name:: INM2
session.subitem_name:: SN1
session.subitem_name:: SN2
session.collaborator:: collaborator
session.agenda_id:: 12
session.item_footnote:: Item footnote
job_number:: 10
9 changes: 9 additions & 0 deletions spec/fixtures/un_bib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
<distribution>general</distribution>
<session>
<number>10</number>
<session-date>2019-02-20</session-date>
<item-number>IN1</item-number>
<item-number>IN2</item-number>
<item-name>INM1</item-name>
<item-name>INM2</item-name>
<subitem-name>SN1</subitem-name>
<subitem-name>SN2</subitem-name>
<collaborator>collaborator</collaborator>
<agenda-id>12</agenda-id>
<item-footnote>Item footnote</item-footnote>
</session>
<job_number>10</job_number>
</ext>
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/un_bib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ editorialgroup:
- committee: Centre for Trade Facilitation and Electronic Business
session:
session_number: '10'
session_date: '2019-02-20'
item_number:
- IN1
- IN2
item_name:
- INM1
- INM2
subitem_name:
- SN1
- SN2
collaborator: collaborator
agenda_id: '12'
item_footnote: Item footnote
doctype: addendum
submissionlanguage:
- en
Expand Down
11 changes: 11 additions & 0 deletions spec/relaton_un/un_bibliographic_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@
RelatonUn::UnBibliographicItem.new distribution: "INV"
end.to output(/invalid distribution/).to_stderr
end

it "return AsciiBib" do
hash = YAML.load_file "spec/fixtures/un_bib.yaml"
hash_bib = RelatonUn::HashConverter.hash_to_bib hash
item = RelatonUn::UnBibliographicItem.new hash_bib
bib = item.to_asciibib
file = "spec/fixtures/asciibib.adoc"
File.write file, bib, encoding: "UTF-8" unless File.exist? file
expect(bib).to eq File.read(file, encoding: "UTF-8")
.gsub(/(?<=fetched::\s)\d[4]-\d{2}-\n{2}/, Date.today.to_s)
end
end
3 changes: 2 additions & 1 deletion spec/relaton_un/xml_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
end

it "warn if XML doesn't contein bibiten or bibdata" do
expect { RelatonUn::XMLParser.from_xml "" }.to output(/can't find bibitem/).to_stderr
expect { RelatonUn::XMLParser.from_xml "" }.to output(/can't find bibitem/)
.to_stderr
end
end
Loading

0 comments on commit 8a4f505

Please sign in to comment.