Last active
August 29, 2015 14:18
-
-
Save dvmonroe/1e404af84bd3dd7d78e9 to your computer and use it in GitHub Desktop.
CA DMV new vehicle registration form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
require 'pry' | |
require 'nokogiri' | |
require 'json' | |
class FeeCheckWorker < Workers | |
def setup_page | |
agent = Mechanize.new | |
# agent.follow_meta_refresh = true | |
agent.user_agent_alias = 'Mac Safari' | |
@page = agent.get('https://www.dmv.ca.gov/FeeCalculatorWeb/newVehicleForm.do') | |
end | |
def form_array | |
form_arr = ["typeLicense", "yearModel", "motivePower", "purchaseMonth", "purchaseDay", "purchaseYear", "purchasePrice", "countyCode", "cityName", "zipCode" ] | |
return form_arr | |
end | |
def start_working(body) | |
params = JSON.parse(body) | |
form_array.each do |e| | |
if params[e].nil? | |
raise StandardError, "Worker: Bad parameters passed from queue" | |
end | |
end | |
session_id = params["session_id"] | |
begin | |
results = registration_search(params) | |
rescue Exception => e | |
puts e | |
record_failure_to_redis(session_id) | |
else | |
record_success_to_redis(results, session_id) | |
end | |
end | |
def registration_search(params) | |
fee_form = @page.form('feeRequestForm') | |
# itterate over form fields | |
form_array.map do |key| | |
fee_form.field_with(name: key).value = params(key) | |
end | |
#submit form | |
new_page = fee_form.submit | |
results = Nokogiri::HTML(new_page.body) | |
return results | |
end | |
def filter_results(results) | |
i = 0 | |
filtered_array = [] | |
while i < results.css("tr").count do | |
arr = results.css("tr")[i].children.map {|p| if !p.children.empty? ; p.children.text end } | |
arr.compact | |
# don't include the junk and/or empty results | |
unless arr.compact.shift.start_with?("\r", "See", "This") || arr.compact.shift.empty? | |
key = arr.compact.shift.split(":")[0].gsub(/\s+/, "_").downcase | |
value = arr.compact.pop | |
filtered_array << { key.to_sym => value } | |
end | |
i +=1 | |
end | |
end | |
private | |
def record_success_to_redis(results, session_id) | |
@redis.hset(session_id, 'fee_check results', filter_results(results).to_json) | |
@redis.hset(session_id, 'fee_check status', 'complete') | |
end | |
def record_failure_to_redis(session_id) | |
@redis.hset(session_id, 'fee check status', 'failed') | |
end | |
end | |
# fee_form.field_with(name: 'typeLicense').value = '11' | |
# fee_form.field_with(name: 'yearModel').value = '2005' | |
# fee_form.field_with(name: 'motivePower').value = 'G' | |
# fee_form.field_with(name: 'purchaseMonth').value = '3' | |
# fee_form.field_with(name: 'purchaseDay').value = '4' | |
# fee_form.field_with(name: 'purchaseYear').value = '2014' | |
# fee_form.field_with(name: 'purchasePrice').value = '10000' | |
# fee_form.field_with(name: 'countyCode').value = '44' | |
# fee_form.field_with(name: 'cityName').value = 'SANTA CRUZ' | |
# fee_form.field_with(name: 'zipCode').value = '95060' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment