Merb & DataMapper - accessing database connection info? - ruby

I'm using Merb and DataMapper with a MySQL db. I want to access the database name, user, and password from a Rake task for my Merb app. I guess I could YAML.load() the the database.yml, but that seems ugly. Any ideas?

desc "outputs database connection parameters"
task :db_conn => :merb_env do |t|
puts "Username: #{DataMapper.repository.adapter.uri.user}"
puts "Password: #{DataMapper.repository.adapter.uri.password}"
puts "Database: #{DataMapper.repository.adapter.uri.path.split('/').last}"
end
The interesting part there is the => :merb_env bit. That ensures that the "merb_env" task has executed before your task does. This simply loads up the Merb environment, at which point you can proceed to inspect its configuration.

Related

How to mock a 3rd-party library [duplicate]

an important part of my project is to log in into remote server with ssh and do something with files on it:
Net::SSH.start(#host, #username, :password => #password) do |ssh|
ssh.exec!(rename_files_on_remote_server)
end
How to test it?
I think I can have local ssh server on and check file names on it (maybe it could be in my test/spec directory).
Or maybe someone could point me better solution?
I think it's enough to test that you're sending the correct commands to the ssh server. You're application presumably doesn't implement the server - so you have to trust that the server is correctly working and tested.
If you do implement the server then you'd need to test that, but as far as the SSH stuff goes, i'd do some mocking like this (RSpec 2 syntax):
describe "SSH Access" do
let (:ssh_connection) { mock("SSH Connection") }
before (:each) do
Net::SSH.stub(:start) { ssh_connection }
end
it "should send rename commands to the connection" do
ssh_connection.should_receive(:exec!).ordered.with("expected command")
ssh_connection.should_receive(:exec!).ordered.with("next expected command")
SSHAccessClass.rename_files!
end
end
Your suggested solution is similar to how I've done it before:
Log into the local machine. For convenience you could use 'localhost' or '127.0.0.1', but for a better simulation of network activity you might want to use the full hostname. On Mac OS and Linux you can grab the host easily by using:
`hostname`
or
require 'socket'
hostname = Socket.gethostname
which should be universal.
From there create or touch a file on the local machine after logging in, so you can test for the change with your test code.

rake db:seed won't seed all the data in production

I have a seeds.rb file with data to seed. Not all the data gets seeded and rake db:seed ends with a message killed in the terminal. The same, however, works for development environment.
Here's the part I want to be seeded
xls_utility = Roo::Spreadsheet.open('/path/to/data.xlsx')
utilities = []
xls_utility.each do |row|
utility = Utility.new
if row[0] != "State"
["state_code", "value"].each_with_index do |attribute, index|
utility[attribute] = row.flatten[index]
end
utilities << utility
end
end
Utility.import utilities
Since I needed to seed data to some tables and since rake db:seed didn't seem to work.
Exporting and importing table in mysql was an alternative.
Export from local
mysqldump -p - -user=username dbname utilites > utilities.sql
Import from production
mysql -u username -p -D dbname < utilities.sql
However, I still want to know why rails seeding won't work. Thanks

Programmatically get access_token from OAuth - Using JIRA-Ruby gem

I'm trying to write a JIRA-ruby script (only be used from command-line) to mark some JIRA issue closed automatically.
I borrow an example from here because I'm using 'jira-ruby' gem.
This works however it will pop-up a browser asking you to click "Allow" to get the access_token. I would like to do this programmatically, but I don't think the API was built for this purpose. As access_token changes every time, and this script will run periodically in a cronjob, so we need to have a way to do this. Any idea what other ways we can do this?
require 'jira'
#jira = JIRA::Client.new({:site => 'http://localhost:2990', :context_path => '/jira', :consumer_key => 'test-jira', :private_key_file => "rsakey.pem"})
if ARGV.length == 0
# If not passed any command line arguments, open a browser and prompt the
# user for the OAuth verifier.
request_token = #jira.request_token
puts "Opening #{request_token.authorize_url}"
system "open #{request_token.authorize_url}"
puts "Enter the oauth_verifier: "
oauth_verifier = gets.strip
access_token = #jira.init_access_token(:oauth_verifier => oauth_verifier)
puts "Access token: #{access_token.token} secret: #{access_token.secret}"
elsif ARGV.length == 2
# Otherwise assume the arguments are a previous access token and secret.
access_token = #jira.set_access_token(ARGV[0], ARGV[1])
else
# Script must be passed 0 or 2 arguments
raise "Usage: #{$0} [ token secret ]"
end
# Show all projects
projects = #jira.Project.all
projects.each do |project|
puts "Project -> key: #{project.key}, name: #{project.name}"
end
issue = #jira.Issue.find('DEMO-1')
puts issue
I know there's a way to use long-life access tokens, but not really use if Jira supports it.
I was using the jira-ruby gem at first but I found the performance terrible. I ended up just going with curl instead as I only needed to require the JSON gem which is less bloated. Have your Jira administrators create a user that will never have the password change with admin access and then do the following to find "DEMO-1"
require 'json'
username = "admin"
password = "abc123"
issue = JSON.parse(%x[curl -u #{username}:#{password} \"http://jira/rest/api/latest/issue/DEMO-1\"])
Here is a link to the Jira REST API documentation, just choose the same version of Jira you are using. This will bypass any issues with oauth and the pop-up.

Unable to connect remote host through net/ssh

This is pretty weird. I have my public key added at host machine. I can simply run
ssh -p <port> -l <username> hostt.com
which simply opens the remote shell. I can even run my capistrano scripts for the deployments on the same machine. But when i was trying connect with this following simple ruby script
require 'rubygems'
require 'net/ssh'
Net::SSH.start("hostt.com",
:port => <port>,
:username => <username>
) do |session|
puts session.pwd
end
it refuses immediately with the following exception:
`initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
Is there anything I'm missing here?
Appreciate your help.
Okay, now after a few days when I look back to the problem, I got a quick success with the following tweak:
Net::SSH.start("<host>", "<user>", :port => "<port>") { |ssh|
puts "logged in"
puts ssh.exec!("ls -l")
} rescue puts "failed to connect."
So the difference with the previous one is the username, which in this case is passed as the second argument rather than like an option key.
you probably need to provide the location of your SSH key, or a password to use with the username you provide in the SSH.start parameters. for the keys, you need to pass the map value as an array :keys => ["path_to_key"]. I'm not sure why the api is set up that way, but it is.

Testing ssh connection

an important part of my project is to log in into remote server with ssh and do something with files on it:
Net::SSH.start(#host, #username, :password => #password) do |ssh|
ssh.exec!(rename_files_on_remote_server)
end
How to test it?
I think I can have local ssh server on and check file names on it (maybe it could be in my test/spec directory).
Or maybe someone could point me better solution?
I think it's enough to test that you're sending the correct commands to the ssh server. You're application presumably doesn't implement the server - so you have to trust that the server is correctly working and tested.
If you do implement the server then you'd need to test that, but as far as the SSH stuff goes, i'd do some mocking like this (RSpec 2 syntax):
describe "SSH Access" do
let (:ssh_connection) { mock("SSH Connection") }
before (:each) do
Net::SSH.stub(:start) { ssh_connection }
end
it "should send rename commands to the connection" do
ssh_connection.should_receive(:exec!).ordered.with("expected command")
ssh_connection.should_receive(:exec!).ordered.with("next expected command")
SSHAccessClass.rename_files!
end
end
Your suggested solution is similar to how I've done it before:
Log into the local machine. For convenience you could use 'localhost' or '127.0.0.1', but for a better simulation of network activity you might want to use the full hostname. On Mac OS and Linux you can grab the host easily by using:
`hostname`
or
require 'socket'
hostname = Socket.gethostname
which should be universal.
From there create or touch a file on the local machine after logging in, so you can test for the change with your test code.

Resources