localized_country_select 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -22,7 +22,7 @@ See http://github.com/rails/country_select/tree/master/lib/country_select.rb
22
22
 
23
23
  In your Gemfile add:
24
24
 
25
- gem 'localized_country_select', '>= 0.9.7'
25
+ gem 'localized_country_select', '>= 0.9.8'
26
26
 
27
27
  == Rails 2.3
28
28
 
@@ -1,3 +1,3 @@
1
1
  module LocalizedCountrySelect
2
- VERSION = "0.9.7"
2
+ VERSION = "0.9.8"
3
3
  end
@@ -14,6 +14,7 @@ require 'active_support/inflector'
14
14
  # LOCALE (required): Sets the locale to use. Output file name will include this.
15
15
  # FORMAT (optional): Output format, either 'rb' or 'yml'. Defaults to 'rb' if not specified.
16
16
  # WEB_LOCALE (optional): Forces a locale code to use when querying the Unicode.org CLDR archive.
17
+ # PARSER (optional): Forces parser to use. Available are nokogiri, hpricot and libxml.
17
18
  #
18
19
  # == Examples
19
20
  # rake import:country_select LOCALE=de
@@ -25,15 +26,8 @@ require 'active_support/inflector'
25
26
 
26
27
  namespace :import do
27
28
 
28
- desc "Import country codes and names for various languages from the Unicode.org CLDR archive. Depends on Hpricot gem."
29
+ desc "Import country codes and names for various languages from the Unicode.org CLDR archive."
29
30
  task :country_select do
30
- begin
31
- require 'hpricot'
32
- rescue LoadError
33
- puts "Error: Hpricot library required to use this task (import:country_select)"
34
- exit
35
- end
36
-
37
31
  # TODO : Implement locale import chooser from CLDR root via Highline
38
32
 
39
33
  # Setup variables
@@ -52,33 +46,20 @@ namespace :import do
52
46
  # ----- Get the CLDR HTML --------------------------------------------------
53
47
  begin
54
48
  puts "... getting the HTML file for locale '#{web_locale}'"
55
- doc = Hpricot( open("http://www.unicode.org/cldr/data/charts/summary/#{web_locale}.html") )
49
+ url = "http://www.unicode.org/cldr/data/charts/summary/#{web_locale}.html"
50
+ html = open(url).read
56
51
  rescue => e
57
52
  puts "[!] Invalid locale name '#{web_locale}'! Not found in CLDR (#{e})"
58
53
  exit 0
59
54
  end
60
55
 
61
56
 
62
- # ----- Parse the HTML with Hpricot ----------------------------------------
63
- puts "... parsing the HTML file"
64
- countries = []
65
- imported_codes = []
66
- doc.search("//tr").each do |row|
67
- n = row.search("td[@class='n']")
68
- g = row.search("td")
69
- if n && n.inner_html =~ /NamesTerritories/ && g.count>=7 && g[4].inner_html =~ /^[A-Z]{2}/
70
- code = g[4].inner_text
71
- code = code[-code.size, 2].to_sym
72
- name = row.search("td[@class='v']").inner_text
73
- unless imported_codes.member?(code)
74
- imported_codes << code
75
- countries << { :code => code, :name => name.to_s }
76
- end
77
- print " ... #{code}: #{name}"
78
- end
79
- end
80
- puts "\n\n... imported countries: #{countries.count}"
81
- puts countries.sort{|a,b| a[:code]<=>b[:code]}.inspect
57
+ set_parser(ENV['PARSER']) if ENV['PARSER']
58
+ puts "... parsing the HTML file using #{parser.name.split("::").last}"
59
+ countries = parser.parse(html).inject([]) { |arr, (code, attrs)| arr << attrs }
60
+ countries.sort_by! { |c| c[:code] }
61
+ puts "\n\n... imported #{countries.count} countries:"
62
+ puts countries.map { |c| "#{c[:code]}: #{c[:name]}" }.join(", ")
82
63
 
83
64
 
84
65
  # ----- Prepare the output format ------------------------------------------
@@ -129,4 +110,113 @@ TAIL
129
110
  # ------------------------------------------------------------------------------
130
111
  end
131
112
 
