Class: Renren2::Interface::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/renren2/interface/base.rb

Overview

The Base class of API

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/renren2/interface/base.rb', line 6

def initialize(client)
  @client = client
end

Instance Method Details

#check_scope(scope_name) ⇒ Object



10
11
12
13
14
# File 'lib/renren2/interface/base.rb', line 10

def check_scope(scope_name)
  unless @client.include_scope? scope_name
    raise "You haven't got the scope " + scope_name
  end
end

#get(path, opts = {}, &block) ⇒ Object



37
38
39
# File 'lib/renren2/interface/base.rb', line 37

def get(path, opts={}, &block)
  request(:get, path, opts, &block)
end

#post(path, opts = {}, &block) ⇒ Object



41
42
43
# File 'lib/renren2/interface/base.rb', line 41

def post(path, opts={}, &block)
  request(:post, path, opts, &block)
end

#request(verb, path, opts = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/renren2/interface/base.rb', line 16

def request(verb, path, opts={}, &block)
  unless @client.is_authorized?
    raise "I can't find a valid access_token. Forgot to get it or expired?"
  end
  
  # Note that it is a hack here. Renren hasn't started using http://graph.renren.com as its api server, so
  # we still use http://api.renren.com/restserver.do as a temporary solution. And the :path param will act 
  # as the :method params in querystring.
  params = {:access_token => @client.token.token,
            :method       => path,
            :v            => "1.0",
            :format       => "JSON"}.merge(opts.delete(:body) || {})
                                    .merge(opts.delete(:params) || {})
  opts.merge!(:body => params.merge(:sig => compute_sig(params)), :parse => :json)
  
  # use the old server
  path = "http://api.renren.com/restserver.do"
  
  @client.token.request(verb, path, opts, &block)
end