Skip to content

Commit

Permalink
Merge 523fd4f into 47d6b67
Browse files Browse the repository at this point in the history
  • Loading branch information
wolflee committed Sep 18, 2017
2 parents 47d6b67 + 523fd4f commit e2e2ee0
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 36 deletions.
35 changes: 17 additions & 18 deletions GB2260.gemspec
@@ -1,34 +1,33 @@
# coding: utf-8
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'GB2260/version'

Gem::Specification.new do |spec|
spec.name = "GB2260"
spec.name = 'GB2260'
spec.version = GB2260::VERSION
spec.authors = ["WolfLee"]
spec.email = ["liyuan0228@gmail.com"]
spec.authors = ['WolfLee']
spec.email = ['me@wolfl.ee']

spec.summary = %q{The Ruby implementation for looking up the Chinese administrative divisions.}
spec.homepage = "https://github.com/wolflee/GB2260.rb"
spec.license = "MIT"
spec.summary = 'The Ruby implementation for looking up the Chinese administrative divisions.'
spec.homepage = 'https://github.com/wolflee/GB2260.rb'
spec.license = 'MIT'

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "https://rubygems.org"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'https://rubygems.org'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.files << Dir["data/*.txt"]
spec.bindir = "exe"
spec.files << Dir['data/**/*.tsv']
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "coveralls"
spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.6'
spec.add_development_dependency 'coveralls'
end
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -33,7 +33,7 @@ The way to look up a administrative division by its GB2260 code is the basic int
gb2260 = GB2260.new

division = gb2260.get(360426)
puts division # => <GB2260-2014 江西省/九江市/德安县>
puts division # => <GB2260-201607 360426 江西省/九江市/德安县>
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion data
Submodule data updated 101 files
2 changes: 1 addition & 1 deletion lib/GB2260.rb
Expand Up @@ -10,7 +10,7 @@ def self.revisions
end

def initialize(revision=nil)
@revision = (revision || LATEST_REVISION).to_s
@revision = (revision || DEFAULT_REVISION).to_s
end

def get(code)
Expand Down
3 changes: 2 additions & 1 deletion lib/GB2260/constants.rb
@@ -1,5 +1,6 @@
class GB2260
LATEST_REVISION = '2014'.freeze
DEFAULT_REVISION = 'stats/201607'.freeze
PROVINCE_SUFFIX = '0000'.freeze
PREFECTURE_SUFFIX = '00'.freeze
NAMESPACE_SEPARATOR = '/'.freeze
end
22 changes: 16 additions & 6 deletions lib/GB2260/data.rb
@@ -1,3 +1,6 @@
require 'pathname'
require 'csv'

class GB2260
class Data
GEM_DIR = File.join(File.dirname(__FILE__), '../../').freeze
Expand All @@ -10,7 +13,7 @@ def data
end

def search(code, revision=nil)
revision ||= LATEST_REVISION
revision ||= DEFAULT_REVISION
data[revision.to_s][code.to_s]
end

Expand All @@ -21,21 +24,28 @@ def fetch_data(dir)
end

def array_data(dir)
Dir["data/*.txt"].map do |fn|
[strip_revision(fn), Hash[per_revision_data(real_path(dir, fn))]]
data_files.map do |fn|
[revision_from(fn).to_s, Hash[per_revision_data(real_path(dir, fn))]]
end
end

def data_files
Dir['data/**/*.tsv'].reject { |fn| fn =~ /sources/ }
end

def per_revision_data(filepath)
File.readlines(filepath).map {|l| l.chomp.split("\t") }
CSV.readlines(filepath, col_sep: "\t", headers: true, header_converters: :symbol)
.map { |div| [div[:code], div[:name]] }
end

def real_path(dir, filename)
File.expand_path(File.join(dir, filename))
end

def strip_revision(filename)
filename.sub(/data\/GB2260-/, '').sub(/\.txt$/, '')
def revision_from(filename)
(Pathname(File.dirname(filename)).each_filename.to_a[1..-1] +
[File.basename(filename, '.tsv')])
.join(NAMESPACE_SEPARATOR)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/GB2260/division.rb
Expand Up @@ -17,7 +17,7 @@ def self.batch(codes, revision=nil)
def initialize(code, name, revision=nil)
@code = code.to_s
@name = name.to_s
@revision = (revision || LATEST_REVISION).to_s
@revision = (revision || DEFAULT_REVISION).to_s
end

def ==(other)
Expand All @@ -33,7 +33,7 @@ def description(separator = '/')
end

def to_s
"<GB2260-#{revision} #{code} #{description}>"
"<GB2260-#{revision.split(NAMESPACE_SEPARATOR)[-1]} #{code} #{description}>"
end

def hash
Expand Down
1 change: 0 additions & 1 deletion spec/GB2260_spec.rb
Expand Up @@ -8,7 +8,6 @@
describe ".revisions" do
it 'returns all revisions' do
expect(GB2260.revisions).to be_instance_of(Array)
expect(GB2260.revisions).to start_with(GB2260::LATEST_REVISION)
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/division_spec.rb
Expand Up @@ -109,9 +109,9 @@

describe '#to_s' do
it 'returns a human readable description' do
expect("#{beijing}").to eq '<GB2260-2014 110000 北京市>'
expect("#{bj_city}").to eq '<GB2260-2014 110100 北京市/市辖区>'
expect("#{dc_dist}").to eq '<GB2260-2014 110101 北京市/市辖区/东城区>'
expect("#{beijing}").to match /<GB2260-\d+ 110000 北京市>/
expect("#{bj_city}").to match /<GB2260-\d+ 110100 北京市\/市辖区>/
expect("#{dc_dist}").to match /<GB2260-\d+ 110101 北京市\/市辖区\/东城区>/
end
end

Expand All @@ -122,13 +122,13 @@
end

it 'uses lastest revision as default' do
expect(GB2260::Division.new(110000, '北京市').revision).to eq(GB2260::LATEST_REVISION)
expect(GB2260::Division.new(110000, '北京市').revision).to eq(GB2260::DEFAULT_REVISION)
end
end

context 'when an earlier @revision is given' do
it 'does not equal to latest division' do
expect(GB2260::Division.get(110101)).to_not eq GB2260::Division.get(110101, 2004)
expect(GB2260::Division.get(110101)).to_not eq GB2260::Division.get(110101, 200212)
end
end

Expand Down

0 comments on commit e2e2ee0

Please sign in to comment.