Class: OpenSocial::Connection

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

Overview

Describes a connection to an OpenSocial container, including the ability to declare an authorization mechanism and appropriate credentials.

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/opensocial/connection.rb', line 73

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.



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

def auth
  @auth
end

#consumer_keyObject

Defines the consumer key for OAuth.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject

Defines the consumer secret for OAuth.



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

def consumer_secret
  @consumer_secret
end

#consumer_tokenObject

Defines the consumer token for OAuth.



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

def consumer_token
  @consumer_token
end

#containerObject

Defines the container that will be used in requests.



49
50
51
# File 'lib/opensocial/connection.rb', line 49

def container
  @container
end

#stObject

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



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

def st
  @st
end

#xoauth_requestor_idObject

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



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

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.



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

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.



113
114
115
116
117
118
# File 'lib/opensocial/connection.rb', line 113

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