README.adoc
= RelatonGb
image:https://img.shields.io/gem/v/relaton-gb.svg["Gem Version", link="https://rubygems.org/gems/relaton-gb"]
image:https://github.com/relaton/relaton-gb/workflows/macos/badge.svg["Build Status (macOS)", link="https://github.com/relaton/relaton-gb/actions?workflow=macos"]
image:https://github.com/relaton/relaton-gb/workflows/windows/badge.svg["Build Status (Windows)", link="https://github.com/relaton/relaton-gb/actions?workflow=windows"]
image:https://github.com/relaton/relaton-gb/workflows/ubuntu/badge.svg["Build Status (Ubuntu)", link="https://github.com/relaton/relaton-gb/actions?workflow=ubuntu"]
image:https://codeclimate.com/github/relaton/relaton-gb/badges/gpa.svg["Code Climate", link="https://codeclimate.com/github/relaton/relaton-gb"]
image:https://img.shields.io/github/issues-pr-raw/relaton/relaton-gb.svg["Pull Requests", link="https://github.com/relaton/relaton-gb/pulls"]
image:https://img.shields.io/github/commits-since/relaton/relaton-gb/latest.svg["Commits since latest",link="https://github.com/relaton/relaton-gb/releases"]
RelatonGb is a Ruby gem that searches and fetches Chinese GB standards.
The standards scraped form:
* GB standards - http://www.std.gov.cn/gb/search
* Social organization standards - http://www.ttbz.org.cn
* Sector standards - http://www.std.gov.cn/hb
NOTE: Unlike the ISO website, the GB websites require the year of publication to be included in the search.
So a standard will not be found successfully through this gem unless the year is included in its document
identifier: for example, not `CN(GB/T 1.1)`, but `CN(GB/T 1.1-2009)`.
== Installation
Add this line to your application's Gemfile:
[source,ruby]
----
gem 'relaton-gb'
----
And then execute:
$ bundle
Or install it yourself as:
$ gem install relaton-gb
== Usage
=== Configuration
Configuration is optional. The available option is `logger` which is a `Logger` instance. By default, the logger is `Logger.new($stderr)` with `Logger::WARN` level. To change the logger level, use `RelatonGb.configure` block.
[source,ruby]
----
require 'relaton_gb'
=> true
RelatonGb.configure do |config|
config.logger.level = Logger::DEBUG
end
----
=== Search document
[source,ruby]
----
hit_collection = RelatonGb::GbBibliography.search "GB/T 20223-2006"
[relaton-gb] (GB/T 20223-2006) Fetching from openstd.samr.gov.cn ...
=> <RelatonGb::HitCollection:0x007fc8d8a26d10 @ref= @fetched=false>
hit_collection.first
=> <RelatonGb::Hit:0x007fc8d8a27c88 @fullIdentifier="" @docref="GB/T 20223-2006">
----
=== Scrape document form collection
[source,ruby]
----
hit_collection.first.fetch
=> <RelatonGb::GbBibliographicItem:0x007f975a0219c8>
hit_collection.first.fetch.committee
=> #<RelatonGb::GbTechnicalCommittee:0x007f975713c450 @name="中国纤维检验局", @type="technical">
hit_collection.first.fetch.gbtype
=> #<RelatonGb::GbStandardType:0x0000000112957dc0 @mandate="recommended", @prefix="GB_national", @scope="national", @topic="other">
hit_collection.first.fetch.ccs
=> [#<Cnccs::Ccs:0x007f975713c090
@code="B32",
@description="纤维作物与产品",
@description_full="农业、林业; 经济作物; 纤维作物与产品",
@fieldcode="B",
@groupcode="30/39",
@notes=[#<Cnccs::Note:0x007f975716edb0 @ccs_code="", @text="棉、麻等">],
@subgroupcode="32">]
hit_collection.first.fetch.title
=> #<RelatonBib::TypedTitleStringCollection:0x0000000112a1ac08
@array=
[#<RelatonBib::TypedTitleString:0x0000000112a1f438
@title=#<RelatonBib::FormattedString:0x0000000112cd6050 @content="棉短绒", @format="text/plain", @language=["zh"], @script=["Hans"]>,
@type="title-main">,
#<RelatonBib::TypedTitleString:0x0000000112a1eb78
@title=#<RelatonBib::FormattedString:0x0000000112cd6000 @content="棉短绒", @format="text/plain", @language=["zh"], @script=["Hans"]>,
@type="main">,
#<RelatonBib::TypedTitleString:0x0000000112a1c198
@title=#<RelatonBib::FormattedString:0x0000000112cd5ce0 @content="Cotton linter", @format="text/plain", @language=["en"], @script=["Latn"]>,
@type="title-main">,
#<RelatonBib::TypedTitleString:0x0000000112a1bba8
@title=#<RelatonBib::FormattedString:0x0000000112cd5c40 @content="Cotton linter", @format="text/plain", @language=["en"], @script=["Latn"]>,
@type="main">]>
hit_collection.first.fetch.date
=> [#<RelatonBib::BibliographicDate:0x000000011307f468 @from=nil, @on="2006-03-10", @to=nil, @type="published">]
----
=== Get document by identifier
[source,ruby]
----
RelatonGb::GbBibliography.get "GB/T 20223-2006"
[relaton-gb] (GB/T 20223-2006) Fetching from openstd.samr.gov.cn ...
[relaton-gb] (GB/T 20223-2006) Found: `GB/T 20223-2006`
=> <RelatonGb::GbBibliographicItem:0x000000000011f8>
----
=== Get document by identifier and year
[source,ruby]
----
RelatonGb::GbBibliography.get "GB/T 20223", "2006"
[relaton-gb] (GB/T 20223-2006) Fetching from openstd.samr.gov.cn ...
[relaton-gb] (GB/T 20223-2006) Found: `GB/T 20223-2006`
=> <RelatonGb::GbBibliographicItem:0x00000000001248>
----
=== Create bibliographic item from YAML
[source,ruby]
----
hash = YAML.load_file 'spec/examples/gb_bib_item.yml'
=> {"id"=>"JB/T13368",
...
RelatonGb::GbBibliographicItem.from_hash hash
=> <RelatonGb::GbBibliographicItem:0x007fc680802700>
----
=== Serialization
[source,ruby]
----
hit_collection.first.fetch.to_xml
=> "<bibitem id="GB/T20223-2006" type="standard" schema-version="v1.2.1">
<fetched>2022-12-03</fetched>
<title type="title-main" format="text/plain" language="zh" script="Hans">棉短绒</title>
<title type="main" format="text/plain" language="zh" script="Hans">棉短绒</title>
<title type="title-main" format="text/plain" language="en" script="Latn">Cotton linter</title>
...
</bibitem>"
----
With `bibdata: true` option XML output is wrapped with `bibdata` element and `ext` element added.
[source,ruby]
----
hit_collection.first.fetch.to_xml bibdata: true
=> "<bibdata type="standard" schema-version="v1.2.1">
<fetched>2022-12-03</fetched>
<title type="title-main" format="text/plain" language="zh" script="Hans">棉短绒</title>
<title type="main" format="text/plain" language="zh" script="Hans">棉短绒</title>
<title type="title-main" format="text/plain" language="en" script="Latn">Cotton linter</title>
<title type="main" format="text/plain" language="en" script="Latn">Cotton linter</title>
...
<ext schema-version="v1.0.0">
<doctype>standard</doctype>
...
</ext>
</bibdata>"
----
=== Typed links
Each GB document has `src` type link.
[source,ruby]
----
hit_collection.first.fetch.link
=> [#<RelatonBib::TypedUri:0x00007fee2c0257e8 @content=#<Addressable::URI:0x67c URI:http://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=083B48FA72DBD3B9BDE74507BC31736A>, @type="src">]
----
== Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
== Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gdbib.
== License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).