Skip to content

Instantly share code, notes, and snippets.

@brynary
Forked from ngauthier/readme.md
Created September 27, 2011 15:53
Show Gist options
  • Save brynary/1245453 to your computer and use it in GitHub Desktop.
Save brynary/1245453 to your computer and use it in GitHub Desktop.
class TwitterReverseAuthController < ApplicationController
class OauthCredential < Struct.new(:token, :secret)
end
class TwitterAuthenticator
Error = Class.new(StandardError)
def initialize(options)
@options = options
end
def authenticate_device
user.devices.find_or_create_by_token!({
:token => oauth_credential.token,
:description => @options[:description]
})
end
private
def user
@user ||= User.find_for_oauth!(fetch_option(:screen_name), oauth_credential)
end
def oauth_credential
@oauth_credential ||= OauthCredential.new(fetch_option(:oauth_token), fetch_option(:oauth_secret))
end
def fetch_option(name)
@options.fetch(name) { raise ArgumentError, "#{name} is required" }
end
end
def api_key_exchange
authenticator = TwitterAuthenticator.new(params)
if (device = authenticator.authenticate_device)
render :json => { :api_key => device.api_key }
else
render :json => { :errors => authenticator.errrors }, :status => :unprocessable_entity
end
end
end
@ngauthier
Copy link

I much prefer James's ActiveModel::Validation extraction:

https://gist.github.com/1243758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment