From 67ede42fbe4c30f6b9d5372695b33042c2f4a573 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Thu, 7 Oct 2010 11:48:13 -0400 Subject: [PATCH] added Gowalla support --- oa-oauth/lib/omniauth/oauth.rb | 1 + oa-oauth/lib/omniauth/strategies/gowalla.rb | 60 +++++++++++++++++++ .../spec/omniauth/strategies/gowalla_spec.rb | 13 ++++ 3 files changed, 74 insertions(+) create mode 100644 oa-oauth/lib/omniauth/strategies/gowalla.rb create mode 100644 oa-oauth/spec/omniauth/strategies/gowalla_spec.rb diff --git a/oa-oauth/lib/omniauth/oauth.rb b/oa-oauth/lib/omniauth/oauth.rb index 7195a7603..696647c79 100644 --- a/oa-oauth/lib/omniauth/oauth.rb +++ b/oa-oauth/lib/omniauth/oauth.rb @@ -11,5 +11,6 @@ module Strategies autoload :GitHub, 'omniauth/strategies/github' autoload :ThirtySevenSignals, 'omniauth/strategies/thirty_seven_signals' autoload :Foursquare, 'omniauth/strategies/foursquare' + autoload :Gowalla, 'omniauth/strategies/gowalla' end end diff --git a/oa-oauth/lib/omniauth/strategies/gowalla.rb b/oa-oauth/lib/omniauth/strategies/gowalla.rb new file mode 100644 index 000000000..50b2af10f --- /dev/null +++ b/oa-oauth/lib/omniauth/strategies/gowalla.rb @@ -0,0 +1,60 @@ +require 'omniauth/oauth' +require 'multi_json' + +module OmniAuth + module Strategies + # + # Authenticate to Gowalla utilizing OAuth 2.0 and retrieve + # basic user information. + # + # Usage: + # + # use OmniAuth::Strategies::Gowalla, 'API Key', 'Secret Key' + # + # Options: + # + # :scope :: Extended permissions such as email and offline_access (which are the defaults). + class Gowalla < OAuth2 + def initialize(app, api_key, secret_key, options = {}) + options[:site] = 'https://api.gowalla.com/api/oauth' + options[:authorize_url] = 'https://gowalla.com/api/oauth/new' + options[:access_token_url] = 'https://api.gowalla.com/api/oauth/token' + super(app, :gowalla, api_key, secret_key, options) + end + + def user_data + @data ||= MultiJson.decode(@access_token.get("/users/me.json")) + end + + def request_phase(options = {}) + options[:scope] ||= "email,offline_access" + super(options) + end + + def user_info + { + 'name' => "#{user_data['first_name']} #{user_data['last_name']}", + 'nickname' => user_data["username"], + 'first_name' => user_data["first_name"], + 'last_name' => user_data["last_name"], + 'location' => user_data["hometown"], + 'description' => user_data["bio"], + 'image' => user_data["image_url"], + 'phone' => nil, + 'urls' => { + 'Gowalla' => "http://www.gowalla.com/#{user_data['url']}", + 'Website' => user_data["website"] + } + } + end + + def auth_hash + OmniAuth::Utils.deep_merge(super, { + 'uid' => user_data["url"].split('/').last, + 'user_info' => user_info, + 'extra' => {'user_hash' => user_data} + }) + end + end + end +end diff --git a/oa-oauth/spec/omniauth/strategies/gowalla_spec.rb b/oa-oauth/spec/omniauth/strategies/gowalla_spec.rb new file mode 100644 index 000000000..f6c5cb91d --- /dev/null +++ b/oa-oauth/spec/omniauth/strategies/gowalla_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe OmniAuth::Strategies::Gowalla do + + it 'should subclass OAuth2' do + OmniAuth::Strategies::Gowalla.should < OmniAuth::Strategies::OAuth2 + end + + it 'should initialize with just api key and secret key' do + lambda{OmniAuth::Strategies::Gowalla.new({},'api_key','secret_key')}.should_not raise_error + end + +end \ No newline at end of file