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/',
:content_type => 'application/json',
:post_body_signing => false,
:use_request_body_hash => true }
IGOOGLE =
{ :endpoint => 'http://www-opensocial-sandbox.googleusercontent.com/api',
:rest => '',
:rpc => 'rpc',
:content_type => 'application/json',
:post_body_signing => false,
:use_request_body_hash => true }
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,
:content_type => 'application/x-www-form-urlencoded',
:post_body_signing => true,
:use_request_body_hash => false }
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.



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

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.



81
82
83
# File 'lib/opensocial/connection.rb', line 81

def auth
  @auth
end

#consumer_keyObject

Defines the consumer key for OAuth.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject

Defines the consumer secret for OAuth.



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

def consumer_secret
  @consumer_secret
end

#consumer_tokenObject

Defines the consumer token for OAuth.



74
75
76
# File 'lib/opensocial/connection.rb', line 74

def consumer_token
  @consumer_token
end

#containerObject

Defines the container that will be used in requests.



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

def container
  @container
end

#content_typeObject

Defines the content-type when sending a request body



84
85
86
# File 'lib/opensocial/connection.rb', line 84

def content_type
  @content_type
end

#post_body_signingObject

Defines whether or not to sign the request body (treating the body as a large query parameter for the purposes of the signature base string)



88
89
90
# File 'lib/opensocial/connection.rb', line 88

def post_body_signing
  @post_body_signing
end

#stObject

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



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

def st
  @st
end

#use_request_body_hashObject

Defines whether or not to sign the body using a request body hash



91
92
93
# File 'lib/opensocial/connection.rb', line 91

def use_request_body_hash
  @use_request_body_hash
end

#xoauth_requestor_idObject

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



78
79
80
# File 'lib/opensocial/connection.rb', line 78

def xoauth_requestor_id
  @xoauth_requestor_id
end

Instance Method Details

#service_uri(service, guid, selector, pid) ⇒ Object

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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/opensocial/connection.rb', line 118

def service_uri(service, guid, selector, pid)
  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
  URI.parse(uri)
end

#sign!(http, req) ⇒ Object

Signs a request using OAuth.



131
132
133
134
135
136
# File 'lib/opensocial/connection.rb', line 131

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