chinacity 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/chinacity.rb CHANGED
@@ -19,6 +19,14 @@ parser = OptionParser.new do |opts|
19
19
  opts.on('-c', '--csv', 'Output CSV format') do
20
20
  options[:format] = :csv
21
21
  end
22
+
23
+ opts.on('-m', '--minimize', 'Trim spaces to minimize JSON data') do
24
+ options[:minimize] = true
25
+ end
26
+
27
+ opts.on('-H', '--headless', 'Skip outputing head line for CSV') do
28
+ options[:headless] = true
29
+ end
22
30
  end
23
31
 
24
32
  parser.parse!
data/lib/chinacity/cli.rb CHANGED
@@ -3,6 +3,7 @@ require 'nokogiri'
3
3
  require 'chinese_pinyin'
4
4
  require 'json'
5
5
  require 'httparty'
6
+ require 'csv'
6
7
 
7
8
  module Chinacity
8
9
  HOST = 'http://www.stats.gov.cn'
@@ -11,8 +12,10 @@ module Chinacity
11
12
 
12
13
  class Cli
13
14
  def initialize options
14
- @short = options[:short]
15
- @foramt = options[:format]
15
+ @short = options[:short]
16
+ @foramt = options[:format]
17
+ @minimize = options[:minimize]
18
+ @headless = options[:headless]
16
19
  end
17
20
 
18
21
  def start
@@ -21,7 +24,7 @@ module Chinacity
21
24
 
22
25
  add_short_name if @short
23
26
 
24
- output (@foramt == :json ? to_json : to_csv)
27
+ @foramt == :json ? output_json : output_csv
25
28
  end
26
29
 
27
30
  private
@@ -74,34 +77,35 @@ module Chinacity
74
77
  pretty
75
78
  end
76
79
 
77
- def to_json
78
- JSON.pretty_generate(nested_cities)
80
+ def output_json
81
+ if @minimize
82
+ STDOUT.write JSON.generate(nested_cities)
83
+ else
84
+ STDOUT.write JSON.pretty_generate(nested_cities)
85
+ end
79
86
  end
80
87
 
81
- def to_csv
82
- list = @short ? [ "id, 名称, 缩写, 层级" ] : ["id, 名称, 层级"]
83
-
84
- list += @cities.map do |city|
85
- level = if city[:id].end_with? '0000'
86
- 1
87
- elsif city[:id].end_with? '00'
88
- 2
89
- else
90
- 3
88
+ def output_csv
89
+ CSV do |csv_out|
90
+ unless @headless
91
+ list = @short ? ["id", "名称", "缩写", "层级"] : ["id", "名称", "层级"]
92
+ csv_out << list
91
93
  end
92
94
 
93
- if @short
94
- "#{city[:id]}, #{city[:text]}, #{city[:short]}, #{level}"
95
- else
96
- "#{city[:id]}, #{city[:text]}, #{level}"
97
- end
98
- end
95
+ @cities.map do |city|
96
+ level = if city[:id].end_with? '0000'
97
+ 1
98
+ elsif city[:id].end_with? '00'
99
+ 2
100
+ else
101
+ 3
102
+ end
99
103
 
100
- list.join("\n")
101
- end
104
+ list = @short ? [city[:id], city[:text], city[:short], level] : [city[:id], city[:text], level]
102
105
 
103
- def output data
104
- STDOUT.write data.to_s
106
+ csv_out << list
107
+ end
108
+ end
105
109
  end
106
110
  end
107
111
  end
@@ -1,3 +1,3 @@
1
1
  module Chinacity
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chinacity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chinese_pinyin