Config information for a gem - ruby

I am working on a gem that needs to be configured with a hostname, username, and password. I would prefer to store this configuration information in config/directoryservices.yml
How do I make my gem automatically create this file when installed?
I've seen a lot of places say that config information should be passed as hashes with gems. The main use of this gem will be in an app that is installed on ~15 servers. Each server will need different config information.
If it is better to include the YAML code in the app and then pass the data as a hash to the gem, I can do that, but I would still like pointers on the best way to do that.
An example of a gem that uses this kind of config is activeldap

If are you working on rails gem, you can create install generator which will copy default config file and then load it into your app.

Related

Protecting APIs keys on Git-hosted Ruby project

I'm not really sure where to start with this. I'm making a Ruby project that interfaces with a dictionary API, and uses an API key. I don't want anybody and his uncle to be able to see this key, but the project will have to be hosted on GitHub. How can I go about doing this, and accessing the key from the Ruby program?
Clarification: this is for a class, and we have to use GitHub
Normally, you'd put such things in a file like this:
DICTIONARY_API=key_goes_here
and check in a version of the file (named .example or .sample or something) which just contains blanks:
DICTIONARY_API=
Or you could read the key from the environment, using ENV. If you host on Heroku, this is recommended. See also the Dotenv gem and the ENVied gem.
I've seen both methods combined (especially when using Dotenv) by making a .env file for local/non-heroku usage, and using Heroku's config settings on Heroku.
Use the Figaro gem. Documentation
Basically you will use a file called application.yml and store environmental variables. Double check that application.yml is listed in your .gitignore file so nobody can view it on github.
You could set:
# application.yml
API_KEY: my_api_key_here
And then you could set it to another variable in your app with:
# anywhere in your app
api_key = ENV['API_KEY']
For production, you can use Figaro's commands to sync your env variables with Heroku.

Ruby Third-Party Dependencies in Custom Puppet Providers

I am not so familiar with Ruby and native custom type development in Puppet, but I cannot find any hints what is the right way of getting modules running that require other ruby gems. I have implemented a custom provider that requires rest-client and nokogiri.
For test purposes I put the module to a specific location und used puppet apply --modulepath=... site.pp. Of course, the run fails because rest-client and nokogiri and are not available on the test host. I can install the gems manually via gem install rest-client nokogiri and everythings works, but I am trying to create a fully automated provision process.
What is the right approach for getting those modules running automatically. In the final scenario, the modules are located at the puppetmaster. Are rest-client and nokogiri required to be present on server- or client-side and how can I make sure that those gems are installed automatically when the module gets used?
Unfortunately, as of right now, "Puppet module metadata.json currently are only concerned with module dependencies," and can't configure gems (PUP-3386).
There was an email discussion on Puppet Users about this at the end of last year as well.

Sinatra Set Configuration File on Heroku

I have been struggling for the last couple hours with this question, and I hope someone can help me out. I'm creating my first Sinatra app and I would like to use Mongo as the backend. I have decided to use Heroku's MongoLab service, and it gave me a connection URI to use to connect with Mongo from within my Sinatra app. This does not seem like the type of info I want to keep in Version Control, but I'm having a hard time figuring out how to not hardcode it into the application. On one hand the key is stored permanently as a Heroku ENV var, but that does not help when I'm developing locally. I've tried creating a config file as outlined here: http://www.miqueloliete.com/configuring-environment-variables-in-sinatra/, but it only helps locally. I can't seem to find the way to do this.
Thanks in advance,
Ryan
There are many ways to achieve these so I can just give you ideas.
Env
You can set the enviroment variable on your local pc with export name=mongourl
configuration
Sinatra provide a way to use different configuration sections. That is what I do normaly. Like these:
configuration :development do
setup things to use your local db
end
configuration :production do
setup things for production db
end
config file
Store these informations in a yaml file.
I figured out the best way to do it for me. I didn't realize that in Ruby ENV['somevar'] accessed your environmental variables for your shell, so in order to not commit my secret keys and passwords to version control, I just make sure that all my environmental variables that I had in my Heroku (the results of heroku config) were also variables in my shell.
You can use the dotenv gem and then create a .env file locally that has the same keys as your Heroku ENV variables. That way you can keep the same environment keys in your code for both environments, and choose to have identical or different values, as needed.
#app.rb
require 'sinatra/base'
...
if Sinatra::Base.environment == :development
require 'dotenv'
Dotenv.load
end
#.env
DATABASE_URL=mongo_sinatra_connection_info