113
+ module LocalizedCountrySelectTasks
114
+ class Parser
115
+ attr_reader :html
116
+
117
+ def initialize(html)
118
+ @html = html
119
+ end
120
+
121
+ def self.parse(html)
122
+ self.new(html).parse
123
+ end
124
+
125
+ def parse
126
+ raise NotImplementedError, "#parse method need to be implemented in child class!"
127
+ end
128
+ end
129
+
130
+ class NokogiriParser < Parser
131
+ def document
132
+ @document ||= Nokogiri::HTML(html)
133
+ end
134
+
135
+ def parse
136
+ document.search("//tr").inject({}) do |hash, row|
137
+ n = row.search("td[@class='n']")
138
+ g = row.search("td")
139
+ if n.inner_html =~ /NamesTerritories/ && g.count >= 7 && g[4].inner_html =~ /^[A-Z]{2}/
140
+ code = g[4].inner_text
141
+ code = code[-code.size, 2].to_sym
142
+ name = row.search("td[@class='v']").inner_text
143
+
144
+ hash[code] = {:code => code, :name => name.to_s}
145
+ end
146
+ hash
147
+ end
148
+ end
149
+ end
150
+
151
+ class HpricotParser < NokogiriParser
152
+ def document
153
+ @document ||= Hpricot(html)
154
+ end
155
+ end
156
+
157
+ class LibXMLParser < Parser
158
+ def document
159
+ @document ||= LibXML::XML::HTMLParser.string(html, options: LibXML::XML::HTMLParser::Options::RECOVER).parse
160
+ end
161
+
162
+ def parse
163
+ document.find("//tr").inject({}) do |hash, row|
164
+ n = row.find("td[@class='n']")
165
+ g = row.find("td")
166
+ if n.map(&:content).join =~ /NamesTerritories/ && g.count >= 7 && g[4].inner_xml =~ /^[A-Z]{2}/
167
+ code = g[4].content
168
+ code = code[-code.size, 2].to_sym
169
+ name = row.find("td[@class='v']").map(&:content).join
170
+
171
+ hash[code] ||= {:code => code, :name => name.to_s}
172
+ end
173
+ hash
174
+ end
175
+ end
176
+ end
177
+
178
+ REQUIREMENTS_MAP = [
179
+ ['nokogiri', :Nokogiri],
180
+ ['hpricot', :Hpricot],
181
+ ['libxml', :LibXML]
182
+ ]
183
+
184
+ def self.detect_parser
185
+ REQUIREMENTS_MAP.each do |library, klass|
186
+ return const_get(:"#{klass}Parser") if const_defined?(klass)
187
+ end
188
+
189
+ REQUIREMENTS_MAP.each do |library, klass|
190
+ begin
191
+ require library
192
+ return const_get(:"#{klass}Parser")
193
+ rescue LoadError
194
+ end
195
+ end
196
+
197
+ raise StandardError, "One of nokogiri, hpricot or libxml-ruby gem is required! Add \"gem 'nokogiri'\" to your Gemfile to resolve this issue."
198
+ end
199
+ end
200
+
201
+ def parser
202
+ @parser ||= LocalizedCountrySelectTasks.detect_parser
203
+ end
204
+
205
+ def set_parser(arg)
206
+ @parser = begin
207
+ parser = nil
208
+ requirements = LocalizedCountrySelectTasks::REQUIREMENTS_MAP
209
+ found = requirements.detect { |library, _| library == arg }
210
+ raise ArgumentError, "Can't find parser for #{arg}! Supported parsers are: #{requirements.map(&:first).join(", ")}." unless found
211
+ library, klass = found
212
+ begin
213
+ require library
214
+ parser = LocalizedCountrySelectTasks.const_get(:"#{klass}Parser")
215
+ rescue LoadError
216
+ gem_name = library == 'libxml' ? 'libxml-ruby' : library
217
+ raise ArgumentError, "Can't find #{library} library! Add \"gem '#{gem_name}'\" to Gemfile."
218
+ end
219
+ parser
220
+ end
221
+ end
132
222
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localized_country_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2013-11-17 00:00:00.000000000 Z
18
+ date: 2014-03-28 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: actionpack
@@ -33,22 +33,6 @@ dependencies:
33
33
  - - ! '>='
34
34
  - !ruby/object:Gem::Version
35
35
  version: '3.0'
36
- - !ruby/object:Gem::Dependency
37
- name: hpricot
38
- requirement: !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :runtime
45
- prerelease: false
46
- version_requirements: !ruby/object:Gem::Requirement
47
- none: false
48
- requirements:
49
- - - ! '>='
50
- - !ruby/object:Gem::Version
51
- version: '0'
52
36
  - !ruby/object:Gem::Dependency
53
37
  name: rspec
54
38
  requirement: !ruby/object:Gem::Requirement
@@ -107,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
91
  version: '0'
108
92
  requirements: []
109
93
  rubyforge_project:
110
- rubygems_version: 1.8.23
94
+ rubygems_version: 1.8.23.2
111
95
  signing_key:
112
96
  specification_version: 3
113
97
  summary: Localized "country_select" helper with Rake task for downloading locales