Class: Renren2::Client

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

Overview

The Renren2::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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/renren2/client.rb', line 45

def initialize(opts={}, &block)
  id = Renren2::Config.api_key
  secret = Renren2::Config.api_secret
  @redirect_uri = Renren2::Config.redirect_uri
  
  options = {:site          => "https://graph.renren.com",
             :authorize_url => "/oauth/authorize",
             :token_url     => "/oauth/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/renren2/client.rb', line 8

def redirect_uri
  @redirect_uri
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

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

Initializes a new Client from a authentication_code

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/renren2/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



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

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

Instance Method Details

#adminObject

APIs



155
156
157
# File 'lib/renren2/client.rb', line 155

def admin
  @admin ||= Renren2::Interface::Admin.new(self)
end

#auth_codeObject

The Authorization Code strategy



140
141
142
# File 'lib/renren2/client.rb', line 140

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

#blogObject



159
160
161
# File 'lib/renren2/client.rb', line 159

def blog
  @blog ||= Renren2::Interface::Blog.new(self)
end

#checkinsObject



163
164
165
# File 'lib/renren2/client.rb', line 163

def checkins
  @checkins ||= Renren2::Interface::Checkins.new(self)
end

#feedObject



167
168
169
# File 'lib/renren2/client.rb', line 167

def feed
  @feed ||= Renren2::Interface::Feed.new(self)
end

#friendsObject



171
172
173
# File 'lib/renren2/client.rb', line 171

def friends
  @friends ||= Renren2::Interface::Friends.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



71
72
73
74
75
76
# File 'lib/renren2/client.rb', line 71

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 => "oauth_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



82
83
84
85
86
87
88
89
# File 'lib/renren2/client.rb', line 82

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.delete("expires") || hash.delete(:expires),
          :header_format => "OAuth2 %s",
          :param_name => "oauth_token"}.merge(hash)
  
  @token = OAuth2::AccessToken.new(self, access_token, opts)
end

#hash_for_saveObject



124
125
126
127
128
129
130
131
# File 'lib/renren2/client.rb', line 124

def hash_for_save
  if token
    token.params.merge({"expires_in" => token.expires_in,
                        "expires_at" => token.expires_at,
                        "refresh_token" => token.refresh_token,
                        "access_token" => token.token})
  end
end

#include_scope?(scope_name) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
# File 'lib/renren2/client.rb', line 115

def include_scope?(scope_name)
  return true
  if token && token.params["scope"]
    token.params["scope"].split.include? scope_name
  else
    false
  end
end

#invitationsObject



175
176
177
# File 'lib/renren2/client.rb', line 175

def invitations
  @invitations ||= Renren2::Interface::Invitations.new(self)
end

#is_authorized?Boolean

Whether or not the client is authorized

Returns:

  • (Boolean)


62
63
64
# File 'lib/renren2/client.rb', line 62

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

#likeObject



179
180
181
# File 'lib/renren2/client.rb', line 179

def like
  @like ||= Renren2::Interface::Like.new(self)
end

#notificationsObject



183
184
185
# File 'lib/renren2/client.rb', line 183

def notifications
  @notifications ||= Renren2::Interface::Notifications.new(self)
end

#pagesObject



187
188
189
# File 'lib/renren2/client.rb', line 187

def pages
  @pages ||= Renren2::Interface::Pages.new(self)
end

#passwordObject

The Resource Owner Password Credentials strategy



147
148
149
# File 'lib/renren2/client.rb', line 147

def password
  super
end

#payObject



191
192
193
# File 'lib/renren2/client.rb', line 191

def pay
  @pay ||= Renren2::Interface::Pay.new(self)
end

#photosObject



195
196
197
# File 'lib/renren2/client.rb', line 195

def photos
  @photos ||= Renren2::Interface::Photos.new(self)
end

#placesObject



199
200
201
# File 'lib/renren2/client.rb', line 199

def places
  @places ||= Renren2::Interface::Places.new(self)
end

#refresh!(params = {}) ⇒ AccessToken

Note:

options should be carried over to the new AccessToken

Refreshes the current Access Token

Returns:

  • (AccessToken)

    a new AccessToken



95
96
97
# File 'lib/renren2/client.rb', line 95

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

#requestsObject



203
204
205
# File 'lib/renren2/client.rb', line 203

def requests
  @requests ||= Renren2::Interface::Requests.new(self)
end

#scopeObject



111
112
113
# File 'lib/renren2/client.rb', line 111

def scope
  token.params["scope"] if token
end

#shareObject



207
208
209
# File 'lib/renren2/client.rb', line 207

def share
  @share ||= Renren2::Interface::Share.new(self)
end

#statusObject



211
212
213
# File 'lib/renren2/client.rb', line 211

def status
  @status ||= Renren2::Interface::Status.new(self)
end

#userObject

Some info



103
104
105
# File 'lib/renren2/client.rb', line 103

def user
  token.params["user"] if token
end

#user_idObject



107
108
109
# File 'lib/renren2/client.rb', line 107

def user_id
  user["id"] if user
end

#usersObject



216
217
218
# File 'lib/renren2/client.rb', line 216

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