Permalink
Browse files

matching algorithm uses the date to find nearby innovations

  • Loading branch information...
Dhruv Bansal
Dhruv Bansal committed Sep 4, 2012
1 parent 8f00be6 commit 23c2849f187b6a07e00ae9634278dd33e144ca79
Showing with 17 additions and 6 deletions.
  1. +17 −6 innovation_matcher.rb
View
@@ -13,17 +13,27 @@ def db
@db ||= Mysql.connect(SETTINGS["mysql"]["host"], SETTINGS["mysql"]["username"], SETTINGS["mysql"]["password"], SETTINGS["mysql"]["database"])
end
-def find_match dob
+def find_match user_data
+ dob = user_data["dob"]
[].tap do |innovations|
- result_set = db.query("SELECT * FROM innovations LIMIT 3")
- innovation = result_set.fetch_hash()
+ result_set = db.query("SELECT * FROM innovations ORDER BY ABS(innovation_date - DATE(\"#{Mysql.quote(dob)}\")) LIMIT 3")
+ innovation = utf8ize(result_set.fetch_hash())
while innovation
innovations << innovation
- innovation = result_set.fetch_hash()
+ innovation = utf8ize(result_set.fetch_hash())
end
end
end
+def utf8ize hash
+ return hash unless hash
+ hash.keys.each do |key|
+ val = hash[key]
+ hash[key] = val.force_encoding('utf-8')
+ end
+ hash
+end
+
def save_match user_data, innovations
uuid = UUID.generate
innovations.each do |innovation|
@@ -39,10 +49,10 @@ def save_match user_data, innovations
def retrieve_match uuid
result_set = db.query("SELECT i.* FROM matches m, innovations i WHERE m.innovation_id = i.id AND m.match_id = \"#{Mysql.quote(uuid)}\"")
innovations = []
- innovation = result_set.fetch_hash
+ innovation = utf8ize(result_set.fetch_hash)
while innovation
innovations << innovation
- innovation = result_set.fetch_hash
+ innovation = utf8ize(result_set.fetch_hash)
end
{
"match_id" => uuid,
@@ -51,6 +61,7 @@ def retrieve_match uuid
end
def pretty_json obj
+
obj.to_json + "\n"
end

0 comments on commit 23c2849

Please sign in to comment.