* still working on profile page (VRFS-313), and also fixing some max_mind tests and manager to account for model addition

This commit is contained in:
Seth Call 2013-05-16 22:54:33 -05:00
parent c12c8515d4
commit 4633f0fa08
8 changed files with 126 additions and 61 deletions

View File

@ -48,6 +48,7 @@ gem 'aasm', '3.0.16'
gem 'carrierwave'
gem 'fog'
gem 'devise', '>= 1.1.2'
gem 'postgres-copy'
#group :libv8 do
# gem 'libv8', "~> 3.11.8"
#end

View File

@ -5,6 +5,7 @@
context.JK = context.JK || {};
context.JK.AccountProfileScreen = function(app) {
var logger = context.JK.logger;
var api = context.JK.API(app);
var userId;
var user = {};
@ -35,17 +36,22 @@
});
}
function populateAccountProfile(userDetail) {
function populateAccountProfile(userDetail, instruments, isps, regions, cities) {
var template = context.JK.fillTemplate($('#template-account-profile').html(), {
country: userDetail.country,
state: userDetail.state,
first_name: userDetail.first_name,
last_name: userDetail.last_name,
instruments : userDetail.instruments,
user_instruments: userDetail.instruments,
birth_date : userDetail.birth_date,
isp : userDetail.isp,
gender: userDetail.gender
gender: userDetail.gender,
instruments: instruments,
cities: cities["cities"],
regions: regions["regions"],
isps: isps["isps"]
});
$('#account-profile-content-scroller').html(template);
}
@ -59,10 +65,22 @@
}
function renderAccountProfile() {
getUserDetail()
.done(populateAccountProfile)
.error(app.ajaxError)
$.when( api.getUserDetail(),
api.getInstruments())
.done(function(userDetail, instruments) {
// now that we have the user's country and region, we can query further
$.when(
api.getIsps( { country: userDetail.country } ),
api.getRegions( { country: userDetail.country } ),
api.getCities( { country: userDetail.country, region: userDetail.state }))
.done(function(isps, regions, cities) {
populateAccountProfile(userDetail, instruments, isps, regions, cities)
})
.fail(app.ajaxError)
})
.fail(app.ajaxError);
}
function navToAccount() {

View File

@ -0,0 +1,84 @@
(function(context,$) {
/**
* Javascript wrappers for the REST API
*/
"use strict";
context.JK = context.JK || {};
context.JK.API = function(app) {
var self = this;
var logger = context.JK.logger;
function getUserDetail(options) {
var id = options && options["id"]
if(!id) {
id = context.JK.currentUserId;
}
var url = "/api/users/" + id;
return $.ajax({
type: "GET",
dataType: "json",
url: url,
async: true,
processData: false
});
}
function getCities (options) {
var country = options['country']
var region = options['region']
return $.ajax('/api/cities', {
data : { country: country, region: region },
dataType : 'json'
});
}
function getRegions(options) {
var country = options["country"]
return $.ajax('/api/regions', {
data : { country: country},
dataType : 'json'
});
}
function getIsps(options) {
var country = options["country"]
return $.ajax('/api/isps', {
data : { country: country},
dataType : 'json'
});
}
function getInstruments(options) {
return $.ajax('/api/isps', {
data : { },
dataType : 'json'
});
}
function initialize() {
return self;
}
// Expose publics
this.initialize = initialize;
this.getUserDetail = getUserDetail;
this.getCities = getCities;
this.getRegions = getRegions;
this.getIsps = getIsps;
this.getInstruments = getInstruments
return this;
};
})(window,jQuery);

View File

@ -7,7 +7,7 @@ class ApiMaxmindRequestsController < ApiController
if regions && regions.length > 0
render :json => { :regions => regions }, :status => 200
else
render :json => { :message => "Unrecognized Country" }, :status => 404
render :json => { :message => "Unrecognized Country" }, :status => 422
end
end
@ -16,7 +16,7 @@ class ApiMaxmindRequestsController < ApiController
if cities && cities.length > 0
render :json => { :cities => cities }, :status => 200
else
render :json => { :message => "Unrecognzied country or region" }, :status => 404
render :json => { :message => "Unrecognzied country or region" }, :status => 422
end
end
@ -25,7 +25,7 @@ class ApiMaxmindRequestsController < ApiController
if isps && isps.length > 0
render :json => { :isps => isps }, :status => 200
else
render :json => { :message => "Unrecognzied Country" }, :status => 404
render :json => { :message => "Unrecognzied Country" }, :status => 422
end
end

View File

@ -83,47 +83,14 @@ class MaxMindManager < BaseManager
def self.isps(country)
# hunts through all isps, and keeping the ones that have a ip_bottom or ip_top within the range of a country's IPs
ActiveRecord::Base.connection_pool.with_connection do |connection|
pg_conn = connection.instance_variable_get("@connection")
pg_conn.exec("SELECT DISTINCT isp FROM max_mind_isp INNER JOIN max_mind_geo
ON (max_mind_isp.ip_bottom <= max_mind_geo.ip_top AND max_mind_isp.ip_bottom >= max_mind_geo.ip_bottom) OR (max_mind_isp.ip_top <= max_mind_geo.ip_top AND max_mind_isp.ip_top >= max_mind_geo.ip_bottom)
WHERE country = $1
ORDER BY isp ASC", [country]).map do |tuple|
pg_conn.exec("SELECT DISTINCT isp FROM max_mind_isp WHERE country = $1 ORDER BY isp ASC", [country]).map do |tuple|
tuple["country"]
end
end
end
# Do this in a transaction so there's no partial import.
def import_database(filename)
clear_location_table
# Format:
# startIpNum,endIpNum,country,region,city,postalCode,latitude,longitude,dmaCode,areaCode
it = 0
CSV.foreach(filename, col_sep: ",", encoding: "ISO-8859-1") do |split_line|
next if split_line[0] == 'startIpNum' # Ignore the legend line at the top
it += 1
if it > 100000
return
end
unless split_line.length < 10
@pg_conn.exec("INSERT INTO max_mind_geo (ip_bottom, ip_top, country, region, city) VALUES ($1, $2, $3, $4, $5)",
[
self.class.ip_address_to_int(split_line[0]),
self.class.ip_address_to_int(split_line[1]),
split_line[2],
split_line[3],
split_line[4]
]).clear
end
end
end
# Note that there's one big country, and then two cities in each region.
def create_phony_database()
clear_location_table
@ -132,7 +99,7 @@ class MaxMindManager < BaseManager
[
self.class.ip_address_to_int("#{top_octet}.0.0.0"),
self.class.ip_address_to_int("#{top_octet}.255.255.255"),
"United States",
"US",
"Region #{(top_octet / 2).floor}",
"City #{top_octet}"
]).clear
@ -140,11 +107,12 @@ class MaxMindManager < BaseManager
clear_isp_table
(0..255).each do |top_octet|
@pg_conn.exec("INSERT INTO max_mind_isp (ip_bottom, ip_top, isp) VALUES ($1, $2, $3)",
@pg_conn.exec("INSERT INTO max_mind_isp (ip_bottom, ip_top, isp, country) VALUES ($1, $2, $3, $4)",
[
self.class.ip_address_to_int("#{top_octet}.0.0.0"),
self.class.ip_address_to_int("#{top_octet}.255.255.255"),
"ISP #{top_octet}",
"US"
]).clear
end

View File

@ -1,18 +1,12 @@
namespace :db do
desc "Import a maxmind database; run like this: rake db:import_maxmind file=<path_to_file>"
desc "Import a maxmind database; run like this: rake db:import_maxmind_geo file=<path_to_file>"
task import_maxmind_geo: :environment do
require 'csv'
MaxMindManager.active_record_transaction do |manager|
manager.import_geo_database ENV['file']
end
MaxMindGeo.import_from_max_mind ENV['file']
end
desc "Import a maxmind isp database; run like this: rake db:import_maxmind_isp file=<path_to_file>"
task import_maxmind_isp: :environment do
require 'csv'
MaxMindManager.active_record_transaction do |manager|
manager.import_isp_database ENV['file']
end
MaxMindIsp.import_from_max_mind ENV['file']
end
desc "Create a fake set of maxmind data"

View File

@ -11,14 +11,14 @@ describe MaxMindManager do
end
it "looks up regions successfully" do
regions = MaxMindManager.regions("United States")
regions = MaxMindManager.regions("US")
regions.length.should == 128
regions.first.should == "Region 0"
regions.last.should == "Region 99" # Based on sort order this will be the top.
end
it "looks up cities successfully" do
cities = MaxMindManager.cities("United States", "Region 1")
cities = MaxMindManager.cities("US", "Region 1")
cities.length.should == 2
cities.first.should == "City 2"
cities.last.should == "City 3"
@ -30,8 +30,8 @@ describe MaxMindManager do
end
it "looks up isp-by-country succesfully" do
isps = MaxMindManager.isps("United States")
isps.length.should == 256 # because the phone_database method creates 256 isps, all in United States
isps = MaxMindManager.isps("US")
isps.length.should == 256 # because the phone_database method creates 256 isps, all in US
end
end

View File

@ -7,7 +7,7 @@ describe UserManager do
before(:each) do
@user_manager = UserManager.new(:conn => @conn)
UserMailer.deliveries.clear
@location = { :country => "United States", :state => "Arkansas", :city => "Little Rock" }
@location = { :country => "US", :state => "Arkansas", :city => "Little Rock" }
@instruments = [ { :instrument_id=>"electric guitar", :proficiency_level => '1', :priority=>0 }]
end
@ -58,7 +58,7 @@ describe UserManager do
@user.errors.any?.should be_false
@user.city.should == 'City 127'
@user.state.should == 'Region 63'
@user.country.should == 'United States'
@user.country.should == 'US'
end
it "accepts location if specified" do
@ -70,7 +70,7 @@ describe UserManager do
@user.errors.any?.should be_false
@user.city.should == 'Little Rock'
@user.state.should == 'Arkansas'
@user.country.should == 'United States'
@user.country.should == 'US'
end
it "accepts a nil location, if specified" do