Permalink
Browse files

postgres ruby wranglin'

  • Loading branch information...
1 parent e68549a commit 7839b02b5db16c47d6751e6eff61f8467713a947 Kevin Burke committed Jan 21, 2013
Showing with 39 additions and 2 deletions.
  1. +1 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +4 −0 app/app.rb
  4. +31 −1 app/models/db.rb
  5. +1 −1 app/views/index.rb
View
@@ -6,6 +6,7 @@ gem 'redis'
gem 'mustache', '~>0.11.2'
gem 'curb', '~>0.7.8'
gem 'coderay', '~>0.8.357'
+gem 'pg'
gem 'sinatra_auth_github', '~>0.10.0'
View
@@ -23,6 +23,7 @@ GEM
faraday_middleware (~> 0.8)
hashie (~> 1.2)
multi_json (~> 1.3)
+ pg (0.14.1)
rack (1.2.1)
redis (3.0.2)
rest-client (1.6.1)
@@ -54,6 +55,7 @@ DEPENDENCIES
coderay (~> 0.8.357)
curb (~> 0.7.8)
mustache (~> 0.11.2)
+ pg
redis
shotgun
sinatra (~> 1.0)
View
@@ -284,5 +284,9 @@ def stringify_data(data)
raise "Cannot stringify #{data.inspect}"
end
end
+
+ after do
+ DB.close
+ end
end
end
View
@@ -1,4 +1,5 @@
require 'fileutils'
+require 'pg'
module Hurl
class AbstractDB
@@ -11,6 +12,34 @@ def self.decode(object)
end
end
+ class PostgresDB < AbstractDB
+ CONN = PG::Connection::new(
+ ENV.fetch("POSTGRES_HOST", "localhost"),
+ ENV.fetch("POSTGRES_PORT", 5432),
+ :dbname => "hurls",
+ :user => ENV.fetch("POSTGRES_USER", "postgres"),
+ :password => ENV.fetch("POSTGRES_PASSWORD", "postgres")
+ )
+
+ def self.find(scope, id)
+ CONN.exec("SELECT content::bytea FROM hurls WHERE scope = $1 AND id = $2 LIMIT 1", [scope, id], 1) do |result|
+ decode(result.getvalue(0, 0)) if result.num_tuples >= 1
+ end
+ end
+
+ def self.save(scope, id, content)
+ CONN.exec("INSERT INTO hurls VALUES ($1::varchar, $2::varchar, $3::bytea)",
+ [scope, id, {:value => encode(content), :format => 1}])
+ end
+
+ def self.count(scope)
+ end
+
+ def self.close
+ CONN.finish
+ end
+ end
+
class RedisDB < AbstractDB
uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://127.0.0.1:6379"))
CONNECTION = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
@@ -65,9 +94,10 @@ def self.dir(scope, id)
end
end
- db_backend = ENV.fetch("DB_BACKEND", "file")
+ db_backend = ENV.fetch("DB_BACKEND", "postgres")
DB = {
"file" => FileDB,
"redis" => RedisDB,
+ "postgres" => PostgresDB
}.fetch(db_backend)
end
View
@@ -142,4 +142,4 @@ def view
[ :header => @view['header'], :body => @view['body'] ]
end
end
-end
+end

0 comments on commit 7839b02

Please sign in to comment.