16 3 / 2013
Publishing cookbook to chef community site
After creating my first errbit cookbook, and publishing it on the chef community site, I’d to do some stretches to publish it.
First, you need to tgz your cookbook and it should have the metadata.json file otherwise the validation fails and the cookbook won’t be published.
So, I am putting it here for me and others:
In your cookbook dir, create a dir .chef with a knife.rb file in it with the following line:
cookbook_path "."
Then issue the command knife cookbook metadata . (Don’t forget the dot)
This will generate metadata.json file.
Better to commit it here and push the changes to your git repo.
Now step out 1 dir path from your cookbook folder and do the compressing.
cd ..
tar -czf errbit.tgz --exclude bin --exclude .envrc --exclude .vagrant --exclude .bundle errbit
You might have to change/add the --exclude vars according to your setup.
Now we’ve the errbit.tgz file with the metadata.json file in it which you can upload to the community site at http://community.opscode.com/cookbooks/new
When updating the cookbook later, do change the version in metadata.rb file and repeat the same process to publish your cookbook’s changes.
Permalink 1 note
01 3 / 2013
How to get facebook page_id and access_token to publish on its wall
Its always tricky to post on the wall of the FB Page. It requires PAGE_ID and ACCESS_TOKEN (Not the user’s access token) and so many questions on how to get facebook page id and access token.
The FB Developers webapp way
First create a page by visiting http://www.facebook.com/pages/create.php?ref_type=sitefooter
But this is optional. If you’ve already created the page, the just skip this part.

Create an App

Created App admin page

Graph API Explorer

Get Access Token
In the Extended Permissions tab, select manage_pages and publish_stream and click Get Access Token

Authorize you account

Allow Permission

Copy the blue-highlighted temporary access token

Permanent Access Token
The above temporary access token is also valid. But its limited to 60 days. Its better to have the permanent access token so that it works in the long run, though I forget to updated periodically.
To get this, go the following url replacing the TEMPORARY_ACCCESS_TOKEN value with the temporary access token.
https://graph.facebook.com/me/accounts?access_token=TEMPORARY_ACCCESS_TOKEN

Ruby Way
First we need to get the PAGE_ID of the page. To get this, visit https://www.facebook.com/pages/
And on the left sidebar under Pages section, select the page.

Copy the Page id from the address bar

Here I’m using the fb_graph gem
require ‘rubygems’ require ‘fb_graph’ require ‘pry’ FbGraph.debug!
PAGE_ID = “349835561791721” YOUR_ACCESS_TOKEN = “……”
owner = FbGraph::User.me(USER_ACCESS_TOKEN) pages = owner.accounts
page = pages.detect do |page| page.identifier == PAGE_ID end
binding.pry page
There is the page access_token
Enjoy!!
07 12 / 2012
Invoking rake task within the rails console
In some situation, I’d to invoke rake task from the console itself. This makes me do it that with just 3 lines of code right from the rails console.
require 'rake'
MyApp::Application.load_tasks
Rake::Task['the_rake_task'].invoke
28 11 / 2012
Rails3 singleton resource with form_for gotcha
Dealing with the singleton resource was quite a bit tricky:
# config/routes.rb
resource :passion
# form.html.slim
= form_for @passion do |f|
Throws an error:
undefined method `passions_path' for #<#<Class:0x0000012d957008>:0x000001347f1ae0>`
So the fix is:
= form_for @passion, url: passion_path do |f|
23 9 / 2012
Use cases of ActiveSupport library
While building a Gem or API or any rubyish, the active_support library comes in so handy. So, listing here some usages as a reminder for me (maybe for you too).
- https://github.com/gocardless/gocardless-ruby/pull/4/files
- Mongoid Indifferent Access
- Ruby on Rails3 Tutorial Book Ref
- http://rubydoc.info/docs/rails/Hash#except!-instance_method
- http://guides.rubyonrails.org/active_support_core_extensions.html
I’ll be updating this list as I find its other use-cases or you can add on the comments and I’ll update this.
Permalink 1 note