Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stve committed Feb 20, 2012
0 parents commit 7af0335
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 0 deletions.
Empty file added .gemtest
Empty file.
40 changes: 40 additions & 0 deletions .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
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--color
--format=nested
--backtrace
1 change: 1 addition & 0 deletions .simplecov
@@ -0,0 +1 @@
SimpleCov.start
3 changes: 3 additions & 0 deletions .yardopts
@@ -0,0 +1,3 @@
--markup markdown
-
LICENSE.md
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'http://rubygems.org'

gemspec
20 changes: 20 additions & 0 deletions 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.
46 changes: 46 additions & 0 deletions 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.
18 changes: 18 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions example/Gemfile
@@ -0,0 +1,5 @@
source :rubygems

gem 'sinatra'
gem 'multi_json'
gem 'omniauth-tradeking', :path => '../'
30 changes: 30 additions & 0 deletions 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
1 change: 1 addition & 0 deletions lib/omniauth-tradeking.rb
@@ -0,0 +1 @@
require 'omniauth/tradeking'
51 changes: 51 additions & 0 deletions 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'
1 change: 1 addition & 0 deletions lib/omniauth/tradeking.rb
@@ -0,0 +1 @@
require 'omniauth/strategies/tradeking'
5 changes: 5 additions & 0 deletions lib/omniauth/tradeking/version.rb
@@ -0,0 +1,5 @@
module OmniAuth
module TradeKing
VERSION = "0.1.0"
end
end
28 changes: 28 additions & 0 deletions 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
5 changes: 5 additions & 0 deletions spec/integration_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe 'OmniAuth::Strategies::TradeKing Integration' do
pending 'write some tests yo'
end
69 changes: 69 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions 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'

0 comments on commit 7af0335

Please sign in to comment.