br_api_fantasy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +18 -0
- data/README.markdown +47 -0
- data/Rakefile +37 -0
- data/config/br_api_fantasy.example.yml +18 -0
- data/gemspec.rb +39 -0
- data/init.rb +1 -0
- data/lib/br_api_fantasy.rb +41 -0
- data/rails/init.rb +1 -0
- data/spec/br_api_fantasy_spec.rb +49 -0
- data/spec/example_app/config/br_api_fantasy.yml +16 -0
- data/spec/example_app/log/test.log +1 -0
- data/spec/test.log +1 -0
- metadata +109 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2009 Winton Welsh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Br Fantasy Api Plugin
|
2
|
+
================
|
3
|
+
|
4
|
+
Talk to Br Fantasy Api from your Rails app to get user info.
|
5
|
+
|
6
|
+
Installation:
|
7
|
+
------------
|
8
|
+
|
9
|
+
<pre>
|
10
|
+
$ gem install br_api_fantasy --no-ri --no-rdoc
|
11
|
+
</pre>
|
12
|
+
|
13
|
+
Copy br_api_fantasy.example.yml to rails_project/config/br_api_fantasy.yml. Update the api_key to the
|
14
|
+
one given to you by Bleacher Report.
|
15
|
+
|
16
|
+
Usage:
|
17
|
+
------------
|
18
|
+
|
19
|
+
After a user logs in or signs up from one of the Bleacher Report's fantasy branded pages, they will be redirected to the passthrough params[:return_to] url, which can be set by the Fantasy Games 3rd party, along with a token (user_token). The 3rd party can use the token to then generate the verified hash on their servers. BR will generate the same hash and compare against it to ensure it's coming from a trusted source.
|
20
|
+
|
21
|
+
The plugin contains the method to generate the api request, this method will use the api_key configured in br_api_fantasy.yml:
|
22
|
+
|
23
|
+
Br::Api::Fantasy.authenticate('user-token-returned-from-login-page')
|
24
|
+
|
25
|
+
Example:
|
26
|
+
------------
|
27
|
+
<pre>
|
28
|
+
$ ./script/console
|
29
|
+
>> pp Br::Api::Fantasy.authenticate('foobar-invalid-token')
|
30
|
+
{"response_details"=>"user with fantasy token foobar-invalid-token not found",
|
31
|
+
"response_data"=>nil,
|
32
|
+
"response_summary"=>"user not found",
|
33
|
+
"response_status"=>200}
|
34
|
+
=> nil
|
35
|
+
>> pp Br::Api::Fantasy.authenticate('8681576f47b87b2988bdc76262164cda00019986')
|
36
|
+
{"response_details"=>"signature successfully matched",
|
37
|
+
"response_data"=>
|
38
|
+
{"user_last_name"=>"McCormick",
|
39
|
+
"user_first_name"=>"Kenny",
|
40
|
+
"user_permalink"=>"http://breport.local/users/309596-kenny-mccormick",
|
41
|
+
"user_id"=>309596},
|
42
|
+
"response_summary"=>"login success",
|
43
|
+
"response_status"=>200}
|
44
|
+
=> nil
|
45
|
+
>>
|
46
|
+
</pre>
|
47
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
require 'gemspec'
|
6
|
+
|
7
|
+
desc "Generate gemspec"
|
8
|
+
task :gemspec do
|
9
|
+
File.open("#{Dir.pwd}/#{GEM_NAME}.gemspec", 'w') do |f|
|
10
|
+
f.write(GEM_SPEC.to_ruby)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
task :uninstall do
|
15
|
+
sudo = ENV['SUDO'].nil? ? '' : 'sudo '
|
16
|
+
`#{sudo} gem uninstall #{GEM_NAME} -x`
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Install gem"
|
20
|
+
task :install do
|
21
|
+
sudo = ENV['SUDO'].nil? ? '' : 'sudo '
|
22
|
+
Rake::Task['gem'].invoke
|
23
|
+
`#{sudo} gem install pkg/#{GEM_NAME}*.gem`
|
24
|
+
`rm -Rf pkg`
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Package gem"
|
28
|
+
Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
|
29
|
+
pkg.gem_spec = GEM_SPEC
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Run specs"
|
33
|
+
Spec::Rake::SpecTask.new do |t|
|
34
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
35
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# This belongs in rails_app/config/br_api_fantasy.yml. Adjust the api_key accordingly.
|
2
|
+
|
3
|
+
development: &defaults
|
4
|
+
api_uri: http://breport.local
|
5
|
+
api_key: "southparkrocks"
|
6
|
+
default_return_to: http://games.stagingbleacherreport.com
|
7
|
+
|
8
|
+
test:
|
9
|
+
<<: *defaults
|
10
|
+
|
11
|
+
staging:
|
12
|
+
api_uri: http://beta.stagingbleacherreport.com
|
13
|
+
<<: *defaults
|
14
|
+
|
15
|
+
production:
|
16
|
+
api_uri: http://beta.stagingbleacherreport.com
|
17
|
+
default_return_to: http://games.stagingbleacherreport.com
|
18
|
+
<<: *defaults
|
data/gemspec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
GEM_NAME = 'br_api_fantasy'
|
2
|
+
GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
|
3
|
+
GEM_SPEC = Gem::Specification.new do |s|
|
4
|
+
# == CONFIGURE ==
|
5
|
+
s.author = "Tung Nguyen"
|
6
|
+
s.email = "tongueroo@gmail.com"
|
7
|
+
s.homepage = "http://github.com/tongueroo/#{GEM_NAME}"
|
8
|
+
s.summary = ""
|
9
|
+
# == CONFIGURE ==
|
10
|
+
s.extra_rdoc_files = [ "README.markdown" ]
|
11
|
+
s.files = GEM_FILES.to_a
|
12
|
+
s.has_rdoc = false
|
13
|
+
s.name = GEM_NAME
|
14
|
+
s.platform = Gem::Platform::RUBY
|
15
|
+
s.require_path = "lib"
|
16
|
+
s.version = "0.1.0"
|
17
|
+
|
18
|
+
path = File.dirname(__FILE__)+"/config/br_api_fantasy.example.yml"
|
19
|
+
yaml_instructions = IO.read(path)
|
20
|
+
s.post_install_message = <<-POST_INSTALL_MESSAGE
|
21
|
+
#{'*'*50}
|
22
|
+
|
23
|
+
Thanks for installing br_api_fantasy-#{s.version}.
|
24
|
+
|
25
|
+
Please make sure to add the gem to your rails project.
|
26
|
+
|
27
|
+
Rails::Initializer.run do |config|
|
28
|
+
...
|
29
|
+
config.gem 'br_api_fantasy', :version => #{s.version}
|
30
|
+
...
|
31
|
+
end
|
32
|
+
|
33
|
+
Also, please make sure you have the following in rails_app/config/br_api_fantasy.yml.
|
34
|
+
|
35
|
+
#{yaml_instructions}
|
36
|
+
|
37
|
+
#{'*'*50}
|
38
|
+
POST_INSTALL_MESSAGE
|
39
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'yaml'
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module Br
|
6
|
+
module Api
|
7
|
+
class Fantasy
|
8
|
+
include HTTParty
|
9
|
+
|
10
|
+
cattr_accessor :config_path
|
11
|
+
cattr_accessor :credentails
|
12
|
+
self.config_path = File.join(RAILS_ROOT,'config','br_api_fantasy.yml')
|
13
|
+
|
14
|
+
def self.parse_creds
|
15
|
+
path = File.expand_path(self.config_path)
|
16
|
+
YAML.load(IO.read(path))[RAILS_ENV]
|
17
|
+
end
|
18
|
+
self.credentails = parse_creds
|
19
|
+
|
20
|
+
def self.sig(token)
|
21
|
+
Digest::SHA1.hexdigest("--#{credentails['api_key']}--#{token}--")
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.authenticate(token)
|
25
|
+
base_uri credentails['api_uri']
|
26
|
+
response = get '/api/fantasy/authenticate.json', :query => {:token => token, :sig => sig(token)}
|
27
|
+
if response.is_a?(Hash)
|
28
|
+
return response
|
29
|
+
elsif response.is_a?(String)
|
30
|
+
return {
|
31
|
+
:response_status => 500,
|
32
|
+
:response_details => "500 Error",
|
33
|
+
:response_data => response
|
34
|
+
}
|
35
|
+
else
|
36
|
+
raise "response is not a Hash or String #{response.class}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../lib/br_api_fantasy")
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# mock rails
|
2
|
+
require "rubygems"
|
3
|
+
require "activesupport"
|
4
|
+
require "logger"
|
5
|
+
RAILS_ENV="test"
|
6
|
+
RAILS_ROOT = File.expand_path(File.dirname(__FILE__)+"/example_app")
|
7
|
+
RAILS_DEFAULT_LOGGER=Logger.new(RAILS_ROOT+"/log/test.log")
|
8
|
+
|
9
|
+
require "lib/br_api_fantasy"
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
end
|
13
|
+
|
14
|
+
# stub out the httparty get request
|
15
|
+
puts "ENV['LIVE_TEST'] #{ENV['LIVE_TEST'].inspect}"
|
16
|
+
LIVE_TEST = !ENV['LIVE_TEST'].nil?
|
17
|
+
# note do do a live test, you'll need to grab a real token from the live api you are ping
|
18
|
+
unless LIVE_TEST
|
19
|
+
require 'httparty'
|
20
|
+
module HTTParty
|
21
|
+
def self.included(base)
|
22
|
+
base.send :extend, ClassMethods
|
23
|
+
end
|
24
|
+
module ClassMethods
|
25
|
+
def get(url, options)
|
26
|
+
{:mock_json => true}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Br::Api::Fantasy do
|
33
|
+
before(:each) do
|
34
|
+
Br::Api::Fantasy.config_path = RAILS_ROOT+"/config/br_api_fantasy.yml"
|
35
|
+
@token = "1bd31ce33c85ea5cdc3166166dbdd170e92dc3a5"
|
36
|
+
end
|
37
|
+
it "parse credentials" do
|
38
|
+
creds = Br::Api::Fantasy.parse_creds
|
39
|
+
creds['api_uri'].should == 'http://breport.local'
|
40
|
+
end
|
41
|
+
it "generate signature" do
|
42
|
+
sig = Br::Api::Fantasy.sig(@token)
|
43
|
+
sig.length.should == 40
|
44
|
+
end
|
45
|
+
it "authenticate" do
|
46
|
+
json = Br::Api::Fantasy.authenticate(@token)
|
47
|
+
json.should be_kind_of(Hash)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
development: &defaults
|
2
|
+
api_uri: http://breport.local
|
3
|
+
api_key: "southparkrocks"
|
4
|
+
default_return_to: http://games.stagingbleacherreport.com
|
5
|
+
|
6
|
+
test:
|
7
|
+
<<: *defaults
|
8
|
+
|
9
|
+
staging:
|
10
|
+
api_uri: http://beta.stagingbleacherreport.com
|
11
|
+
<<: *defaults
|
12
|
+
|
13
|
+
production:
|
14
|
+
api_uri: http://beta.stagingbleacherreport.com
|
15
|
+
default_return_to: http://games.stagingbleacherreport.com
|
16
|
+
<<: *defaults
|
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Mon Jul 12 20:59:58 -0700 2010 by logger.rb/22283
|
data/spec/test.log
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Mon Jul 12 20:56:09 -0700 2010 by logger.rb/22283
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: br_api_fantasy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tung Nguyen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-13 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: tongueroo@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.markdown
|
29
|
+
files:
|
30
|
+
- config/br_api_fantasy.example.yml
|
31
|
+
- gemspec.rb
|
32
|
+
- init.rb
|
33
|
+
- lib/br_api_fantasy.rb
|
34
|
+
- MIT-LICENSE
|
35
|
+
- rails/init.rb
|
36
|
+
- Rakefile
|
37
|
+
- README.markdown
|
38
|
+
- spec/br_api_fantasy_spec.rb
|
39
|
+
- spec/example_app/config/br_api_fantasy.yml
|
40
|
+
- spec/example_app/log/test.log
|
41
|
+
- spec/test.log
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/tongueroo/br_api_fantasy
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message: |
|
47
|
+
**************************************************
|
48
|
+
|
49
|
+
Thanks for installing br_api_fantasy-0.1.0.
|
50
|
+
|
51
|
+
Please make sure to add the gem to your rails project.
|
52
|
+
|
53
|
+
Rails::Initializer.run do |config|
|
54
|
+
...
|
55
|
+
config.gem 'br_api_fantasy', :version => 0.1.0
|
56
|
+
...
|
57
|
+
end
|
58
|
+
|
59
|
+
Also, please make sure you have the following in rails_app/config/br_api_fantasy.yml.
|
60
|
+
|
61
|
+
# This belongs in rails_app/config/br_api_fantasy.yml. Adjust the api_key accordingly.
|
62
|
+
|
63
|
+
development: &defaults
|
64
|
+
api_uri: http://breport.local
|
65
|
+
api_key: "southparkrocks"
|
66
|
+
default_return_to: http://games.stagingbleacherreport.com
|
67
|
+
|
68
|
+
test:
|
69
|
+
<<: *defaults
|
70
|
+
|
71
|
+
staging:
|
72
|
+
api_uri: http://beta.stagingbleacherreport.com
|
73
|
+
<<: *defaults
|
74
|
+
|
75
|
+
production:
|
76
|
+
api_uri: http://beta.stagingbleacherreport.com
|
77
|
+
default_return_to: http://games.stagingbleacherreport.com
|
78
|
+
<<: *defaults
|
79
|
+
|
80
|
+
|
81
|
+
**************************************************
|
82
|
+
|
83
|
+
rdoc_options: []
|
84
|
+
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.6
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: ""
|
108
|
+
test_files: []
|
109
|
+
|