lib/base/db.rb in logworm-0.7.3 vs lib/base/db.rb in logworm-0.7.4
- old
+ new
@@ -6,28 +6,27 @@
class DatabaseException < Exception ; end
class InvalidQueryException < Exception ; end
class DB
- DEFAULT_HOST = "db.logworm.com"
URL_FORMAT = /logworm:\/\/([^:]+):([^@]+)@([^\/]+)\/([^\/]+)\/([^\/]+)\//
# URI: logworm://<consumer_key>:<consumer_secret>@db.logworm.com/<access_token>/<access_token_secret>/
+
+ attr_reader :host, :consumer_key, :consumer_secret, :token, :token_secret
def initialize(url)
match = DB.parse_url(url)
- raise ForbiddenAccessException.new("Incorrect URL Format #{url}") unless match and match.size == 6
-
+ raise ForbiddenAccessException.new("Incorrect URL Format #{url}") unless match and match.size == 6
@consumer_key, @consumer_secret, @host, @token, @token_secret = match[1..5]
- raise ForbiddenAccessException.new("Missing consumer keys") if @consumer_key.nil? or @consumer_secret.nil?
- raise ForbiddenAccessException.new("Missing tokens") if @token.nil? or @token_secret.nil?
@connection = OAuth::AccessToken.new(OAuth::Consumer.new(@consumer_key, @consumer_secret), @token, @token_secret)
end
def self.with_tokens(token, token_secret)
consumer_key = ENV["#{ENV['APP_ID']}_APPS_KEY"]
consumer_secret = ENV["#{ENV['APP_ID']}_APPS_SECRET"]
- DB.new(DB.url(consumer_key, consumer_secret, token, token_secret))
+ host = ENV["#{ENV['APP_ID']}_DB_HOST"]
+ DB.new(DB.make_url(host, consumer_key, consumer_secret, token, token_secret))
end
def self.from_config
# Try with URL from the environment
return DB.new(ENV['LOGWORM_URL']) if ENV['LOGWORM_URL'] and DB.parse_url(ENV['LOGWORM_URL'])
@@ -48,19 +47,19 @@
db = self.from_config
raise "The application is not properly configured. Either use 'heroku addon:add' to add logworm to your app, or save your project's credentials into the .logworm file" unless db
db
end
- def self.url(consumer_key, consumer_secret, token, token_secret, host = DEFAULT_HOST)
+ def self.make_url(host, consumer_key, consumer_secret, token, token_secret)
"logworm://#{consumer_key}:#{consumer_secret}@#{host}/#{token}/#{token_secret}/"
end
def url()
- DB.url(@consumer_key, @consumer_secret, @token, @token_secret, @host)
+ DB.make_url(@host, @consumer_key, @consumer_secret, @token, @token_secret)
end
def self.example_url
- self.url("Ub5sOstT9w", "GZi0HciTVcoFHEoIZ7", "OzO71hEvWYDmncbf3C", "J7wq4X06MihhZgqDeB")
+ self.make_url("db.logworm.com", "Ub5sOstT9w", "GZi0HciTVcoFHEoIZ7", "OzO71hEvWYDmncbf3C", "J7wq4X06MihhZgqDeB")
end
def tables()
res = db_call(:get, "#{host_with_protocol}/")
end