Sunday, August 12, 2012

Rails 3 : APN on Rails

I got very excited when one of the colleague told me that he wants to send notifications to his apple devises(itouch/ iphone) through rails application I am working on.

So I got PRX/apn_on_rails and yekmer.posterous.com tutorial. And I started working after reading the required information. Wow..

But there was error as below in console while running devise creation command
ActiveRecord::StatementInvalid: PG::Error: ERROR:  relation "apn_bases" does not exist
LINE 4:              WHERE a.attrelid = '"apn_bases"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"apn_bases"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Well, this was not working.
Tried to find some solution, and got issue 38 for github.com/PRX/apn_on_rails.

After reading added
self.abstract_class = true
to apn/base.rb and restarted the server. It started working as required. It successfully created a devise information row in the table.

I thought the work is done(not really) and just need to run command
rake apn:notifications:deliver --trace
But it started throwing the error as below
** Invoke apn:notifications:deliver (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute apn:notifications:deliver
rake aborted!
SSL_connect returned=1 errno=0 state=SSLv3 read finished A: sslv3 alert certificate unknown
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:58:in `connect'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:58:in `open'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/libs/connection.rb:23:in `open_for_delivery'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/app/models/apn/notification.rb:104:in `send_notifications'
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/apn_on_rails-0.3.1rails3/lib/apn_on_rails/tasks/apn.rake:7:in `block (3 levels) in '
/home/supriya/.rvm/gems/ruby-1.9.3-p125/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
Again searched and got to know that there might be something to do with certificate generated. Ok, I and colleague discussed on this and he solved the same with generating new .pem file for this task. The certificate generation steps can be viewed at maniacdev.com/.

Replaced the same in my project and wow, that worked like a charm.

Tuesday, August 7, 2012

Rails 3 : WARN Could not determine content-length

One of the project I am working on, gets the call from iPhone application. Suddenly iPhone application stopped receiving the data. After checking the log I found the following error.


I searched and found the solution on stackoverflow.com : content-length-of-response-body and stackoverflow.com : unknown-warning-in-development-mode which was very informative. I added
gem 'thin'
and then
bundle install
After the whole process restarted the server. Error was vanished and iPhone application was running as per the requirement.