From 7af0335827efc7716c99b3adf37dbc8fe3f61f58 Mon Sep 17 00:00:00 2001 From: Steve Agalloco Date: Mon, 20 Feb 2012 10:26:47 -0500 Subject: [PATCH] initial commit --- .gemtest | 0 .gitignore | 40 +++++++++++++ .rspec | 3 + .simplecov | 1 + .yardopts | 3 + Gemfile | 3 + LICENSE.md | 20 +++++++ README.md | 46 +++++++++++++++ Rakefile | 18 ++++++ example/Gemfile | 5 ++ example/config.ru | 30 ++++++++++ lib/omniauth-tradeking.rb | 1 + lib/omniauth/strategies/tradeking.rb | 51 ++++++++++++++++ lib/omniauth/tradeking.rb | 1 + lib/omniauth/tradeking/version.rb | 5 ++ omniauth-tradeking.gemspec | 28 +++++++++ spec/integration_spec.rb | 5 ++ spec/omniauth/strategies/tradeking_spec.rb | 69 ++++++++++++++++++++++ spec/spec_helper.rb | 6 ++ 19 files changed, 335 insertions(+) create mode 100644 .gemtest create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 .simplecov create mode 100644 .yardopts create mode 100644 Gemfile create mode 100644 LICENSE.md create mode 100644 README.md create mode 100755 Rakefile create mode 100644 example/Gemfile create mode 100644 example/config.ru create mode 100644 lib/omniauth-tradeking.rb create mode 100644 lib/omniauth/strategies/tradeking.rb create mode 100644 lib/omniauth/tradeking.rb create mode 100644 lib/omniauth/tradeking/version.rb create mode 100644 omniauth-tradeking.gemspec create mode 100644 spec/integration_spec.rb create mode 100644 spec/omniauth/strategies/tradeking_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gemtest b/.gemtest new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d00bb9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +*.gem +*.rbc +*.sw[a-p] +*.tmproj +*.tmproject +*.un~ +*~ +.DS_Store +.Spotlight-V100 +.Trashes +._* +.bundle +.config +.directory +.elc +.emacs.desktop +.emacs.desktop.lock +.redcar +.yardoc +Desktop.ini +Gemfile.lock +Icon? +InstalledFiles +Session.vim +Thumbs.db +\#*\# +_yardoc +auto-save-list +coverage +doc +lib/bundler/man +pkg +pkg/* +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +tmtags +tramp diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..bb259fe --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--color +--format=nested +--backtrace diff --git a/.simplecov b/.simplecov new file mode 100644 index 0000000..34fc1b2 --- /dev/null +++ b/.simplecov @@ -0,0 +1 @@ +SimpleCov.start \ No newline at end of file diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..dbcb341 --- /dev/null +++ b/.yardopts @@ -0,0 +1,3 @@ +--markup markdown +- +LICENSE.md diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..d65e2a6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'http://rubygems.org' + +gemspec diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..1210b40 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +Copyright (c) 2012 Steve Agalloco, Jason Barry + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d296d0 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# OmniAuth TradeKing + +This gem is an OmniAuth 1.0 Strategy for the [TradeKing API](https://developers.tradeking.com) + +## Usage + +Add the strategy to your `Gemfile` alongside OmniAuth: + +```ruby +gem 'omniauth' +gem 'omniauth-tradeking' +``` + +Then integrate the strategy into your middleware: + +```ruby +use OmniAuth::Builder do + provider :tradeking, ENV['TRADEKING_CONSUMER_KEY_'], ENV['TRADEKING_CONSUMER_SECRET'] +end +``` + +In Rails, you'll want to add to the middleware stack: + +```ruby +Rails.application.config.middleware.use OmniAuth::Builder do + provider :tradeking, ENV['TRADEKING_CONSUMER_KEY_'], ENV['TRADEKING_CONSUMER_SECRET'] +end +``` + +You will have to put in your consumer key and secret. + +For additional information, refer to the [OmniAuth wiki](https://github.com/intridea/omniauth/wiki). + +## Note on Patches/Pull Requests + +* Fork the project. +* Make your feature addition or bug fix. +* Add tests for it. This is important so I don't break it in a + future version unintentionally. +* Commit, do not mess with rakefile, version, or history. + (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) +* Send me a pull request. Bonus points for topic branches. + +## Copyright + +Copyright (c) 2012 Steve Agalloco, Jason Barry. See [LICENSE](https://github.com/jcbarry/omniauth-tradeking/blob/master/LICENSE.md) for details. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100755 index 0000000..3616e54 --- /dev/null +++ b/Rakefile @@ -0,0 +1,18 @@ +#!/usr/bin/env rake + +require 'bundler' +Bundler::GemHelper.install_tasks + +require 'rspec/core/rake_task' +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec +task :test => :spec + +require 'yard' +namespace :doc do + YARD::Rake::YardocTask.new do |task| + task.files = ['LICENSE.md', 'lib/**/*.rb'] + task.options = ['--markup', 'markdown'] + end +end diff --git a/example/Gemfile b/example/Gemfile new file mode 100644 index 0000000..1afef42 --- /dev/null +++ b/example/Gemfile @@ -0,0 +1,5 @@ +source :rubygems + +gem 'sinatra' +gem 'multi_json' +gem 'omniauth-tradeking', :path => '../' \ No newline at end of file diff --git a/example/config.ru b/example/config.ru new file mode 100644 index 0000000..12af325 --- /dev/null +++ b/example/config.ru @@ -0,0 +1,30 @@ +require 'bundler/setup' +require 'sinatra/base' +require 'omniauth-tradeking' + +ENV['CONSUMER_KEY'] = 'p8c1ZAnEBSdOcEL3ut5oJeOKPIixp4TlLf7uyHIy' +ENV['CONSUMER_SECRET'] = 'QIFWJuKYmXV8yW7hvfyZALpTIPSk8vrA3aKbAqcZ' + +class App < Sinatra::Base + get '/' do + redirect '/auth/tradeking' + end + + get '/auth/:provider/callback' do + content_type 'application/json' + MultiJson.encode(request.env) + end + + get '/auth/failure' do + content_type 'application/json' + MultiJson.encode(request.env) + end +end + +use Rack::Session::Cookie + +use OmniAuth::Builder do + provider :tradeking, ENV['CONSUMER_KEY'], ENV['CONSUMER_SECRET'] +end + +run App.new \ No newline at end of file diff --git a/lib/omniauth-tradeking.rb b/lib/omniauth-tradeking.rb new file mode 100644 index 0000000..f13084c --- /dev/null +++ b/lib/omniauth-tradeking.rb @@ -0,0 +1 @@ +require 'omniauth/tradeking' \ No newline at end of file diff --git a/lib/omniauth/strategies/tradeking.rb b/lib/omniauth/strategies/tradeking.rb new file mode 100644 index 0000000..4fa6b41 --- /dev/null +++ b/lib/omniauth/strategies/tradeking.rb @@ -0,0 +1,51 @@ +require 'omniauth-oauth' + +module OmniAuth + module Strategies + class TradeKing < OmniAuth::Strategies::OAuth + option :client_options, { + :site => 'https://api.tradeking.com', + :authorize_url => 'https://developers.tradeking.com/oauth/authorize', + :request_token_url => 'https://developers.tradeking.com/oauth/request_token', + :access_token_url => 'https://developers.tradeking.com/oauth/access_token' + } + + uid { login_id } + + info do + { + 'uid' => login_id, + 'name' => "#{first_name} #{last_name}", + 'email' => email + } + end + + extra do + { 'raw_info' => raw_info } + end + + def raw_info + @raw_info ||= MultiJson.decode(access_token.get('https://api.tradeking.com/v1/member/profile.json').body)['response'] + end + + def first_name + raw_info['userdata']['userprofile']['entry'].select { |r| r['name'] == 'primaryFirstName' }.first['value'] + end + + def last_name + raw_info['userdata']['userprofile']['entry'].select { |r| r['name'] == 'primaryLastName' }.first['value'] + end + + def email + raw_info['userdata']['userprofile']['entry'].select { |r| r['name'] == 'emailAddress1' }.first['value'] + end + + def login_id + raw_info['userdata']['userprofile']['entry'].select { |r| r['name'] == 'login_id' }.first['value'] + end + + end + end +end + +OmniAuth.config.add_camelization 'tradeking', 'TradeKing' \ No newline at end of file diff --git a/lib/omniauth/tradeking.rb b/lib/omniauth/tradeking.rb new file mode 100644 index 0000000..d652e3a --- /dev/null +++ b/lib/omniauth/tradeking.rb @@ -0,0 +1 @@ +require 'omniauth/strategies/tradeking' \ No newline at end of file diff --git a/lib/omniauth/tradeking/version.rb b/lib/omniauth/tradeking/version.rb new file mode 100644 index 0000000..83ba1a7 --- /dev/null +++ b/lib/omniauth/tradeking/version.rb @@ -0,0 +1,5 @@ +module OmniAuth + module TradeKing + VERSION = "0.1.0" + end +end diff --git a/omniauth-tradeking.gemspec b/omniauth-tradeking.gemspec new file mode 100644 index 0000000..4b92f7a --- /dev/null +++ b/omniauth-tradeking.gemspec @@ -0,0 +1,28 @@ +# encoding: utf-8 +require File.expand_path('../lib/omniauth/tradeking/version', __FILE__) + +Gem::Specification.new do |gem| + gem.name = 'omniauth-tradeking' + gem.version = OmniAuth::TradeKing::VERSION + gem.homepage = 'https://github.com/jcbarry/omniauth-tradeking' + + gem.author = "Steve Agalloco" + gem.email = 'steve.agalloco@gmail.com' + gem.description = 'TradeKing strategy for OmniAuth 1.0' + gem.summary = gem.description + + gem.add_dependency "omniauth-oauth", "~> 1.0" + + gem.add_development_dependency 'rake', '~> 0.9' + gem.add_development_dependency 'rdiscount', '~> 1.6' + gem.add_development_dependency 'rspec', '~> 2.7' + gem.add_development_dependency 'simplecov', '~> 0.5' + gem.add_development_dependency 'yard', '~> 0.7' + gem.add_development_dependency 'webmock', '~> 1.7' + + gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)} + gem.files = `git ls-files`.split("\n") + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + + gem.require_paths = ['lib'] +end diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb new file mode 100644 index 0000000..c6d053a --- /dev/null +++ b/spec/integration_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe 'OmniAuth::Strategies::TradeKing Integration' do + pending 'write some tests yo' +end \ No newline at end of file diff --git a/spec/omniauth/strategies/tradeking_spec.rb b/spec/omniauth/strategies/tradeking_spec.rb new file mode 100644 index 0000000..fbf35f4 --- /dev/null +++ b/spec/omniauth/strategies/tradeking_spec.rb @@ -0,0 +1,69 @@ +require 'spec_helper' + +describe OmniAuth::Strategies::TradeKing do + before :each do + @request = double('Request') + @request.stub(:params) { {} } + end + + subject do + OmniAuth::Strategies::TradeKing.new(nil, @options || {}).tap do |strategy| + strategy.stub(:request) { @request } + end + end + + describe '#client_options' do + it 'has correct TradeKing site' do + subject.options.client_options.site.should eq('https://api.tradeking.com') + end + + it 'has correct authorize url' do + subject.options.client_options.authorize_url.should eq('https://developers.tradeking.com/oauth/authorize') + end + + it 'has correct request token url' do + subject.options.client_options.request_token_url.should eq('https://developers.tradeking.com/oauth/request_token') + end + + it 'has correct access token url' do + subject.options.client_options.access_token_url.should eq('https://developers.tradeking.com/oauth/access_token') + end + end + + describe '#uid' do + it 'returns the uid from raw_info' do + subject.stub(:login_id) { 'omgbbqlol' } + subject.uid.should eq('omgbbqlol') + end + end + + describe '#info' do + before :each do + subject.stub(:first_name) { 'James' } + subject.stub(:last_name) { 'Brown' } + subject.stub(:email) { 'jbrown@gmail.com' } + subject.stub(:login_id) { '12345' } + end + + context 'when data is present in raw info' do + it 'returns the name' do + subject.info['name'].should eq('James Brown') + end + + it 'returns the email' do + subject.info['email'].should eq('jbrown@gmail.com') + end + end + end + + describe '#extra' do + before :each do + @raw_info_hash = { "accounts" => [] } + subject.stub(:raw_info) { @raw_info_hash } + end + + it 'returns a Hash' do + subject.extra.should be_a(Hash) + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d571fa7 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +$:.unshift File.expand_path('..', __FILE__) +$:.unshift File.expand_path('../../lib', __FILE__) +require 'simplecov' +require 'omniauth-tradeking' +require 'rspec' +require 'webmock/rspec'