ActiveResource: Issues With Using Email For Usernames
I first ran up against this issue of using email addresses rather then the standard usernames with ActiveResource when I was doing the Lighthouse integration for the administrative interface on Lypp. Now with Lypp running our Mashup Contest against the APIs over at 37Signals I’m seeing a lot of Rails developers getting stuck on this same issue.
A fix has made it into Rails a while ago: Rails Trac Ticket #11112
Here are some of the error snippets for search indexing purposes.
URI::InvalidURIError: bad URI(is not URI?): https://username@example.com:password@dev1.lypp.com/users/9999
...
URI::InvalidComponentError: bad component(expected userinfo component or user component): username@example.com
We all wish we could run on edge rails all the time across all our projects but reality is we don’t always have that luxury.
Real simple test class to test basic calls with.
Add this file to a blank rails project.
# apps/models/conference.rb
class Conference < ActiveResource::Base
self.site = 'https://lypp.com/users/9999'
self.site.user = 'username@example.com'
self.site.password = 'password'
end
Jump to the console.
./script/console
Loading development environment (Rails 2.0.2)
>> Conference.find(:all)
If you get any of the above errors. Here is a code snippet to put in one of your project’s initializers to get pass this issue if you are stuck at an incompatible ActiveResource version.
# config/initializers/url_fix.rb
module URI
class Generic
def check_user_with_escaped_username(user)
check_user_without_escaped_username(escape_userpass(user))
end
alias_method_chain :check_user, :escaped_username
end
end
Enjoy!
I hope you found this post before you got to frustrated or discouraged…
Here are some great resources to move you along a little further once you get past this.
Railscast Episode #94: ActiveResource Basics
Railscast Episode #95: More on ActiveResource
I really need to make the time contribute my 5 tips for the Railscast 100th episode contest. The iPod would be nice but really just want to give a little back to what I think is hands down the best FREE resource for getting going with Rails.
