I am using a combo of omniauth
and delayed_job
to integrate with Facebook. I have an Authentication
model that looks a little bit like this:
class Authentication < ActiveRecord::Base
def account
@account ||= FbGraph::User.me(token)
end
def post_to!(opts = {})
account.feed!(opts)
end
end
So then from the console I can do something like this:
auth.post_to :message => 'Foo bar', :link => 'http://google.com'
... and everything works perfectly and an update is posted to my Facebook feed.
However, in the application I have created a custom delayed job that does exactly the above, only it fails every time with the following error stack:
undefined method `bytesize'' for nil:NilClass
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/activesupport-3.1.3/lib/active_support/whiny_nil.rb:48:in `method_missing''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:830:in `block in escape''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/activesupport-3.1.3/lib/active_support/core_ext/string/output_safety.rb:142:in `gsub''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/activesupport-3.1.3/lib/active_support/core_ext/string/output_safety.rb:142:in `gsub''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:829:in `escape''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:822:in `block in escape_query''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:818:in `each''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:818:in `collect''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:818:in `escape_query''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:809:in `create_query_part_str''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:538:in `set_content''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:447:in `init_request''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient/http.rb:705:in `new_request''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient.rb:997:in `create_request''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient.rb:866:in `do_request''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient.rb:756:in `request''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/httpclient-2.2.4/lib/httpclient.rb:666:in `post''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/fb_graph-2.4.7/lib/fb_graph/node.rb:55:in `block in post''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/fb_graph-2.4.7/lib/fb_graph/node.rb:122:in `handle_response''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/fb_graph-2.4.7/lib/fb_graph/node.rb:54:in `post''
/Users/aaron/.rvm/gems/ruby-1.9.2-p290@bb/gems/fb_graph-2.4.7/lib/fb_graph/connections/feed.rb:15:in `feed!''
/Users/aaron/Sites/bb/app/models/authentication.rb:40:in `post_to!''
/Users/aaron/Sites/bb/lib/share_activity_job.rb:39:in `block in perform''
/Users/aaron/Sites/bb/lib/share_activity_job.rb:11:in `each''
/Users/aaron/Sites/bb/lib/share_activity_job.rb:11:in `perform''
I have officially got to the point where I start throwing expensive computers out of the window. Can anyone offer any insight as to why it's failing, why it works when executed from the console but fails when executed from a custom delayed_job?
Any help is greatly appreciated. Thank you.
The solution is simple. You have to use to_str in your code:
auth.post_to message: message.to_str, link: 'http://google.com'