Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Aug 20, 2020
1 parent 288da2d commit 3aafb56
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 22 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
12 changes: 12 additions & 0 deletions lib/relaton_gb/ccs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ class Ccs
def to_hash
{ "code" => code }
end

# @param prefix [String]
# @param count [Integer]
# @return [String]
def to_aciibib(prefix = "", count = 1)
pref = prefix.empty? ? prefix : prefix + "."
pref += "ccs"
out = count > 1 ? "#{pref}::\n" : ""
out += "#{pref}.code:: #{code}\n" if code
out += "#{pref}.description:: #{description}\n" if description
out
end
end
end
8 changes: 8 additions & 0 deletions lib/relaton_gb/gb_bibliographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def to_hash
hash
end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "")
out = super
ccs.each { |c| out += c.to_aciibib prefix, ccs.size }
out
end

# @return [String]
def inspect
"<#{self.class}:#{format('%<id>#.14x', id: object_id << 1)}>"
Expand Down
12 changes: 5 additions & 7 deletions lib/relaton_gb/t_scrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class << self
def scrape_page(text)
search_html = OpenURI.open_uri(
"http://www.ttbz.org.cn/Home/Standard?searchType=2&key=" +
CGI.escape(text.tr("-", [8212].pack("U"))),
CGI.escape(text.tr("-", [8212].pack("U")))
).read
header = Nokogiri::HTML search_html
xpath = '//table[contains(@class, "standard_list_table")]/tr/td/a'
Expand Down Expand Up @@ -85,7 +85,8 @@ def get_committee(doc, _ref)

def get_titles(doc)
xpz = '//td[contains(.,"中文标题")]/following-sibling::td[1]'
titles = RelatonBib::TypedTitleString.from_string doc.at(xpz).text, "zh", "Hans"
titles = RelatonBib::TypedTitleString.from_string doc.at(xpz)
.text, "zh", "Hans"
xpe = '//td[contains(.,"英文标题")]/following-sibling::td[1]'
ten = doc.xpath(xpe).text
return titles if ten.empty?
Expand All @@ -94,13 +95,10 @@ def get_titles(doc)
end

def gbtype
{ scope: "social-group", prefix: "T", mandate: "mandatory", topic: "other" }
{ scope: "social-group", prefix: "T", mandate: "mandatory",
topic: "other" }
end

# def get_group_code(ref)
# ref.match(%r{(?<=\/)[^\s]})
# end

def get_ccs(doc)
[doc.xpath('//td[contains(.,"中国标准分类号")]/following-sibling::td[1]')
.text.gsub(/[\r\n]/, "").strip.match(/^[^\s]+/).to_s]
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_gb/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RelatonGb
VERSION = "1.2.0"
VERSION = "1.3.0"
end
2 changes: 1 addition & 1 deletion relaton_gb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "cnccs", "~> 0.1.1"
spec.add_dependency "gb-agencies", "~> 0.0.1"
spec.add_dependency "relaton-iso-bib", ">= 1.2.0"
spec.add_dependency "relaton-iso-bib", ">= 1.3.0"
end
51 changes: 51 additions & 0 deletions spec/examples/asciibib.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[%bibitem]
== {blank}
id:: JB/T13368
fetched:: 2020-08-15
title::
title.type:: title-main
title.conten:: 叉车属具 软包夹
title.language:: zh
title.script:: Hans
title.format:: text/plain
title::
title.type:: main
title.conten:: 叉车属具 软包夹
title.language:: zh
title.script:: Hans
title.format:: text/plain
title::
title.type:: title-main
title.conten:: Forklift attachment
title.language:: en
title.script:: Latn
title.format:: text/plain
title::
title.type:: main
title.conten:: Forklift attachment
title.language:: en
title.script:: Latn
title.format:: text/plain
type:: standard
docid.type:: JB
docid.id:: T13368
docnumber:: 13368
language:: zh
language:: en
script:: Hans
script:: Latn
docstatus.stage:: activated
date.type:: published
date.on:: 2009-01-01
link.type:: src
link.content:: http://www.std.gov.cn/hb/search/stdHBDetailed?id=6BC3AD94A1728ABCE05397BE0A0A5667
contributor.organization.name.conten:: State Economic and Trade Commission
contributor.organization.name.language:: en
contributor.role.type:: publisher
doctype:: standard
ics.code:: 53.060
ics.description:: Industrial trucks
structured_identifier.project_number:: JB/T 13368
structured_identifier.type:: Chinese Standard
ccs.code:: J83
ccs.description:: 仓储设备、装卸机械
36 changes: 25 additions & 11 deletions spec/relaton_gb/gb_bibliographic_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,30 @@
# end.to raise_error ArgumentError
# end

it "returns Hash" do
hash = YAML.load_file "spec/examples/gb_bib_item.yml"
item_hash = RelatonGb::HashConverter.hash_to_bib hash
item = RelatonGb::GbBibliographicItem.new item_hash
h = item.to_hash
expect(h["committee"]).to eq hash["committee"]
expect(h["ics"]).to eq hash["ics"]
expect(h["structuredidentifier"]).to eq hash["structuredidentifier"]
expect(h["gbtype"]).to eq hash["gbtype"]
expect(h["ccs"]).to eq hash["ccs"]
expect(h["plannumber"]).to eq hash["plannumber"]
context "instance" do
subject do
hash = YAML.load_file "spec/examples/gb_bib_item.yml"
item_hash = RelatonGb::HashConverter.hash_to_bib hash
RelatonGb::GbBibliographicItem.new item_hash
end

it "returns Hash" do
hash = YAML.load_file "spec/examples/gb_bib_item.yml"
h = subject.to_hash
expect(h["committee"]).to eq hash["committee"]
expect(h["ics"]).to eq hash["ics"]
expect(h["structuredidentifier"]).to eq hash["structuredidentifier"]
expect(h["gbtype"]).to eq hash["gbtype"]
expect(h["ccs"]).to eq hash["ccs"]
expect(h["plannumber"]).to eq hash["plannumber"]
end

it "returns AciiBib" do
file = "spec/examples/asciibib.adoc"
bib = subject.to_asciibib
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}-\d{2}/, Date.today.to_s)
end
end
end

0 comments on commit 3aafb56

Please sign in to comment.