From 95b61bc444f71139cb3367bc56aa6b2286add2e0 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:04:23 -0600 Subject: [PATCH 1/6] initial commit --- Gemfile | 1 + Gemfile.lock | 10 ++++++++-- server.rb | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 20e422c9..66526d43 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ ruby '2.0.0' gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' +gem 'rest-client' # Testing gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 92b9a02a..39e03203 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,9 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + mime-types (2.4.3) multi_json (1.10.1) + netrc (0.10.0) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -24,6 +26,9 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -51,6 +56,7 @@ PLATFORMS DEPENDENCIES pry-byebug + rest-client rspec (~> 2.14.1) - sinatra - sinatra-contrib + sinatra (~> 1.4.5) + sinatra-contrib (~> 1.4.2) diff --git a/server.rb b/server.rb index 9ae90b34..70433179 100644 --- a/server.rb +++ b/server.rb @@ -2,6 +2,7 @@ require 'sinatra/reloader' require 'rest-client' +set :bind, '0.0.0.0' # # # This is our only html view... # From 4e9c981f89bdc7572219da4beb5bfeaea4dcd40a Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:08:04 -0600 Subject: [PATCH 2/6] initial commit --- Gemfile.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0bfa104b..0c111e30 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,15 +11,15 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) -<<<<<<< HEAD + mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) -======= + mime-types (1.25.1) multi_json (1.10.1) netrc (0.8.0) ->>>>>>> 82e9a3d74cc1a7f9e00c05553481111f3e8a119a + pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) From 0c14e098f20cb138b431afb954524c778f869213 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:36:57 -0600 Subject: [PATCH 3/6] Preparing to merge --- Gemfile.lock | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0c111e30..78cd3d34 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,15 +11,11 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) - + mime-types (1.25.1) mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) - - mime-types (1.25.1) - multi_json (1.10.1) netrc (0.8.0) - pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) From ca84ea337b2da610486d9e22bc3bb9839d46f3f9 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 15:32:18 -0600 Subject: [PATCH 4/6] Prepping to merge --- Gemfile | 1 + Gemfile.lock | 2 ++ lib/PetDatabaseRepo.rb | 6 +++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 4324772d..637d5332 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,4 @@ gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' # Testing gem 'pry-byebug' +gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index 49a90396..1bf7ce17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,7 @@ GEM mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) + pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -55,6 +56,7 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug rest-client rspec (~> 2.14.1) diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index 1d150032..f2a66e6a 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -29,7 +29,7 @@ # imageurl | character varying | # adoption_status | boolean | -module PetShop + class Database def initialize @db = PG.connect(host: 'localhost', dbname:'petshop_db') @@ -51,7 +51,7 @@ def addData url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/dogs/" tempdogs = RestClient.get(url) dogs = JSON.parse(tempdogs) - dogs.each { |dog| #each dog + dogs.each { |dog| #each dogs shop = dog["shopId"] name = dog["name"] happiness = dog["happiness"] @@ -62,7 +62,7 @@ def addData INSERT INTO dogs (shop_id, dog_id, name, imageurl, happiness, adoption_status) VALUES ($1, $2, $3, $4, $5, $6) ] - @db.exec_params(sql, [shop, id, name, image, happiness, adopt]) + @db.exec(sql, [shop, id, name, image, happiness, adopt]) }#end each dog url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/cats/" From 9085c8c3a63ceabf5bc6a5c3623fea59ab8bcbe6 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 15:38:53 -0600 Subject: [PATCH 5/6] updated after merge --- Gemfile | 1 - lib/PetDatabaseRepo.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f27610a2..1446f3d9 100644 --- a/Gemfile +++ b/Gemfile @@ -9,4 +9,3 @@ gem 'pg' # Testing gem 'pry-byebug' -gem 'pg' diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index c4ee25be..2e23fffc 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -29,7 +29,7 @@ # imageurl | character varying | # adoption_status | boolean | - +module PetShop class Database def self.dbconnect PG.connect(host: 'localhost', dbname:'petshop_db') From 6850de198beaa0c01dfebd4ee1e630bfea6e976b Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 16:39:21 -0600 Subject: [PATCH 6/6] Cats db work --- lib/repos/DB.rb | 25 +++++++++++++++++++++++-- server.rb | 7 ++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 9b750345..50cee362 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -6,9 +6,30 @@ def self.get_shops(db) end def self.get_cats(db, shop_id) - db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + mappings = {"shop_id" => "shopId", "cat_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} + response = db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + cats = [] + response.each do |cat| + if cat['adoption_status'] == "t" + cat['adoption_status'] = "true" + else + cat['adoption_status'] = "false" + end + cats << Hash[cat.map {|k, v| [mappings[k], v] }] + end + cats end end -end \ No newline at end of file +end +# {"shop_id":"3", +# "cat_id":"5", +# "name":"karthurian", +# "imageurl":"http://cucinatestarossa.blogs.com/weblog/images/cat.jpg", +# "adoption_status":"t"} +# {"shopId":1, +# "name":"Scaredy Cat", +# "imageUrl":"http://i.imgur.com/TOEskNX.jpg", +# "adopted":true, +# "id":1} \ No newline at end of file diff --git a/server.rb b/server.rb index 2efed647..12913cb1 100644 --- a/server.rb +++ b/server.rb @@ -59,6 +59,7 @@ # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") db = PetShop::Database.dbconnect cats = PetShop::DB.get_cats(db, id) + puts cats.to_json cats.to_json end @@ -80,7 +81,7 @@ headers['Content-Type'] = 'application/json' id = params[:id] # TODO: Update database instead - # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") + RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") end put '/shops/:shop_id/dogs/:id/adopt' do @@ -88,8 +89,8 @@ shop_id = params[:shop_id] id = params[:id] # TODO: Update database instead - # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", - # { adopted: true }, :content_type => 'application/json') + RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", + { adopted: true }, :content_type => 'application/json') # TODO (after you create users table): Attach new dog to logged in user end