cnccs 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cnccs/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cnccs"
8
+ spec.version = Cnccs::VERSION
9
+ spec.authors = ["Ribose Inc."]
10
+ spec.email = ["open.source@ribose.com"]
11
+
12
+ spec.summary = %q{Classification of Chinese Standards ("CCS") are accessible via this gem.}
13
+ spec.description = %q{Classification of Chinese Standards ("CCS") are accessible via this gem.}
14
+ spec.homepage = "https://github.com/riboseinc/cnccs"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "simplecov"
28
+ spec.add_development_dependency "pry-byebug"
29
+ end
@@ -0,0 +1,8 @@
1
+ require "cnccs/version"
2
+ require "cnccs/ccs"
3
+
4
+ module Cnccs
5
+ def self.fetch(code = nil, fieldcode: nil, groupcode: nil, subgroupcode: nil)
6
+ Ccs.new code, fieldcode: fieldcode, groupcode: groupcode, subgroupcode: subgroupcode
7
+ end
8
+ end
@@ -0,0 +1,100 @@
1
+ require "csv"
2
+
3
+ module Cnccs
4
+ class Note
5
+
6
+ # @return [String]
7
+ attr_reader :text, :ccs_code
8
+
9
+ # @param text [String]
10
+ # @param ccs_code [String]
11
+ def initialize(text, ccs_code)
12
+ @text = text
13
+ @ccs_code = ccs_code
14
+ end
15
+
16
+ # Fetch ccs reference by code.
17
+ # @return [Cnccs::Ccs]
18
+ def ccs
19
+ Ccs.new @ccs_code
20
+ end
21
+ end
22
+
23
+ class Ccs
24
+
25
+ # @return [String]
26
+ attr_reader :code, :fieldcode, :groupcode, :subgroupcode, :description, :description_full
27
+
28
+ # @return [Array<Cnccs::Note>]
29
+ attr_reader :notes
30
+
31
+ # @param code [String]
32
+ # @param fieldcode [String]
33
+ # @param groupcode [String]
34
+ # @param subgroupcode [String]
35
+ def initialize(code = nil, fieldcode: nil, groupcode: nil, subgroupcode: nil)
36
+ if code
37
+ case code.size
38
+ when 1 then fetch_field code
39
+ when 3 then fetch_subgroup code
40
+ end
41
+ elsif fieldcode && groupcode
42
+ @code, @description, @fieldcode, nts = fetch_data(2, fieldcode + groupcode)
43
+ @groupcode = @code[1..-1]
44
+ @notes = fetch_notes nts
45
+ _fldc, desc = fetch_data(1, @fieldcode)
46
+ @description_full = "#{desc}; #{@description}"
47
+ elsif fieldcode
48
+ fetch_field fieldcode
49
+ elsif subgroupcode
50
+ fetch_subgroup subgroupcode
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # Fetch data by field code.
57
+ # @param field_code [String]
58
+ def fetch_field(field_code)
59
+ @code = field_code
60
+ @fieldcode, @description = fetch_data(1, field_code)
61
+ @notes = []
62
+ end
63
+
64
+ # Fetch data by subgroup code.
65
+ # @param subgroup_code [String]
66
+ def fetch_subgroup(subgroup_code)
67
+ @code, @description, sg_code, nts = fetch_data(3, subgroup_code)
68
+ @fieldcode = @code.slice 0
69
+ @groupcode = sg_code[1..-1]
70
+ @subgroupcode = @code[1..-1]
71
+ @notes = fetch_notes nts
72
+ _fldc, desc1 = fetch_data(1, @fieldcode)
73
+ _fldc, desc2 = fetch_data(2, sg_code)
74
+ @description_full = "#{desc1}; #{desc2}; #{@description}"
75
+ end
76
+
77
+ # Read data from file.
78
+ # @param level [Integer]
79
+ # @param match [String]
80
+ # @return [Array<String, Array>]
81
+ def fetch_data(level, match)
82
+ csv = CSV.read "#{__dir__}/../../cn-ccs-codes/cat-level#{level}.csv", headers: true
83
+ row = csv.find { |r| r[0] == match }
84
+ row.map(&:last)
85
+ end
86
+
87
+ # Fetch notes.
88
+ # @param nts [String]
89
+ # @return [Array<Cnccs::Note>]
90
+ def fetch_notes(nts)
91
+ splitter = [227, 128, 130].pack('c*').force_encoding("UTF-8")
92
+ rgxp = /[A-Z]\d{2}(\/\d{2})?$/
93
+ nts.split(splitter).map do |n|
94
+ ccs_code = n.match(rgxp).to_s
95
+ text = n.sub(rgxp, "{ccs-code}")
96
+ Note.new text, ccs_code
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module Cnccs
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cnccs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Classification of Chinese Standards ("CCS") are accessible via this gem.
84
+ email:
85
+ - open.source@ribose.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.adoc
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - cn-ccs-codes/cat-level1.csv
101
+ - cn-ccs-codes/cat-level2.csv
102
+ - cn-ccs-codes/cat-level3.csv
103
+ - cnccs.gemspec
104
+ - lib/cnccs.rb
105
+ - lib/cnccs/ccs.rb
106
+ - lib/cnccs/version.rb
107
+ homepage: https://github.com/riboseinc/cnccs
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.12
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Classification of Chinese Standards ("CCS") are accessible via this gem.
131
+ test_files: []