Permalink
Browse files

finish all interfaces

  • Loading branch information...
1 parent b639521 commit ead696e72b217e1de90992529a9ee4362c756aab @acenqiu committed Nov 3, 2011
View
@@ -37,6 +37,7 @@ GEM
multi_json (~> 1.0)
arel (2.2.1)
builder (3.0.0)
+ diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
@@ -75,11 +76,23 @@ GEM
rake (0.9.2.2)
rdoc (3.11)
json (~> 1.4)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ rspec-rails (2.6.1)
+ actionpack (~> 3.0)
+ activesupport (~> 3.0)
+ railties (~> 3.0)
+ rspec (~> 2.6.0)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
- sqlite3 (1.3.4)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -91,5 +104,5 @@ PLATFORMS
ruby
DEPENDENCIES
- sqlite3
+ rspec-rails
weibo2!
View
@@ -1,2 +1,16 @@
-module Weibo2
-end
+require 'weibo2/util'
+require 'weibo2/client'
+require 'weibo2/config'
+require 'weibo2/error'
+require 'weibo2/interface/base'
+require 'weibo2/interface/account'
+require 'weibo2/interface/comments'
+require 'weibo2/interface/favorites'
+require 'weibo2/interface/friendships'
+require 'weibo2/interface/register'
+require 'weibo2/interface/search'
+require 'weibo2/interface/statuses'
+require 'weibo2/interface/suggestions'
+require 'weibo2/interface/tags'
+require 'weibo2/interface/trends'
+require 'weibo2/interface/users'
View
@@ -0,0 +1,96 @@
+require 'oauth2'
+
+module Weibo2
+
+ # The Weibo2::Client class
+ class Client
+
+ attr_reader :key, :secret, :redirect_uri
+ attr_accessor :client, :token
+
+ def self.from_signed_request(signed_request, opts={}, &block)
+ client = self.new(opts, &block)
+ client.get_token_from_signed_request(signed_request)
+
+ client
+ end
+
+ #
+ def initialize(opts={}, &block)
+ @key = Weibo2::Config.api_key
+ @secret = Weibo2::Config.api_secret
+ @redirect_uri = Weibo2::Config.redirect_uri
+
+ options = {:site => "https://api.weibo.com/2/",
+ :authorize_url => "oauth2/authorize",
+ :token_url => "oauth2/access_token",
+ :raise_errors => false,
+ :ssl => {:verify => false}}.merge(opts)
+
+ @client = OAuth2::Client.new(@key, @secret, options, &block)
+ end
+
+ def authorize_url(params={})
+ client.auth_code.authorize_url({:redirect_uri => redirect_uri}.merge(params))
+ end
+
+ def get_token(code, params={})
+ @token = client.auth_code.get_token(code, {:redirect_uri => redirect_uri, :parse => :json}.merge(params), {:header_format => "OAuth2 %s"})
+ end
+
+ def get_token_from_signed_request(signed_request)
+ data = Weibo2::Util.parse_signed_request(signed_request, secret)
+ puts data.inspect
+ unless ["-1", "-2"].include? data
+ opts = {:expires_at => data["expires"],
+ :header_format => "OAuth2 %s"}
+ @token = OAuth2::AccessToken.new(client, data["oauth_token"], opts)
+ end
+ end
+
+
+ def account
+ @account ||= Weibo2::Interface::Account.new(self)
+ end
+
+ def comments
+ @comments ||= Weibo2::Interface::Comments.new(self)
+ end
+
+ def favorites
+ @favorites ||= Weibo2::Interface::Favorites.new(self)
+ end
+
+ def friendships
+ @friendships ||= Weibo2::Interface::Friendships.new(self)
+ end
+
+ def register
+ @register ||= Weibo2::Interface::Register.new(self)
+ end
+
+ def search
+ @search ||= Weibo2::Interface::Search.new(self)
+ end
+
+ def statuses
+ @statuses ||= Weibo2::Interface::Statuses.new(self)
+ end
+
+ def suggestions
+ @suggestions ||= Weibo2::Interface::Suggestions.new(self)
+ end
+
+ def tags
+ @tags ||= Weibo2::Interface::Tags.new(self)
+ end
+
+ def trends
+ @trends ||= Weibo2::Interface::Trends.new(self)
+ end
+
+ def users
+ @users ||= Weibo2::Interface::Users.new(self)
+ end
+ end
+end
View
@@ -0,0 +1,28 @@
+module Weibo2
+ module Config
+
+ def self.api_key=(val)
+ @@api_key = val
+ end
+
+ def self.api_key
+ @@api_key
+ end
+
+ def self.api_secret=(val)
+ @@api_secret = val
+ end
+
+ def self.api_secret
+ @@api_secret
+ end
+
+ def self.redirect_uri=(val)
+ @@redirect_uri = val
+ end
+
+ def self.redirect_uri
+ @@redirect_uri
+ end
+ end
+end
View
@@ -0,0 +1,12 @@
+module Weibo2
+ class Error < OAuth2::Error
+ def initialize(response)
+ response.error = self
+ @response = response
+ if response.parsed.is_a?(Hash)
+ @code = response.parsed['error_code']
+ @description = response.parsed['error']
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit ead696e

Please sign in to comment.