geo-lite-city 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/geo.rb +26 -14
  2. data/lib/geo/etest.rb +26 -0
  3. data/lib/geo/version.rb +1 -1
  4. metadata +19 -3
data/lib/geo.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "geoip"
2
+ require "ensure/encoding"
2
3
 
3
4
  module Geo
4
5
  end
@@ -23,9 +24,24 @@ module Geo
23
24
 
24
25
  private
25
26
 
27
+ def self.adjust_encoding(rec)
28
+ rec.inject({}) do |hsh, (key, value)|
29
+ if value.is_a?(String)
30
+ value = value.ensure_encoding('UTF-8',
31
+ :external_encoding => [Encoding::ISO8859_1, Encoding::UTF_8],
32
+ :invalid_characters => :transcode
33
+ )
34
+ end
35
+
36
+ hsh.update key => value
37
+ end
38
+ end
39
+
26
40
  def self.complete_city(city)
27
41
  city = city.to_hash
28
42
 
43
+ city = adjust_encoding(city)
44
+
29
45
  parts = city.values_at :city_name, :region_name, :country_name
30
46
  city_name = parts.grep(/[a-zA-Z]/).join(", ")
31
47
 
@@ -40,24 +56,20 @@ module Geo
40
56
 
41
57
  public
42
58
 
43
- def self.cities(s, &block)
44
- expect! s => [ Regexp, /../ ]
59
+ def self.cities(pattern=nil, &block)
60
+ expect! pattern => [ nil, Regexp, String ]
45
61
 
46
- s = Regexp.compile("^" + Regexp.escape(s) + "$") unless s.is_a?(Regexp)
47
-
48
- quietly do
49
- r = []
62
+ pattern = Regexp.compile("^" + Regexp.escape(pattern) + "$") if pattern.is_a?(String)
50
63
 
51
- instance.each.select do |rec|
52
- next if rec.city_name !~ s
64
+ ary = []
65
+ collect = block || ary.method(:push)
53
66
 
54
- rec = complete_city(rec)
55
- yield rec if block
56
- r.push rec
57
- end
58
-
59
- r
67
+ instance.each do |rec|
68
+ next if pattern && rec.city_name !~ pattern
69
+ collect.call complete_city(rec)
60
70
  end
71
+
72
+ block ? self : ary
61
73
  end
62
74
 
63
75
  private
@@ -64,5 +64,31 @@ module Geo::Etest
64
64
  :timezone => "Europe/Berlin"
65
65
  }
66
66
  end
67
+
68
+ def test_geoip_cities_callback
69
+ r = nil
70
+ Geo.cities do |rec|
71
+ r = rec
72
+ break
73
+ end
74
+
75
+ assert_equal r, :area_code => nil,
76
+ :city_name => "",
77
+ :continent_code => "AS",
78
+ :country_code => "AS.JP",
79
+ :country_code2 => "JP",
80
+ :country_code3 => "JPN",
81
+ :country_name => "Japan",
82
+ :dma_code => nil,
83
+ :ip => "",
84
+ :latitude => 36.0,
85
+ :longitude => 138.0,
86
+ :name => "Japan",
87
+ :postal_code => "",
88
+ :region_code => "AS.JP",
89
+ :region_name => "",
90
+ :request => "",
91
+ :timezone => "Asia/Tokyo"
92
+ end
67
93
  end
68
94
 
@@ -1,3 +1,3 @@
1
1
  module Geo
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo-lite-city
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-22 00:00:00.000000000 Z
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: geoip
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: ensure-encoding
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  description: Sometimes you just need SQL.
31
47
  email: eno@radiospiel.org
32
48
  executables: []
@@ -55,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
71
  version: '0'
56
72
  segments:
57
73
  - 0
58
- hash: 2540758688009425679
74
+ hash: 4243812494533547067
59
75
  required_rubygems_version: !ruby/object:Gem::Requirement
60
76
  none: false
61
77
  requirements: