Skip to content
Browse files

return the fb_user's name with matches

  • Loading branch information...
1 parent 23c2849 commit 2a9455aa596beeeeca2d958fd2fc0ea27e747369 @dhruvbansal dhruvbansal committed Sep 5, 2012
Showing with 27 additions and 9 deletions.
  1. +14 −2 README.rdoc
  2. +13 −7 innovation_matcher.rb
View
16 README.rdoc
@@ -64,9 +64,12 @@ Returns an array of innovations matching given parameters +dob+ and
+hometown+. Also returns a +match_id+ that can be used to retrieve
this match later.
+If the +name+ parameter is given, that name will be returned when
+asking for the match later.
+
Example:
- $ curl -s -X POST http://localhost:4567/matches -d '{"dob": "1983-05-23", "hometown": "New York, NY"}'
+ $ curl -s -X POST http://localhost:4567/matches -d '{"dob": "1983-05-23", "hometown": "New York, NY", "name": "Dhruv Bansal"}'
{
"match_id": "6b73f010-d469-012f-ad21-002219342852",
"innovations": [
@@ -81,13 +84,21 @@ Example:
},
{ ... },
...
- ]
+ ],
+ "user_data": {
+ "dob": "1983-05-23",
+ "hometown": "New York, NY",
+ "name": "Dhruv Bansal"
+ }
}
== Retrieve a match
Returns an array of innovations from a previously returned +match_id+.
+If a +name+ was passed in with the original match, it will be
+available as the +fb_user+ value in the returned +innovation+ object.
+
Example:
$ curl -s -X GET http://localhost:4567/matches/6b73f010-d469-012f-ad21-002219342852
@@ -102,6 +113,7 @@ Example:
"innovator_name": "NICOLAUS OTTO",
"innovation_date": "8/14/1877",
...
+ "fb_user": "Dhruv Bansal"
},
{ ... },
...
View
20 innovation_matcher.rb
@@ -14,9 +14,13 @@ def db
end
def find_match user_data
- dob = user_data["dob"]
+ dob = (user_data["dob"] or return)
[].tap do |innovations|
- result_set = db.query("SELECT * FROM innovations ORDER BY ABS(innovation_date - DATE(\"#{Mysql.quote(dob)}\")) LIMIT 3")
+ begin
+ result_set = db.query("SELECT * FROM innovations ORDER BY ABS(innovation_date - DATE(\"#{Mysql.quote(dob)}\")) LIMIT 3")
+ rescue Mysql::Error => e
+ return
+ end
innovation = utf8ize(result_set.fetch_hash())
while innovation
innovations << innovation
@@ -35,9 +39,10 @@ def utf8ize hash
end
def save_match user_data, innovations
+ fb_user = user_data.fetch("name", '')
uuid = UUID.generate
innovations.each do |innovation|
- db.query("INSERT INTO matches (match_id, innovation_id) VALUES (\"#{uuid}\", #{innovation['id']})")
+ db.query("INSERT INTO matches (match_id, innovation_id, fb_user) VALUES (\"#{uuid}\", #{innovation['id']}, \"#{Mysql.quote(fb_user)}\")")
end
{
"match_id" => uuid,
@@ -47,7 +52,7 @@ def save_match user_data, innovations
end
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)}\"")
+ result_set = db.query("SELECT i.*, m.fb_user FROM matches m, innovations i WHERE m.innovation_id = i.id AND m.match_id = \"#{Mysql.quote(uuid)}\"")
innovations = []
innovation = utf8ize(result_set.fetch_hash)
while innovation
@@ -61,7 +66,6 @@ def retrieve_match uuid
end
def pretty_json obj
-
obj.to_json + "\n"
end
@@ -90,8 +94,10 @@ def pretty_json obj
end
return 400 unless user_data.is_a?(Hash) && user_data["dob"]
+ match = find_match(user_data)
+ return 400 unless match
- pretty_json(save_match(user_data, find_match(user_data)))
+ pretty_json(save_match(user_data, match))
end
get "/matches/:uuid" do
@@ -100,7 +106,7 @@ def pretty_json obj
end
error 400 do
- pretty_json({:status => "error", :code => 400, :message => "The POST body must contain a valid JSON hash containing the key 'dob'"})
+ pretty_json({:status => "error", :code => 400, :message => "The POST body must contain a valid JSON hash with a key 'dob' mapped to a valid date in a MySQL parseable format like '2012-01-01'."})
end
error 404 do

0 comments on commit 2a9455a

Please sign in to comment.
Something went wrong with that request. Please try again.