Permalink
Browse files

all working except the matching algorithm

  • Loading branch information...
Dhruv Bansal
Dhruv Bansal committed Aug 30, 2012
1 parent 73dc1d7 commit b2d48eef0276ff6ba6d66a78aa25307029f3a03b
Showing with 68 additions and 16 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 Gemfile
  3. +8 −2 Gemfile.lock
  4. +1 −0 Procfile
  5. 0 config.ru
  6. +49 −13 innovation_matcher.rb
  7. +7 −0 settings.example.yml
View
@@ -0,0 +1,2 @@
+settings.yml
+db/*
View
@@ -4,4 +4,4 @@ gem "sinatra"
gem "unicorn"
gem "json"
gem "mysql"
-
+gem "uuid"
View
@@ -3,20 +3,25 @@ GEM
specs:
json (1.7.5)
kgio (2.7.4)
+ macaddr (1.5.0)
+ systemu (>= 2.4.0)
mysql (2.8.1)
rack (1.4.1)
rack-protection (1.2.0)
rack
raindrops (0.10.0)
sinatra (1.3.3)
- rack (>= 1.3.6, ~> 1.3)
+ rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
- tilt (>= 1.3.3, ~> 1.3)
+ tilt (~> 1.3, >= 1.3.3)
+ systemu (2.5.2)
tilt (1.3.3)
unicorn (4.3.1)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
+ uuid (2.3.5)
+ macaddr (~> 1.0)
PLATFORMS
ruby
@@ -26,3 +31,4 @@ DEPENDENCIES
mysql
sinatra
unicorn
+ uuid
View
@@ -0,0 +1 @@
+web: shotgun -p 3000 -o 0.0.0.0 innovation_matcher.rb
View
No changes.
View
@@ -2,29 +2,52 @@
require 'json'
require 'mysql'
require 'time'
+require 'yaml'
+require 'uuid'
+
+SETTINGS = YAML.load(File.read(File.expand_path('../settings.yml', __FILE__)))
set :show_exceptions, false
def db
- @db ||= Mysql.connect(ENV["MYSQL_HOST"], ENV["MYSQL_USER"], ENV["MYSQL_PASSWORD"], ENV["MYSQL_DATABASE"])
+ @db ||= Mysql.connect(SETTINGS["mysql"]["host"], SETTINGS["mysql"]["username"], SETTINGS["mysql"]["password"], SETTINGS["mysql"]["database"])
end
def find_match dob
- # { "id" => 11, "innovation" => "motion picture projector", "innovator" => "Fred H. Meyer",
- # "date" => Time.parse("1972-09-04"), "patent_number" => "US3642357",
- # "link_to_patent" => "http://www.google.com/patents/US3642357",
- # "notes" => "This motion picture viewer projected movies onto a screen by cycling the film forward or backward at a set speed."
- # }
- db.query("SELECT * FROM innovations LIMIT 3")
+ [].tap do |innovations|
+ result_set = db.query("SELECT * FROM innovations LIMIT 3")
+ innovation = result_set.fetch_hash()
+ while innovation
+ innovations << innovation
+ innovation = result_set.fetch_hash()
+ end
+ end
end
def save_match user_data, innovations
- innovations
- # {"id" => 123}.merge("innovation" => innovation, "user_data" => user_data)
+ uuid = UUID.generate
+ innovations.each do |innovation|
+ db.query("INSERT INTO matches (match_id, innovation_id) VALUES (\"#{uuid}\", #{innovation['id']})")
+ end
+ {
+ "match_id" => uuid,
+ "innovations" => innovations,
+ "user_data" => user_data
+ }
end
-def retrieve_match id
- db.query("SELECT * FROM matches WHERE match_id=#{id.to_i}")
+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
+ while innovation
+ innovations << innovation
+ innovation = result_set.fetch_hash
+ end
+ {
+ "match_id" => uuid,
+ "innovations" => innovations
+ }
end
def pretty_json obj
@@ -36,6 +59,18 @@ def pretty_json obj
content_type "application/json"
end
+get "/matches" do
+ begin
+ user_data = JSON.parse(request.body.read)
+ rescue JSON::ParserError => e
+ return 400
+ end
+
+ return 400 unless user_data.is_a?(Hash) && user_data["dob"]
+
+ pretty_json(find_match(user_data))
+end
+
post "/matches" do
begin
user_data = JSON.parse(request.body.read)
@@ -48,8 +83,9 @@ def pretty_json obj
pretty_json(save_match(user_data, find_match(user_data)))
end
-get "/matches/:id" do
- pretty_json(retrieve_match(request.params[:id]))
+get "/matches/:uuid" do
+ uuid = params["uuid"]
+ pretty_json(retrieve_match(uuid)
end
error 400 do
View
@@ -0,0 +1,7 @@
+---
+
+mysql:
+ host: localhost
+ username: ~
+ password: ~
+ database: nissan_garage_connect

0 comments on commit b2d48ee

Please sign in to comment.