Permalink
Browse files

append strategy weibo, and update weibo2, sepc testing scripts

  • Loading branch information...
1 parent d31072d commit 3ad8efaf1dc577995a7928602d5234ec3b6c0228 @xixilive xixilive committed Dec 14, 2011
View
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+nbproject
View
@@ -4,9 +4,5 @@ source 'http://rubygems.org'
gemspec
group :development, :test do
- gem 'guard'
- gem 'guard-rspec'
- gem 'guard-bundler'
- gem 'rb-fsevent'
- gem 'growl'
+
end
View
@@ -1,10 +0,0 @@
-guard 'rspec', :version => 2 do
- watch(%r{^spec/.+_spec\.rb$})
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
- watch('spec/spec_helper.rb') { "spec" }
-end
-
-guard 'bundler' do
- watch('Gemfile')
- watch('omniauth-github.gemspec')
-end
View
@@ -1,13 +1,16 @@
# OmniAuth Weibo2
-**Note:** This gem is designed to work with weibo.com v2 which authorize via OAuth2
+**Note:** This gem is designed to work with weibo.com,
+include double strategies weibo and weibo2, weibo is based-on omniauth-oauth,
+and weibo2 is based-on omniauth-oauth2
## Getting Started
This is an example that you might put into a Rails initializer at
`config/initializers/omniauth.rb`:
```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
+ provider :weibo, "APP_KEY", "APP_SECRET"
provider :weibo2, "APP_KEY", "APP_SECRET"
end
```
@@ -1,5 +1,5 @@
module OmniAuth
module Weibo2
- VERSION = "1.0.0"
+ VERSION = "1.0.1"
end
end
@@ -0,0 +1,46 @@
+require 'omniauth-oauth'
+
+module OmniAuth
+ module Strategies
+ class Weibo < OmniAuth::Strategies::OAuth
+
+ option :name, "weibo"
+
+ option :client_options, {
+ :access_token_path => '/oauth/access_token',
+ :authorize_path => '/oauth/authorize',
+ :request_token_path => '/oauth/request_token/',
+ :site => 'http://api.t.sina.com.cn'
+ }
+
+ info do
+ {
+ 'nickname' => raw_info['screen_name'],
+ 'email' => "#{raw_info['id']}@weibo.com",
+ 'name' => raw_info['name'],
+ 'location' => raw_info['location'],
+ 'description' => raw_info['description'],
+ 'image' => raw_info['profile_image_url'],
+ 'urls' => {
+ 'weibo' => raw_info['domain'].present?? "http://weibo.com/#{raw_info['domain']}" : "http://weibo.com/u/#{raw_info['id']}",
+ 'blog' => raw_info['url']
+ }
+ }
+ end
+
+ uid { raw_info["id"] }
+
+ extra do
+ { 'raw_info' => raw_info }
+ end
+
+ def raw_info
+ access_token.options[:mode] = :query
+ @raw_info ||= access_token.get("/account/verify_credentials.json").parsed
+ end
+
+ end
+ end
+end
+
+OmniAuth.config.add_camelization 'weibo', 'Weibo'
@@ -4,10 +4,8 @@ module Strategies
class Weibo2 < OmniAuth::Strategies::OAuth2
option :client_options, {
:site => 'https://api.weibo.com',
- :authorize_url => 'https://api.weibo.com/oauth2/authorize',
- :token_url => 'https://api.weibo.com/oauth2/access_token',
- #:get_uid_url => 'https://api.weibo.com/2/account/get_uid.json',
- :raw_info_url => 'http://api.t.sina.com.cn/account/verify_credentials.json'
+ :authorize_path => '/oauth2/authorize',
+ :access_token_path => '/oauth2/access_token'
}
def request_phase
@@ -37,10 +35,13 @@ def request_phase
def raw_info
access_token.options[:mode] = :query
- #@uid ||= access_token.get(default_options[:client_options][:get_uid_url]).parsed
- @raw_info ||= access_token.get("#{default_options[:client_options][:raw_info_url]}").parsed
+ @uid ||= access_token.get('/2/account/get_uid.json').parsed["uid"]
+ @raw_info ||= access_token.get("/2/users/show.json?uid=#{@uid}").parsed
end
+
end
+
+
end
end
@@ -2,20 +2,19 @@ require File.expand_path('../lib/omniauth-weibo2/version', __FILE__)
Gem::Specification.new do |gem|
gem.add_dependency 'omniauth', '~> 1.0'
+ gem.add_dependency 'omniauth-oauth', '~> 1.0'
gem.add_dependency 'omniauth-oauth2', '~> 1.0'
- gem.add_development_dependency 'rspec', '~> 2.7'
- gem.add_development_dependency 'rack-test'
- gem.add_development_dependency 'webmock'
- gem.add_development_dependency 'simplecov'
+ gem.add_development_dependency 'rspec', '~> 2.7'
+
gem.authors = ["Mickey"]
gem.email = ["xixilive@gmail.com"]
- gem.description = %q{This gem is designed to work with weibo.com version 2}
- gem.summary = %q{OmniAuth strategy for weibo.com v2}
+ gem.description = %q{This gem is designed to work with weibo.com,include 2 strategies, weibo and weibo2}
+ gem.summary = %q{OmniAuth strategy for weibo.com,include weibo and weibo2}
gem.homepage = 'http://github.com/xixilive/omniauth-weibo2'
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.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "omniauth-weibo2"
gem.require_paths = ["lib"]
gem.version = OmniAuth::Weibo2::VERSION
@@ -1,7 +1,98 @@
require 'spec_helper'
+require 'omniauth/strategies/weibo2'
describe OmniAuth::Strategies::Weibo2 do
- it 'should do some testing' do
- pending
+
+ before :each do
+ @request = double('Request')
+ @request.stub(:params) { {} }
+ end
+
+ subject do
+ OmniAuth::Strategies::Weibo2.new(nil, @options || {}).tap do |strategy|
+ strategy.stub(:request) { @request }
+ end
+ end
+
+ describe '#client_options' do
+ it 'has correct site' do
+ subject.options.client_options[:site].should eq('https://api.weibo.com')
+ end
+
+ it 'has correct authorize path and url' do
+ subject.options.client_options[:authorize_path].should eq('/oauth2/authorize')
+ end
+
+ it 'has correct access token path and url' do
+ subject.options.client_options[:access_token_path].should eq('/oauth2/access_token')
+ end
+ end
+
+ describe '#uid' do
+ it 'returns the uid from raw_info' do
+ subject.stub(:raw_info) {{"id"=>12345678}}
+ subject.uid.should eq(12345678)
+ end
end
+
+ describe '#info' do
+ before :each do
+ @raw_info_hash = {
+ "id"=>12345678,
+ "screen_name"=>"xixilive_nick",
+ "name"=>"xixilive",
+ "province"=>"11",
+ "city"=>"8",
+ "location"=>"北京海淀区",
+ "description"=>"description content",
+ "url"=>"http://xixilive.blog.163.com",
+ "profile_image_url"=>"http://tp2.sinaimg.cn/12345678/50/1281523744",
+ "domain"=>"xixilive"
+ }
+ subject.stub(:raw_info) { @raw_info_hash }
+ end
+
+ context 'when data is present in raw info' do
+ it 'returns the name' do
+ subject.info['name'].should eq('xixilive')
+ end
+
+ it 'returns the nickname' do
+ subject.info['nickname'].should eq('xixilive_nick')
+ end
+
+ it 'returns the email' do
+ subject.info['email'].should eq('12345678@weibo.com')
+ end
+
+ it 'returns the location' do
+ subject.info['location'].should eq('北京海淀区')
+ end
+
+ it 'returns the location' do
+ subject.info['location'].should eq('北京海淀区')
+ end
+
+ it 'returns the description' do
+ subject.info['description'].should eq('description content')
+ end
+
+ it 'returns the urls' do
+ subject.info['urls']['weibo'].should eq('http://weibo.com/xixilive')
+ subject.info['urls']['blog'].should eq('http://xixilive.blog.163.com')
+ end
+
+ end
+ end
+
+ describe '#extra' do
+ before :each do
+ subject.stub(:raw_info) { {"name"=>"xixilive"} }
+ end
+
+ it 'returns a Hash' do
+ subject.extra.should be_a(Hash)
+ end
+ end
+
end
@@ -0,0 +1,96 @@
+require 'spec_helper'
+require 'omniauth/strategies/weibo'
+
+describe OmniAuth::Strategies::Weibo do
+ before :each do
+ @request = double('Request')
+ @request.stub(:params) { {} }
+ end
+
+ subject do
+ OmniAuth::Strategies::Weibo.new(nil, @options || {}).tap do |strategy|
+ strategy.stub(:request) { @request }
+ end
+ end
+
+ describe '#client_options' do
+ it 'has correct site' do
+ subject.options.client_options[:site].should eq('http://api.t.sina.com.cn')
+ end
+
+ it 'has correct authorize path and url' do
+ subject.options.client_options[:authorize_path].should eq('/oauth/authorize')
+ end
+
+ it 'has correct access token path and url' do
+ subject.options.client_options[:access_token_path].should eq('/oauth/access_token')
+ end
+ end
+
+ describe '#uid' do
+ it 'returns the uid from raw_info' do
+ subject.stub(:raw_info) {{"id"=>12345678}}
+ subject.uid.should eq(12345678)
+ end
+ end
+
+ describe '#info' do
+ before :each do
+ @raw_info_hash = {
+ "id"=>12345678,
+ "screen_name"=>"xixilive_nick",
+ "name"=>"xixilive",
+ "province"=>"11",
+ "city"=>"8",
+ "location"=>"北京海淀区",
+ "description"=>"description content",
+ "url"=>"http://xixilive.blog.163.com",
+ "profile_image_url"=>"http://tp2.sinaimg.cn/12345678/50/1281523744",
+ "domain"=>"xixilive"
+ }
+ subject.stub(:raw_info) { @raw_info_hash }
+ end
+
+ context 'when data is present in raw info' do
+ it 'returns the name' do
+ subject.info['name'].should eq('xixilive')
+ end
+
+ it 'returns the nickname' do
+ subject.info['nickname'].should eq('xixilive_nick')
+ end
+
+ it 'returns the email' do
+ subject.info['email'].should eq('12345678@weibo.com')
+ end
+
+ it 'returns the location' do
+ subject.info['location'].should eq('北京海淀区')
+ end
+
+ it 'returns the location' do
+ subject.info['location'].should eq('北京海淀区')
+ end
+
+ it 'returns the description' do
+ subject.info['description'].should eq('description content')
+ end
+
+ it 'returns the urls' do
+ subject.info['urls']['weibo'].should eq('http://weibo.com/xixilive')
+ subject.info['urls']['blog'].should eq('http://xixilive.blog.163.com')
+ end
+
+ end
+ end
+
+ describe '#extra' do
+ before :each do
+ subject.stub(:raw_info) { {"name"=>"xixilive"} }
+ end
+
+ it 'returns a Hash' do
+ subject.extra.should be_a(Hash)
+ end
+ end
+end
View
@@ -1,18 +1,18 @@
$:.unshift File.expand_path('..', __FILE__)
$:.unshift File.expand_path('../../lib', __FILE__)
-require 'simplecov'
-SimpleCov.start
require 'rspec'
-require 'rack/test'
-require 'webmock/rspec'
require 'omniauth'
+require 'omniauth-oauth'
require 'omniauth-oauth2'
RSpec.configure do |config|
- config.include WebMock::API
- config.include Rack::Test::Methods
- config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
+ #
end
+class String
+ def present?
+ return !self.nil? && !self.empty?
+ end
+end

0 comments on commit 3ad8efa

Please sign in to comment.