Module: InfomemeClient::Base

Included in:
InfomemeClient
Defined in:
lib/infomeme_client/base.rb

Instance Method Summary collapse

Instance Method Details

#authorize_url(token, permissions = []) ⇒ Object



17
18
19
# File 'lib/infomeme_client/base.rb', line 17

def authorize_url(token, permissions = [])
"https://infomeme.com/authorize?token=#{token}&permissions=#{permissions.join(',')}"
end

#authorize_with_credentials(login, password, permissions = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/infomeme_client/base.rb', line 38

def authorize_with_credentials(, password, permissions = nil)
  req_token = request_authorization
  response = put(consumer.authorize_url, {:oauth_token => req_token.token, :login => , :password => password, :permissions => permissions || all_permissions.collect {|perm| perm.id}})
  return false if response.error?
  authorize_with_verifier(req_token.token, req_token.secret, response.verifier)
end

#authorize_with_token(token, secret) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/infomeme_client/base.rb', line 28

def authorize_with_token(token, secret)
  access_token = OAuth::AccessToken.new(consumer, token, secret)
  response = token_request(access_token, :get, "/oauth/verify_access")
  return false if response.error?
  self.verified_user = response.user_id
  self.token = access_token
  refresh_user_data
  self
end

#authorize_with_verifier(token, secret, verifier) ⇒ Object



21
22
23
24
25
26
# File 'lib/infomeme_client/base.rb', line 21

def authorize_with_verifier(token, secret, verifier)
  req_token = OAuth::RequestToken.new(consumer, token, secret)
  response = token_request(req_token, :get, finalize_get_url(consumer.access_token_url, {:oauth_verifier => verifier})) #do request with request_token
  return false if response.error?
  authorize_with_token(response.access_token.token, response.access_token.secret)
end

#authorized?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/infomeme_client/base.rb', line 52

def authorized?
  ! verified_user.nil?
end

#credentials(just = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/infomeme_client/base.rb', line 56

def credentials(just = nil)
  creds = {
      :login => verified_user,
      :token => token.token,
      :secret => token.secret,
  }
  creds.key?(just) ? creds[just] : creds
end

#deauthorizeObject



45
46
47
48
49
50
# File 'lib/infomeme_client/base.rb', line 45

def deauthorize
  self.verified_user = nil
  self.token = OAuth::AccessToken.new(consumer)
  refresh_user_data
  self
end

#initialize(options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/infomeme_client/base.rb', line 2

def initialize(options = {})
  self.consumer = OAuth::Consumer.new(options[:api_key] || ::INFOMEME_API_KEY, options[:api_secret] || ::INFOMEME_API_SECRET, :site => options[:site] || ::INFOMEME_API_SITE)
  self.default_language = options[:language] || "en"
  case
   when options.key?(:token) && options.key?(:secret) then authorize_with_token(options[:token], options[:secret])
   when options.key?(:login) && options.key?(:password) then authorize_with_credentials(options[:login], options[:password])
   else deauthorize
  end
end

#inspectObject



65
66
67
68
# File 'lib/infomeme_client/base.rb', line 65

def inspect
  @all_permissions ||= all_permissions
  "#<#{self.class.name}: #{authorized? ? credentials.merge({:permissions => (@all_permissions || []).select {|perm| has_permission?(perm.id)}.collect {|perm| perm.name}}).inspect : 'not authorized'}>"
end

#request_authorizationObject



12
13
14
15
# File 'lib/infomeme_client/base.rb', line 12

def request_authorization
  response = handle_response :get, consumer.request_token_url
  response.request_token
end