Class: Weibo2::Client

Inherits:
OAuth2::Client
  • Object
show all
Defined in:
lib/weibo2/client.rb

Overview

The Weibo2::Client class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|builder| ... } ⇒ Client

Initializes a new Client

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create the client with

Options Hash (opts):

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

Yields:

  • (builder)

    The Faraday connection builder



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/weibo2/client.rb', line 59

def initialize(opts={}, &block)
  id = 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)
      
  super(id, secret, options, &block)
end

Instance Attribute Details

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/weibo2/client.rb', line 8

def redirect_uri
  @redirect_uri
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/weibo2/client.rb', line 9

def token
  @token
end

Class Method Details

.from_code(code, opts = {}) {|builder| ... } ⇒ Object

Initializes a new Client from a signed_request

Parameters:

  • code (String)

    The Authorization Code value

  • opts (Hash) (defaults to: {})

    the options to create the client with

Options Hash (opts):

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

Yields:

  • (builder)

    The Faraday connection builder



18
19
20
21
22
23
# File 'lib/weibo2/client.rb', line 18

def self.from_code(code, opts={}, &block)
  client = self.new(opts, &block)
  client.auth_code.get_token(code)
  
  client
end

.from_hash(hash, opts = {}) {|builder| ... } ⇒ Object

Initializes a new Client from a hash

Parameters:

  • a (Hash)

    Hash contains access_token and expires

  • opts (Hash) (defaults to: {})

    the options to create the client with

Options Hash (opts):

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

Yields:

  • (builder)

    The Faraday connection builder



46
47
48
49
50
51
# File 'lib/weibo2/client.rb', line 46

def self.from_hash(hash, opts={}, &block)
  client = self.new(opts, &block)
  client.get_token_from_hash(hash)
  
  client
end

.from_signed_request(signed_request, opts = {}) {|builder| ... } ⇒ Object

Initializes a new Client from a signed_request

Parameters:

  • signed_request (String)

    param posted by Sina

  • opts (Hash) (defaults to: {})

    the options to create the client with

Options Hash (opts):

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

Yields:

  • (builder)

    The Faraday connection builder



32
33
34
35
36
37
# File 'lib/weibo2/client.rb', line 32

def self.from_signed_request(signed_request, opts={}, &block)
  client = self.new(opts, &block)
  client.signed_request.get_token(signed_request)
  
  client
end

Instance Method Details

#accountObject

APIs



142
143
144
# File 'lib/weibo2/client.rb', line 142

def 
  @account ||= Weibo2::Interface::Account.new(self)
end

#auth_codeObject

The Authorization Code strategy



120
121
122
# File 'lib/weibo2/client.rb', line 120

def auth_code
  @auth_code ||= Weibo2::Strategy::AuthCode.new(self)
end

#commentsObject



146
147
148
# File 'lib/weibo2/client.rb', line 146

def comments
  @comments ||= Weibo2::Interface::Comments.new(self)
end

#favoritesObject



150
151
152
# File 'lib/weibo2/client.rb', line 150

def favorites
  @favorites ||= Weibo2::Interface::Favorites.new(self)
end

#friendshipsObject



154
155
156
# File 'lib/weibo2/client.rb', line 154

def friendships
  @friendships ||= Weibo2::Interface::Friendships.new(self)
end

#get_token(params, access_token_opts = {}) ⇒ AccessToken

Initializes an AccessToken by making a request to the token endpoint

Parameters:

  • params (Hash)

    a Hash of params for the token endpoint

  • access (Hash)

    token options, to pass to the AccessToken object

Returns:

  • (AccessToken)

    the initalized AccessToken



85
86
87
88
89
90
# File 'lib/weibo2/client.rb', line 85

def get_token(params, access_token_opts={})
  params = params.merge(:parse => :json)
  access_token_opts = access_token_opts.merge({:header_format => "OAuth2 %s", :param_name => "access_token"})
  
  @token = super(params, access_token_opts)
end

#get_token_from_hash(hash) ⇒ AccessToken

Initializes an AccessToken from a hash

Parameters:

  • hash (Hash)

    a Hash contains access_token and expires

Returns:

  • (AccessToken)

    the initalized AccessToken



96
97
98
99
100
101
102
103
# File 'lib/weibo2/client.rb', line 96

def get_token_from_hash(hash)
  access_token = hash.delete('access_token') || hash.delete(:access_token) || hash.delete('oauth_token') || hash.delete(:oauth_token)
  opts = {:expires_at => hash["expires"] || hash[:expires],
          :header_format => "OAuth2 %s",
          :param_name => "access_token"}
  
  @token = OAuth2::AccessToken.new(self, access_token, opts)
end

#is_authorized?Boolean

Whether or not the client is authorized

Returns:

  • (Boolean)


76
77
78
# File 'lib/weibo2/client.rb', line 76

def is_authorized?
  !!token && !token.expired?
end

#passwordObject

The Resource Owner Password Credentials strategy



127
128
129
# File 'lib/weibo2/client.rb', line 127

def password
  super
end

#refresh!(params = {}) ⇒ AccessToken

Note:

options should be carried over to the new AccessToken

Refreshes the current Access Token

Returns:

  • (AccessToken)

    a new AccessToken



109
110
111
# File 'lib/weibo2/client.rb', line 109

def refresh!(params={})
  @token = token.refresh!(params)
end

#registerObject



158
159
160
# File 'lib/weibo2/client.rb', line 158

def register
  @register ||= Weibo2::Interface::Register.new(self)
end

#searchObject



162
163
164
# File 'lib/weibo2/client.rb', line 162

def search
  @search ||= Weibo2::Interface::Search.new(self)
end

#signed_requestObject

The Signed Request Strategy



134
135
136
# File 'lib/weibo2/client.rb', line 134

def signed_request
  @signed_request ||= Weibo2::Strategy::SignedRequest.new(self)
end

#statusesObject



166
167
168
# File 'lib/weibo2/client.rb', line 166

def statuses
  @statuses ||= Weibo2::Interface::Statuses.new(self)
end

#suggestionsObject



170
171
172
# File 'lib/weibo2/client.rb', line 170

def suggestions
  @suggestions ||= Weibo2::Interface::Suggestions.new(self)
end

#tagsObject



174
175
176
# File 'lib/weibo2/client.rb', line 174

def tags
  @tags ||= Weibo2::Interface::Tags.new(self)
end


178
179
180
# File 'lib/weibo2/client.rb', line 178

def trends
  @trends ||= Weibo2::Interface::Trends.new(self)
end

#usersObject



182
183
184
# File 'lib/weibo2/client.rb', line 182

def users
  @users ||= Weibo2::Interface::Users.new(self)
end