What's the redis equivalent of ActiveRecord::Base.logger = Logger.new(STDOUT)? I want to see what redis is up to.
$redis.client.logger = Rails.logger
It's better!
I usually want to set some additional attributes (such as the redis DB name). You can pass on arguments in the initialize method like this
$redis = Redis.new(db: Rails.config.redis.db, logger: Rails.logger)
Never mind. It's easy:
$redis = Redis.new(:host => 'localhost', :port => 6379)
$redis.client.logger = Logger.new(STDOUT)
I wrote a gem call RedisLogger:
https://github.com/hellolucky/redis_logger
Related
I'm using this config to connect to MongoDB with MongoMapper in my Sinatra application:
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
Now I have a replica set with 2 mongos on separate servers, 10.5.5.5, and 10.5.5.6. How do I setup the connection with both mongos? How do I add authentication to this connection?
I ended up doing this:
MongoMapper.connection = Mongo::MongoReplicaSetClient.new(
['10.5.5.5:27017', '10.5.5.6:27017'],
:read => :primary, :rs_name => 'name', :connect_timeout => 30, :op_timeout => 30
)
MongoMapper.database = "db_name"
MongoMapper.database.authenticate("user", "test123")
Works beautifully.
You should be able to set a different connection per model. But I guess this is not exactly what you trying to do.
class MyModel
include MongoMapper::Document
connection(Mongo::Connection.new('localhost', 27017))
set_database_name "my_database"
# ...
end
Or there is ReplSetConnection with this you can set your replications sets:
MongoMapper.connection = Mongo::ReplicaSetConnection.new(['10.5.5.5', 30000], [' 10.5.5.6', 30000])
And the authentication is simple:
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "DBNAME"
MongoMapper.database.authenticate("USERNAME", "PASSWORD")
For convention reasons I would like to initialize the Fitgem client in config/initializers/fitgem.rb. When I say initialize I mean pass in my app's consumer token and consumer secret like so:
Fitgem.configure do |config|
config.consumer_key = "XXXX"
config.consumer_secret = "XXXX"
config.token = "XXXX"
config.secret = "XXXX"
end
This is the exact same manner that is done with Facebook and Twitter clients (https://github.com/sferik/twitter) elsewhere. Is there a similar way I can do this with Fitgem?
The error I receive when I try to initialize the client this way is:
undefined method `configure' for Fitgem:Module
The fitgem docs (http://www.rubydoc.info/github/whazzmaster/fitgem/frames) say to do it like this:
client = Fitgem::Client.new {
:consumer_key => my_key,
:consumer_secret => my_secret,
:token => fitbit_oauth_token,
:secret => fitbit_oauth_secret
}
But I don't want to have to re-initialize the Fitgem client in every method.
So, number 1, I would love to know how to do this, and number 2 I would love to know how to look at the fitgem code to see that configure is not an acceptable method.
Maybe it's been out of date to answer, but now there is omniouth for fitbit. It's not so difficult to use the gem because there is the explanation about how to do the configuration. You have to make omniauth.rb file under config/initializers and place the consumer_key and consumer_secret there.
I'd like to use the Readability API through the Readit gem; however, I've been having some trouble trying to get an access token through XAuth. Here's the code that I have:
require 'highline/import'
require 'yaml'
require 'oauth'
require 'readit'
config = YAML.load_file("config/readability.yaml")
uname = ask ("Username: ")
passwd = ask ("Password: ") {|q| q.echo = false}
consumer = OAuth::Consumer.new(config["-consumer_key"], config["-consumer_secret"], :site => "https://www.readability.com/api/rest/v1/oauth/access_token/")
access_token = consumer.get_access_token(nil, {}, {:x_auth_mode => 'client_auth', :x_auth_username => uname, :x_auth_password => passwd})
However, when I try to run this, I get the following:
/Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:219:in `token_request': 404 NOT FOUND (OAuth::Unauthorized)
from /Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:109:in `get_access_token'
from instab.rb:11:in `<main>'
Can someone explain to me what I am doing wrong?
You should write as follows:
consumer = ::OAuth::Consumer.new(Readit::Config.consumer_key,Readit::Config.consumer_secret,:site=>"https://www.readability.com/", :access_token_path => "/api/rest/v1/oauth/access_token/")
Is there any way to get resque-web to work with a Redis To Go hosted redis instance?
UPDATE:
#Nemo157's suggestion was correct. Ended up creating a test-evn.rb containing:
uri = URI.parse(" redis://XXXX#catfish.redistogo.com:9122")
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Pass it the config file you're using to setup redis in the app, e.g.
resque-web ./environment.rb
where environment.rb contains something like:
Resque.redis = Redis.new(:host => "path.to.host", :port => 6379)
Note: I haven't tested this since all my redis instances have been on localhost, but that's my understanding of how it works.
I want to change the logging level of an application (ruby).
require 'logger'
config = { :level => 'Logger::WARN' }
log = Logger.new STDOUT
log.level = Kernel.const_get config[:level]
Well, the irb wasn't happy with that and threw "NameError: wrong constant name Logger::WARN" in my face. Ugh! I was insulted.
I could do this in a case/when to solve this, or do log.level = 1, but there must be a more elegant way!
Does anyone have any ideas?
-daniel
Why don't you just use the literal constant in your config hash?
config = { :level => Logger::WARN }
Then you don't have to fool around with const_get or anything like that; you can simply do log.level = config[:level].
If it absolutely must be a string, you can drop the namespace prefix and call const_get on the Logger module:
irb(main):012:0> Logger.const_get 'WARN'
=> 2
If it really really has to be the qualified string, you might try using this blog's qualified_const_get method (which is not a built-in!).