geo_redirect 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -89,6 +89,15 @@ If that doesn't suit you, you can customize these when adding `GeoRedirect` to y
89
89
  :config => 'geo_cfg.yml'
90
90
  }
91
91
 
92
+ ### Debugging
93
+
94
+ You can add a `logfile` path string when adding the middleware if you want it to log some of its decision process into the file.
95
+ This is useful when working on your configuration YAML.
96
+
97
+ Rails.application.middleware.use GeoRedirect::Middleware, :logfile => 'log/geo_redirect.log'
98
+
99
+ `GeoRedirect`'s log messages will always be prefixed with `[GeoRedirect]`.
100
+
92
101
  ## Known Issues
93
102
 
94
103
  A couple issues I know about but haven't had the time to fix:
data/lib/geo_redirect.rb CHANGED
@@ -12,6 +12,7 @@ module GeoRedirect
12
12
  # Some defaults
13
13
  options[:db] ||= 'db/GeoIP.dat'
14
14
  options[:config] ||= 'config/geo_redirect.yml'
15
+ @logfile = options[:logfile] || nil
15
16
 
16
17
  @app = app
17
18
 
@@ -38,6 +39,8 @@ module GeoRedirect
38
39
  puts "GeoRedirect middlware."
39
40
  raise e
40
41
  end
42
+
43
+ self.log "Initialized middleware"
41
44
  end
42
45
 
43
46
  def call(env)
@@ -57,6 +60,7 @@ module GeoRedirect
57
60
  def session_exists?
58
61
  host = @request.session['geo_redirect']
59
62
  if host.present? && @config[host].nil? # Invalid var, remove it
63
+ self.log "Invalid session var, forgetting"
60
64
  forget_host
61
65
  host = nil
62
66
  end
@@ -66,6 +70,7 @@ module GeoRedirect
66
70
 
67
71
  def handle_session
68
72
  host = @request.session['geo_redirect']
73
+ self.log "Handling session var: #{host}"
69
74
  redirect_request(host)
70
75
  end
71
76
 
@@ -77,6 +82,7 @@ module GeoRedirect
77
82
  def handle_force
78
83
  url = URI.parse(@request.url)
79
84
  host = host_by_hostname(url.host)
85
+ self.log "Handling force flag: #{host}"
80
86
  remember_host(host)
81
87
  redirect_request(url.host, true)
82
88
  end
@@ -84,15 +90,21 @@ module GeoRedirect
84
90
  def handle_geoip
85
91
  # Fetch country code
86
92
  begin
87
- res = @db.country(@request.env['HTTP_X_FORWARDED_FOR'] || @request.env['REMOTE_ADDR'])
93
+ ip_address = @request.env['HTTP_X_FORWARDED_FOR'] || @request.env['REMOTE_ADDR']
94
+ ip_address = ip_address.split(',').first.chop # take only the first given ip
95
+ self.log "Handling GeoIP lookup: IP #{ip_address}"
96
+ res = @db.country(ip_address)
88
97
  code = res.try(:country_code)
89
98
  country = res.try(:country_code2) unless code.nil? || code.zero?
90
99
  rescue
91
100
  country = nil
92
101
  end
93
102
 
103
+ self.log "GeoIP match: country code #{country}"
104
+
94
105
  unless country.nil?
95
106
  host = host_by_country(country) # desired host
107
+ self.log "GeoIP host match: #{host}"
96
108
  remember_host(host)
97
109
 
98
110
  redirect_request(host)
@@ -119,7 +131,10 @@ module GeoRedirect
119
131
  }.to_param
120
132
  url.query = nil if url.query.empty?
121
133
 
122
- [301, {'Location' => url.to_s, 'Content-Type' => 'text/plain'}, ['Moved Permanently\n']]
134
+ self.log "Redirecting to #{url}"
135
+ [301,
136
+ {'Location' => url.to_s, 'Content-Type' => 'text/plain'},
137
+ ['Moved Permanently\n']]
123
138
  else
124
139
  @app.call(@request.env)
125
140
  end
@@ -136,11 +151,21 @@ module GeoRedirect
136
151
  end
137
152
 
138
153
  def remember_host(host)
154
+ self.log "Remembering: #{host}"
139
155
  @request.session['geo_redirect'] = host
140
156
  end
141
157
 
142
158
  def forget_host
159
+ self.log "Forgetting: #{host}"
143
160
  remember_host(nil)
144
161
  end
162
+
163
+ protected
164
+ def log(message)
165
+ unless @logfile.nil?
166
+ @logger ||= Logger.new(@logfile)
167
+ @logger.debug("[GeoRedirect] #{message}")
168
+ end
169
+ end
145
170
  end
146
171
  end
@@ -1,3 +1,3 @@
1
1
  module GeoRedirect
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_redirect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
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: 2012-11-04 00:00:00.000000000 Z
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: geoip