Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

try to set country and city via geoip

  • Loading branch information...
commit bd2b8e6781635aeb922b9c8bed67e46bc272857f 1 parent 11d465e
@rkh rkh authored
View
15 app/assets/javascripts/orders.js.coffee
@@ -7,9 +7,17 @@ OrderForm = (form) ->
$(this).val(name) if name != ''
if $("#vat").length > 0
- $('#order_billing_address_attributes_country').change -> _this.setupVAT $(this).val()
- $('#order_add_vat').change -> $('#vat_note').toggle($(this).val() == "true")
- _this.setupVAT $('#order_billing_address_attributes_country').val()
+ country = $('#order_billing_address_attributes_country')
+ city = $('#order_billing_address_attributes_city')
+
+ $.getJSON '/geo_ip.json', (data) ->
+ city.val(data.city) if city.val() == ""
+ country.val(data.country_name) if country.val() == ""
+ _this.setupVAT country.val()
+
+ country.change -> _this.setupVAT country.val()
+ $('#order_add_vat').change -> $('#vat_note').toggle($(this).val() == "true")
+ _this.setupVAT country.val()
form.submit ->
if $('#order_card_name').length
@@ -83,4 +91,3 @@ $(document).ready ->
delayIn: 100
title: ->
$(this).parent().find('.hint').html()
-
View
5 config.ru
@@ -1,4 +1,7 @@
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
-run Travis::Application
+
+#require 'geo_ip'
+map('/') { run Travis::Application }
+#map('/geo_ip.json') { run GeoIP.new }
View
3  config/routes.rb
@@ -1,3 +1,5 @@
+require 'geo_ip'
+
Travis::Application.routes.draw do
root to: 'home#show'
match 'company_packages', to: 'home#sponsoring_plans'
@@ -7,6 +9,7 @@
match 'packages/:package', as: :new_package, to: 'orders#new'
match 'subscriptions/:package', as: :new_subscription, to: 'orders#new', subscription: true
+ match 'geo_ip.json', to: GeoIP.new
resources :orders, except: :new do
get 'confirm', on: :member
View
27 lib/geo_ip.rb
@@ -0,0 +1,27 @@
+require 'net/http'
+require 'timeout'
+
+class GeoIP
+ def initialize(geoip_host = 'freegeoip.net')
+ @geoip_host = geoip_host
+ @cache = {}
+ end
+
+ def call(env, cb = env['async.callback'])
+ ip = Rack::Request.new(env).ip
+
+ @cache.clear if @cache.size > 1024 # magic values ftw!
+ return @cache[ip] if @cache.include? ip
+
+ if cb
+ EM.defer { cb.call call(env, false) }
+ return [-1, {}, []]
+ end
+
+
+ body = Timeout.timeout(5) { Net::HTTP.get(@geoip_host, "/json/#{ip}") }
+ @cache[ip] = Rack::Response.new(body, 200, "Content-Type" => "application/json").finish
+ rescue Timeout::Error
+ [504, {"Content-Type" => "text/plain"}, ["GeoIP server did not respond properly."]]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.