Heroku and Datamapper problems

I can launch a basic app on Heroku, displaying a message with get '/'... works just fine. However, whenever I try to add sqlite with datamapper, the thing breaks down.
In order to see my app structure, check out the project on github. I've kept the code pretty bare-bones.
In the log from heroku I'm getting:
2011-06-26T21:28:36+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/adapters.rb:163:in `require': no such file to load -- dm-postgres-adapter (LoadError)
The thing about this is that I'm not using postgres, so I'm confused why it is saying this.
"The thing about this is that I'm not using postgres, so I'm confused why it is saying this."
You're using Heroku, so you are using Postgresql.
In your app.rb you have this line:
DataMapper::setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/notes.db")
which I'm guessing you got from the Heroku database docs. This line basically says "check for the environment variable 'DATABASE_URL', and if it's set use it as the database url, otherwise use the Sqlite url". Running locally this environment variable won't be set, so you'll use the Sqlite url, but on Heroku this will be set to something like (see the page linked above):
postgres://username:password#hostname/database
Datamapper will see that this is a Postgresql url, and try to require the postgres adapter, which isn't installed so will result in the error that you see.
The best solution would be to install Postgresql locally, so that your development and production environments are as similar as possible. If you can't do this, or don't want to, you can specify the Sqlite adapter locally, and the Postgres adapter in production. It'll look something like this in your Gemfile:
group :development do
gem 'dm-sqlite-adapter'
end
group :production do
gem 'dm-postgres-adapter'
end
If you do this you'll need to tell Heroku to which groups to leave out when installing gems, see the Heroku Gem Bunder docs.
You are moving from sqlite to postgres-sql and thus you should include dm-postgres-adapter in your Gemfile (and install it ofcourse).
Heroku only supports a read-only file system (or at least only supports read-only operations for any files that you want to be persistent). Hence, you can't use SQLite on Heroku. Heroku only supports PostgreSQL as a database so you need to configure DataMapper to use PostgreSQL (see Yet Another Geek's answer).
I'd recommend installing PostgreSQL in your development environment if you're planning to deploy to Heroku. Every database is a little different and no ORM will protect you from the differences. A bit of searching for questions tagged heroku and postgresql should show some of the problems you'll have if you develop on top of one database but deploy on another.

Running gem server in passenger

I'm running a few rails/rake apps in Apache/passenger and I want to add the documentation app served by gem server to these apps, so I can easily give it a special (sub)domain, like docs.example.org, so it's easily available for all members of our team and nobody has to start the server himself or remember port numbers (like 8808, the default gem server port).
I would recommend looking into bdoc instead of gem server, it allows the user to access all their gem docs without a server running at all. It would also be trivial to modify bdoc to output to a specific directory then you could easily add a step to regenerate the docs.
The nice thing about having them in static files would be the apache config is dead simple.
If you do want to make bdoc output to a specific dir look at this line.
Edit:
I actually went ahead and branched this on github and made the change. Now you can supply the output directory on the command line and it will generate the static rdoc pages for you.
I'm running http://gems.local on my machine in case I want to do some Ruby cracking offline. (Plain journey, trains, etc).
This is really easy, you can actually run passenger with all the Ruby gems' documentation locally without having to access the net.
I was following Jason's tips and got everything working. See the following article and you should be ready to go:
http://jasonseifer.com/2009/02/22/offline-gem-server-rdocs
Attila
I wrote a blog post on how I have my gems, ruby, rails and jquery docs locally using the yard server and nginx for proxing in mac os x. Steps for linux are almost the same, only thing that changes is the way to configure the daemons.
https://makarius.posterous.com/offline-rails-ruby-jquery-and-gems-docs-with

Resources