Topic: A little gotcha with Oauth Plugin and plural Oauth Credentials keys

Topic type:

A workaround for using plural named keys with the https://github.com/pelle/oauth-plugin gem and Rails 3.

I'm working on a project that will pull in data from an API that is behind Oauth 1a authorization. When setting up use of https://github.com/pelle/oauth-plugin one of the first things you have to do (after, in this case, the pain of actually getting a consumer key and secrect from the API provider) is declare your Oauth services that your application is a consumer of in config/initializers/oauth_consumers.rb like so (Ruby 1.9x hash syntax):

OAUTH_CREDENTIALS={
service_name: {
key: '...',
secret: '...',
options: {
site: "https://...",
}
}
} unless defined? OAUTH_CREDENTIALS

If your service name is something plural though, you'll get a mysterious "uninitialized constant" error when a user tries to use the service in question. What's happening is that the gem's oauth/models/consumers/service_loader.rb file defines the ...Token class automatically based on the key of the service in OAUTH_CREDENTIALS. It calls "classify" on the key, which strips any plurality out of the new class name.

However, the oauth consumer controller doesn't de-pluralize the class name and therefore you get the "unitialized constant" error.

Let's say our mythical plural branded service is called 'slacks', here's how you would work around this issue with the class_name setting:

OAUTH_CREDENTIALS={
slacks: {
class_name: 'SlacksToken',
key: '...',
secret: '...',
options: {
site: "https://...",
}
}
} unless defined? OAUTH_CREDENTIALS

P.S. - I'm so going to ditch TinyMCE in the future. Really not happy with out it strips spacing out of "pre" wrapped code!

Discuss This Topic

There are 0 comments in this discussion.

join this discussion

Like what you've read? You can check out my latest project at Generali.st.

Got a project of your own that you need help with? Let's talk