Class: OpenSocial::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/opensocial/connection.rb

Constant Summary collapse

ORKUT =
{ :endpoint => 'http://sandbox.orkut.com/social',
:rest => 'rest/',
:rpc => 'rpc/' }
IGOOGLE =
{ :endpoint => 'http://gmodules.com/api',
:rest => '',
:rpc => 'rpc' }
MYSPACE =
{ :endpoint => 'http://api.myspace.com/v2',
:rest => '',
:rpc => '',
:base_uri => 'http://api.myspace.com',
:request_token_path => '/request_token',
:authorize_path => '/authorize',
:access_token_path => '/access_token',
:http_method => :get }
AUTH_HMAC =
0
AUTH_ST =
1
DEFAULT_OPTIONS =
{ :container => ORKUT,
:st => '',
:consumer_key => '',
:consumer_secret => '',
:consumer_token => OAuth::Token.new('', ''),
:xoauth_requestor_id => '',
:auth => AUTH_HMAC }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Initializes the Connection using the supplied options hash, or the defaults. Verifies that the supplied authentication type has proper (ie. non-blank) credentials, and that the authentication type is known.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/opensocial/connection.rb', line 77

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  options.each do |key, value|
    self.send("#{key}=", value)
  end
  
  if @auth == AUTH_HMAC && !has_valid_hmac_double?
    raise ArgumentError.new('Connection authentication is set to ' +
                            'HMAC-SHA1, but a valid consumer_key and' +
                            'secret pair was not supplied.')
  elsif @auth == AUTH_ST && @st.empty?
    raise ArgumentError.new('Connection authentication is set to ' +
                            'security token, but a security token was ' +
                            'not supplied.')
  elsif ![AUTH_HMAC, AUTH_ST].include?(@auth)
    raise ArgumentError.new('Connection authentication is set to an ' +
                            'unknown value.')
  end
end

Instance Attribute Details

#authObject

Defines the authentication scheme: HMAC or security token.



72
73
74
# File 'lib/opensocial/connection.rb', line 72

def auth
  @auth
end

#consumer_keyObject

Defines the consumer key for OAuth.



59
60
61
# File 'lib/opensocial/connection.rb', line 59

def consumer_key
  @consumer_key
end

#consumer_secretObject

Defines the consumer secret for OAuth.



62
63
64
# File 'lib/opensocial/connection.rb', line 62

def consumer_secret
  @consumer_secret
end

#consumer_tokenObject

Defines the consumer token for OAuth.



65
66
67
# File 'lib/opensocial/connection.rb', line 65

def consumer_token
  @consumer_token
end

#containerObject

Defines the container that will be used in requests.



53
54
55
# File 'lib/opensocial/connection.rb', line 53

def container
  @container
end

#stObject

Defines the security token, for when OAuth is not in use.



56
57
58
# File 'lib/opensocial/connection.rb', line 56

def st
  @st
end

#xoauth_requestor_idObject

Defines the ID of the requestor (required by some implementations when using OAuth).



69
70
71
# File 'lib/opensocial/connection.rb', line 69

def xoauth_requestor_id
  @xoauth_requestor_id
end

Instance Method Details

#service_uri(service, guid, selector, pid, extra_fields = {}) ⇒ Object

Constructs a URI to the OpenSocial endpoint given a service, guid, selector, and pid.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/opensocial/connection.rb', line 99

def service_uri(service, guid, selector, pid, extra_fields = {})
  uri = [@container[:endpoint], service, guid, selector, pid].compact.
          join('/')
  
  if @auth == AUTH_HMAC && !xoauth_requestor_id.empty?
    uri << '?xoauth_requestor_id=' + @xoauth_requestor_id
  elsif @auth == AUTH_ST
    uri << '?st=' + self.st
  end

  extra_fields.each do |name, value|
    uri << "&#{name}=#{value}"
  end
  
  URI.parse(uri)
end

#sign!(http, req) ⇒ Object

Signs a request using OAuth.



117
118
119
120
121
122
# File 'lib/opensocial/connection.rb', line 117

def sign!(http, req)
  if @auth == AUTH_HMAC
    consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret)
    req.oauth!(http, consumer, @consumer_token, :scheme => 'query_string')
  end
end