Skip to content

Instantly share code, notes, and snippets.

@TheNicholasNick
Created May 27, 2009 00:18
Show Gist options
  • Save TheNicholasNick/118383 to your computer and use it in GitHub Desktop.
Save TheNicholasNick/118383 to your computer and use it in GitHub Desktop.
{
"_id": "3cc5dc54e4c47b0a12a57e68b6fdd4f0"
,"_rev": "1-1229578437"
,"drivers": {
"0": {
"rating": 2
,"gender": "M"
,"dob": "01/01/1960"
,"first_name": "John"
,"last_name": "Doe"
}
}
,"created_at": "2009/05/20 12: 20: 17 +0000"
,"vehicle": {
"finance_type": "FI"
,"garaged_postcode": 2065
,"sum_insured": 35990.0
,"make": "Alfa Romeo"
,"use": "P"
,"year": 2008
,"family": "147"
,"agreed_value": true
,"key": "ALFA08AA"
,"category": "F"
}
,"updated_at": "2009/05/20 12: 20: 17 +0000"
,"form_data": {
"vehicle_key_s": "ALFA08AA"
,"vehicle_use_s_rb": "P"
,"qu-options_expressFreight_b_rb": true
,"drivers-0_firstName_s": "John"
,"qu-specialExcess_amount_f": 0.0
,"qu-specialExcess_comments_s": ""
,"qu-risk_lastTenYears_b_rb": false
,"drivers-0_gender_s_rb": "M"
,"drivers-0_rating_i": 2
,"qu-options_doubleExcess_b_rb": true
,"vehicle_garagedPostcode_i": 2065
,"qu_riskType_s_rb": "PRE"
,"qu-options_windscreenReplacement_b_rb": true
,"vehicle_sumInsured_f": 35990.0
,"vehicle_year_i": 2008
,"vehicle_family_s": "147"
,"vehicle_financeType_s_rb": "FI"
,"drivers-0_dob_s": "01/01/1960"
,"qu_underwriter_s": "AMR"
,"qu_transactionDate_s": "20090520"
,"vehicle_category_s": "F"
,"qu-options_protectedNcb_b_rb": true
,"qu_increaseExcessBy_i": 0
,"qu_productType_s": "ALP"
,"vehicle_agreedValue_b_rb": true
,"qu-options_lowKm_b_rb": true
,"qu-risk_lastFiveYears_b_rb": false
,"drivers-0_lastName_s": "Doe"
,"vehicle_make_s": "Alfa Romeo"
,"qu-options_hireCar_b_rb": true
}
,"user_id": 2
,"couchrest-type": "qu"
,"qu": {
"transaction_date": "20090520"
,"risk_type": "PRE"
,"product_type": "ALP"
,"increase_excess_by": 0
,"special_excess": {
"comments": ""
,"amount": 0.0
}
,"risk": {
"last_five_years": false
,"last_ten_years": false
}
,"underwriter": "RWW"
,"options": {
"low_km": true
,"protected_ncb": true
,"hire_car": true
,"express_freight": true
,"windscreen_replacement": true
,"double_excess": true
}
}
}
class Application < Merb::Controller
def parse_params_to_native_types_and_objects
result = {}
params.each do |param|
key = param[0]
value = value_to_native_type(key, param[1])
# split key into parts by _
split_key = key.split(/_/)
# depending on how many parts there are, run individual regex matches
case split_key.size
when 2 # plain field, fieldName_s
# is this a valid param to add to @parsed_params
# tags_s | firstName_s
# id_i | aBigNumber_i
# amount_f | monthlyAmount_i
# functional_b | isGood_b
if key.match(/^[a-z]+[a-zA-Z0-9]+_[sifb]$/)
# working with a valid param
result[compute_key(nil, split_key[0])] = value
end
when 3
if split_key.last.eql?("rb") # plain radio
# is this a valid param to add to @parsed_params
# tags_s_rb | firstName_s_rb
# id_i_rb | aBigNumber_i_rb
# amount_f_rb | monthlyAmount_i_rb
# functional_b_rb | isGood_b_rb
if key.match(/^[a-z]+[a-zA-Z0-9]+_[sifb]_rb$/)
# working with a valid param
result[compute_key(nil, split_key[0])] = value
end
else # object field, object-array-or-hash_fieldName_s
# is this a valid param to add to @parsed_params
# people_tags_s | people_firstName_s
# people-posts_id_i | people-posts_aBigNumber_i
# people-0_amount_f | people-0_monthlyAmount_i
# people-0-employerDetails_valid_b | people-0-employerDetails_areGreat_b
if key.match(/^[a-z]+[a-zA-Z0-9]*(-[a-z]+[a-zA-Z0-9]*|-0|-[1-9][0-9]+)*_[a-z]+[a-zA-Z0-9]+_[sifb]$/)
# working with a valid param
result[compute_key(split_key[0], split_key[1])] = value
end
end
when 4 # object radio, object-array-or-hash_fieldName_s_rb
# is this a valid param to add to @parsed_params
# people_tags_s_rb | people_firstName_s_rb
# people-posts_id_i_rb | people-posts_aBigNumber_i_rb
# people-0_amount_f_rb | people-0_monthlyAmount_i_rb
# people-0-employerDetails_valid_b_rb | people-0-employerDetails_areGreat_b_rb
if key.match(/^[a-z]+[a-zA-Z0-9]*(-[a-z]+[a-zA-Z0-9]*|-0|-[1-9][0-9]+)*_[a-z]+[a-zA-Z0-9]+_[sifb]_rb$/)
# working with a valid param
result[compute_key(split_key[0], split_key[1])] = value
end
end
end
# now that all the params are in the hash - make sub hashes...
result.expand_hash
end
def compute_key(key, name)
split_key = camel_to_underscore(key).split(/-/)
name = camel_to_underscore(name)
[split_key.join('-'), name].join('-')
end
def parse_params_for_form_fields
result = {}
params.each do |param|
key = param[0]
value = value_to_native_type(key, param[1])
if key.match(/^[a-z]+[a-zA-Z0-9]+_[sifb]$/) ||
key.match(/^[a-z]+[a-zA-Z0-9]+_[sifb]_rb$/)||
key.match(/^[a-z]+[a-zA-Z0-9]*(-[a-z]+[a-zA-Z0-9]*|-0|-[1-9][0-9]+)*_[a-z]+[a-zA-Z0-9]+_[sifb]$/) ||
key.match(/^[a-z]+[a-zA-Z0-9]*(-[a-z]+[a-zA-Z0-9]*|-0|-[1-9][0-9]+)*_[a-z]+[a-zA-Z0-9]+_[sifb]_rb$/)
result[key] = value
end
end
result
end
end
Merb::BootLoader.before_app_loads do
# Hash extension for parsing form names into useable hash/object
class Hash
def expand_hash
sub_keys = self.keys.map { |key| key.to_s.match(/^[^-]+/).to_a.first if key.to_s.match(/-/) }.compact.uniq
result = {};
self.each do |key, value|
result[key] = value unless sub_keys.include?( key.to_s.match(/^[^-]*/).to_a.first )
end
sub_keys.each do |name|
hsh = {}
self.each do |key, value|
m = key.to_s.match(/^[^-]*/).to_a.first
if( m.to_s == name.to_s )
hsh[key.to_s.gsub("#{m}-", '').to_sym] = value
end
end
result[name] = hsh.expand_hash()
end
return result;
end
end
end
class RefQu < Application
def create(id)
only_provides :json
parsed_params = parse_params_to_native_types_and_objects
@qu = Qu.new
@qu.qu = parsed_params["qu"]
@qu.vehicle = parsed_params["vehicle"]
@qu.drivers = parsed_params["drivers"]
@qu.form_data = parse_params_for_form_fields
@qu.user_id = id.to_i
if @qu.save
display :success => true, :message => "Qu was successfully created", :id => @qu["_id"], :url => url(:ref_show_qu, @active_ref.id, @qu["_id"])
else
display :success => false, :error => @quote.errors
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment