mispy/twitter_ebooks — pull request #50 by Stawberri (2014-12-09 04:37:25+00:00)
Title: read_config_file

Added the ability to read a configuration file while initializing a new bot! This will also make it check for a twitter hash inside of that file, and then automatically load usernames, keys, tokens, and secrets out of it as well!

This means that if you're creating a bot template, and you want to upload it to github, you just have to .gitignore your configuration file to keep everyone from having your details! If you have a test bot, you can have them both use the same relative configuration filename, and then just shuffle files between multiple folders to switch between bots!

Using this is super easy! You know how you create a bot with `MyBot.new('username')`? All you need to do is change it to `MyBot.new('any file you want.yaml')`! Currently, this supports only yaml and json because those were the only two configuration file formats Ruby seems to support out of the box, but if @mispy merges this pull request, I'll go and create more separate pull requests incorporating more file formats!

> ***The old format still works completely!***

I personally find json a lot easier to write than yaml is, but look at how easy yaml configuration is!
twitter:
username: your_username_here
consumer key: amYgXlP9IRZjiYmjfZb2H
consumer secret: nH6KL7TOLjIvQg15kgWvQi2O9wu0w6iudaXCEXLGdvu2J6OpV0IX
access token: 5400193136-Muty60xkZq11nzd3S8A5fvXcpyR03LBc262eseZn
access token secret: ARThyyQNaOtfaGE1d1Crysv1mNAYTdxLUhBarJoFTSfBO1h
It's human readable! Be careful though, because YAML is super-duper particular about how it's formatted. You should probably use a converter like http://jsontoyaml.com/ (that's the first result on Google, so I'm not vouching for it or anything) to generate yaml's structure. I'm sure you can edit your actual values using a text editor just fine.

Anyways, once your configuration file is loaded, those things I put in my sample will automatically be put into their proper variables in your bot. If they're missing from your file, that's fine. I hope you set them some other way! But the really fun thing about this is~ everything in your configuration file also gets loaded into `YourBot.config`! It's set to `attr_reader`, which means that you shouldn't edit it. That's for the file to do! But that means that you can also put anything else in that file. Are you making a bot that loads some content off of a website with a paywall? Instead of putting your API keys directly into your code, why not put it in a separate file that's easy to remember to hide from other people, and then access `@config['paywall site']['api key']`?

Do you have multiple bots? Great! Just add in multiple config files! Note that your config files don't even have to be named after your bots! My own config file is named `bot.yaml`, and that's really great, because I have two copies of it: one in my heroku folder and another in my test folder. I just move my files back and forth, and suddenly both bots can tweet with no file editing to do whatsoever!