Specify complex password in Datamapper.setup - ruby

I m trying to use Datamapper as ORM for my Ruby/Padrino application. To setup db connection, in databse.rb, I have:
when :development then DataMapper.setup(:default, "mysql://db_user:dsfsdfs#localhost/my_app_development")
This work fine. But my requirement is to have complex password, something like:
when :development then DataMapper.setup(:default, "mysql://db_user:Passw0rd#13#localhost/my_app_development")
This doesn't work and I get error:
ruby/1.9.1/gems/addressable-2.2.8/lib/addressable/uri.rb:1179:in `port=': Invalid port number: "Passw0rd" (Addressable::URI::InvalidURIError)
I figured out that the '#' character is problem. Then I attempted doing this:
DataMapper.setup(:default,
{
:adapter => "mysql",
:database => "my_app_development",
:username => "db_user",
:password => "Passw0rd#13",
:host => "localhost"
})
When I do this, it seems like DM is ignoring the hash altogether; when I run
padrino rake db:create
It tries to connect as current logged in user, rather than the username specified here. Solution?

I suppose you still need to have to declare it like following, in case you are not
when :development then
DataMapper.setup(:default,
{
:adapter => "mysql",
:database => "my_app_development",
:username => "db_user",
:password => "Passw0rd#13",
:host => "localhost"
})
when :production then
# rest of your code
end

Got it - the key :username should be :user. Basically, the hash should contains keys as understood by Addressable::URI

Related

How to use Net::LDAP with JRuby

I am using Ruby and JRuby on a Ubuntu machine through RVM.
OpenLDAP is connecting and doing the CRUD operation through Ruby code with the Net::LDAP gem perfectly.
Here is the code:
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new :host => 'locahost',
:port => 389,
:async => 1,
:keepalive => 1,
:auth => {
:method => :simple,
:username => "cn=admin,dc=test,dc=com",
:password => "secret"
}
puts ldap.bind
When I execute the same code with JRuby, it throws an error:
$ jruby ldap_test.rb
ArgumentError: unsupported protocol family `__UNKNOWN_CONSTANT__'
initialize at org/jruby/ext/socket/RubySocket.java:188
new at org/jruby/RubyIO.java:847

Ruby Sinatra App Can't Connect to Local Postgres

I recently made a small wrapper in Sinatra. I had it working locally using SQLite, and I decided to switch to Postgres for the eventual move to Heroku.
However, I can't get my app to connect to my local postgres db. I'm running Windows 7 64-bit.
Here is database.yml:
development:
host: localhost
adapter: postgresql
database: postgres
username:
password:
Here is environments.rb:
configure :production, :development do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/postgres')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
This is the result when I try to rake:
Could not load 'active_record/connection_adapters/postgresql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.
I can connect to the db via psql and localhost just fine.

Add S3 with Carrierwave to Rails App for Heroku

I'm trying to add S3 to my Heroku app however, I'm getting the same problem I had on a previous question (carrierwave image not loading into source code) where the image url isn't loading into the source code.
Feature_image_uploader.rb has this instead of storage :file:
storage :fog
Gemfile:
gem 'carrierwave'
gem 'fog', '~> 1.3.1'
fog.rb file:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'xxx',
:aws_secret_access_key => 'yyy',
:region => 'eu-west-1',
:host => 's3.example.com',
:endpoint => 'https://s3.example.com:8080'
config.fog_directory = 'luchiauploads'
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
It has my access key ID and secret access key inserted. I also ran the terminal commands as per Heroku's instructions.
And this pastebin is my server log: http://pastebin.com/TH68bhn4
And rake tests have no errors.
I know I'm missing something really simple, but can't work out what. Thank you.
Error (line5 in pastebin):
Unpermitted parameters: feature_image_cache, remove_feature_image
you need to add these parameters to permited attributes:
portofolios_controller.rb
...
private
def portofolio_params
params.require(:portofolio).permit(:title, :date, :content, :feature_image, :feature_image_cache, :remove_feature_image)
end

Send emails with Padrino in Heroku

I'm trying to send emails via sendmail in Padrino. I did the configuration specified here (Configuration and Quick Usage)
But I always get the following error in the server log (on Heroku or localhost):
app[web.1]: sh: Illegal option -
app[web.1]: Errno::EPIPE - Broken pipe:
I installed the mail gem and I'm using Padrino 0.10.7
I'm using this, to send the email:
post :create do
email(:from => "tony#reyes.com", :to => "john#smith.com", :subject => "Welcome!", :body=>"Body")
end
That's practically all I have...
You should be using one of the parter addons for sending mail with Heroku.
A good option is Sendgrid
heroku addons:add sendgrid:starter --app=your_app_name
Then in your Padrino app in app.rb inside your App class:
set :delivery_method, :smtp => {
:address => "smtp.sendgrid.net",
:port => 587,
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
You could substitute these for settings for another external SMTP server, or look at Mandrill for transactional emails.
I suspect the Errno::EPIPE error you were seeing was that it could not connect to a valid SMTP server, so your controller code should be fine as it is.
Pat is right, you don't need an add-on, just configure your app.rb like stef suggests and you're good to go. So, for example, we use gmail and our config looks something like this:
set :delivery_method, :smtp => {
:address => "smtp.domain.com",
:port => 587,
:domain => 'rails.domain.com',
:user_name => "rails#domain.com",
:password => "super-secret",
:authentication => "plain",
:enable_starttls_auto => true,
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE
}

attempting to connect to sybase but resolving to mssql adapter

require 'rubygems'
require 'jdbc/jtds'
require 'activerecord-jdbc-adapter'
config = {
:username => 'me',
:password => 'mypass',
:adapter => 'jdbc',
:driver => 'net.sourceforge.jtds.jdbc.Driver',
:url => 'jdbc:jtds:sybase://myserver:1234/mydb'
}
ActiveRecord::Base.establish_connection( config )
ActiveRecord::Base.connection.class
# => ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
I realise that MSSQL and Sybase have a shared history, but is it correct that I get this adapter when connecting to Sybase?
My problem is that types are not being mapped to ruby type correctly. e.g. floats and, decimals are being translated to String.
Gems:
activerecord-jdbc-adapter 1.1.3
jdbc-jtds 1.2.5
ActiveRecord will resolved the JDBC adapter by adding the
:dialect => 'sybase'
...config option

Resources