giggly 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,150 @@
1
+ module Giggly
2
+ module Rest
3
+ class Socialize
4
+
5
+ GIGYA_URL = "http://socialize.api.gigya.com/socialize."
6
+
7
+ attr_accessor :request
8
+
9
+ # Create a new socialize object by passing in a <tt>Giggly::Rest::Socialize</tt> object
10
+ # or by passing in a hash that will be used to initialize a new <tt>Giggly::Rest::Socialize</tt>.
11
+ # See the rdoc for Request to find details for a new connection hash.
12
+ # A <tt>Giggly::Rest::Socialize</tt> is used per user (because the Request object is).
13
+ # example:
14
+ # @socialize = Giggly::Rest::Socialize.new(@connection)
15
+ def initialize(request)
16
+ @request = request and return if request.kind_of?(Giggly::Rest::Request)
17
+ @request = Giggly::Rest::Request.new(request)
18
+ end
19
+
20
+ # Disconnects the user from the provided network.
21
+ # Params::
22
+ # * +network+ array of networks to disconnect from, if emtpy user will be disconnected from all networks
23
+ # Allowed values for network are facebook, myspace, twitter and yahoo.
24
+ def disconnect(provider = nil)
25
+ validate_providers! %w[facebook yahoo myspace twitter], provider
26
+ perform_post :disconnect, :provider => (provider.join(',') if provider)
27
+ end
28
+
29
+ # retrieves the friends of the current user
30
+ # Params::
31
+ # * +enabled_providers+ comma +separated+ list of providers to include. (ONLY)
32
+ # * +disabled_providers+ comma separated list of providers to exclude. (NOT)
33
+ # * +uids+ list of users to retrieve.
34
+ # * +detail_level+ 'basic' or 'extended'
35
+ # Returns::
36
+ # * +Array+ of +Giggly::User+ objects
37
+ def friends_info(options = {})
38
+ validate_providers! %w[facebook yahoo myspace twitter], options[:enabled_providers]
39
+ params = provider_hash(options)
40
+ params[:UIDs], params[:detailLevel] = options[:uids], options[:detailLevel]
41
+
42
+ response = perform_post(:getFriendsInfo, params)
43
+ friends = []
44
+ response['friends']['friend'].each do |friend|
45
+ friends << Giggly::Friend.new(friend)
46
+ end
47
+ friends
48
+ end
49
+
50
+ # get raw data from an individual provider about a user
51
+ # Params::
52
+ # * +provider+ the provider to retrieve the raw data from, only facebook and myspace are currently supported
53
+ # * +fields+ a array of provider specific fields to retrieve
54
+ # Returns::
55
+ # * +Hash+ of the raw data
56
+ def raw_data(provider, fields)
57
+ validate_providers! %w[facebook myspace], provider
58
+ perform_post :getRawData, {:provider => provider, :fields => fields.join(',')}
59
+ end
60
+
61
+ # get the connection info for a session with a direct api provider
62
+ # this is useful to make calls to a specific provider (not via Socialize)
63
+ # for functions that are currently unsupported by Gigya Socialize
64
+ # Params::
65
+ # * +provider+ the provider to get the connection information for,
66
+ # * possible values are facebook, myspace, twitter, or yahoo
67
+ # * +padding_mode+ padding mode for the AES algorithm used in encryping some of the response parameters
68
+ # * values are PKCS5, PKCS7 or ZEROS PKCS7 will be used as the default
69
+ # See http://wiki.gigya.com/030_Gigya_Socialize_API_2.0/030_API_reference/REST_API/socialize.getSessionInfo on decrypting
70
+ def session_info(provider, padding_mode = 'PKCS7')
71
+ validate_providers! %w[facebook yahoo myspace twitter], provider
72
+ # TODO: possibly decrypt response
73
+ Giggly::SessionInfo.new perform_post(:getSessionInfo, {:provider => provider, :paddingMode => padding_mode})
74
+ end
75
+
76
+ # retrieves user information from gigya, including or excluding providers
77
+ # as indicated. default usage includes all providers. returns user.
78
+ # Params::
79
+ # * +providers+ an optional hash of arrays the has the keys of
80
+ # * +enabled_providers+ an array of provider strings
81
+ # * +disabled_providers+ an array of provider strings
82
+ def user_info(providers = {})
83
+ Giggly::User.new perform_post(:getUserInfo, provider_hash(providers))
84
+ end
85
+
86
+ # see: http://wiki.gigya.com/030_Gigya_Socialize_API_2.0/030_API_reference/REST_API/socialize.publishUserAction
87
+ # for how to create user_action_xml
88
+ def publish_user_action(user_action_xml, providers = {})
89
+ validate_providers! %w[facebook yahoo], providers[:enabled_providers]
90
+ perform_post :publishUserAction, {:userAction => user_action_xml}.merge(provider_hash(providers))
91
+ end
92
+
93
+ # Sends a notification to a list of friends
94
+ # Params::
95
+ # * +recipients+ a string or array of uids to send the notification to
96
+ # * +subject+ the subject line of the notification
97
+ # * +body+ the body of the notification, do not use html, Gigya will autolink urls
98
+ # this will post to both facebook and twitter
99
+ def send_notification(recipients, subject, body)
100
+ recipients = recipients.is_a?(Array) ? recipients.join(',') : recipients
101
+ perform_post :sendNotification, :recipients => recipients, :subject => subject, :body => body
102
+ end
103
+
104
+ # sets the status of a user for the given providers (or all of them if blank)
105
+ # Params::
106
+ # * +status+ the status to set for the user
107
+ # * +providers+ an optional hash of arrays the has the keys of
108
+ # * +enabled_providers+ an array of provider strings
109
+ # * +disabled_providers+ an array of provider strings
110
+ def set_status(status, providers = {})
111
+ validate_providers! %w[facebook yahoo myspace twitter], providers[:enabled_providers]
112
+ perform_post :setStatus, {:status => status}.merge(provider_hash(providers))
113
+ end
114
+
115
+ # sets the status of a user for all providers
116
+ # Params::
117
+ # * +status+ the status to set for the user
118
+ # This method will return the status if it is successful, and raise an error on failure
119
+ # Use set status if you want to return the response object
120
+ # Its just in here because we think it looks cool and its convenient
121
+ def status=(status)
122
+ set_status status
123
+ end
124
+
125
+ protected
126
+
127
+ def perform_post(action, params = {})
128
+ params.reject! {|k,v| v.nil?}
129
+ @request.post "#{GIGYA_URL}#{action}", params
130
+ end
131
+
132
+ def provider_hash(providers)
133
+ { :enabledProviders => (providers[:enabled_providers].join(',') if providers[:enabled_providers]),
134
+ :disabledProviders => (providers[:disabled_providers].join(',') if providers[:disabled_providers]) }
135
+ end
136
+
137
+ def validate_providers!(valid_providers, given_providers = nil)
138
+ return if given_providers.nil?
139
+ given_providers.each do |provider|
140
+ raise Giggly::Rest::Socialize::InvalidProvider.new(
141
+ "#{provider} is not a valid provider, please only use #{valid_providers.join(' ')}"
142
+ ) unless valid_providers.include? provider
143
+ end
144
+ end
145
+
146
+ class InvalidProvider < StandardError; end
147
+
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,11 @@
1
+ module Giggly
2
+ class SessionInfo
3
+
4
+ attr_reader :session
5
+
6
+ def initialize(session)
7
+ @session = session
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Giggly
2
+ class User
3
+
4
+ attr_reader :data
5
+
6
+ def initialize(data)
7
+ @data = data
8
+ end
9
+
10
+ def user_id
11
+ @data["UID"]
12
+ end
13
+
14
+ def [](value)
15
+ @data[value.to_s]
16
+ end
17
+
18
+ def method_missing(method, *args)
19
+ super unless @data[method.to_s]
20
+ @data[method.to_s]
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+ require 'yaml'
3
+
4
+ # sets up a socialize faker.
5
+ #
6
+ # example::
7
+ # <tt>
8
+ # FakeSocialize.setup_response("disconnect", :success)
9
+ # </tt>
10
+ class FakeSocialize
11
+
12
+ @@responses ||= {}
13
+
14
+ def self.setup_response(method_ref, response_name)
15
+ reload_responses if @@responses.nil? || @@responses.empty?
16
+ response = @@responses[method_ref]
17
+ FakeWeb.register_uri(
18
+ response[:request_type],
19
+ Regexp.new(Giggly::Rest::Socialize::GIGYA_URL + response[:method_name]),
20
+ :body => response[response_name]
21
+ )
22
+ return response.merge({:response_key => "socialize.#{response[:method_name]}Response"})
23
+ end
24
+
25
+ def self.reload_responses
26
+ responses = {}
27
+ Dir.new(File.join(File.dirname(__FILE__), 'responses')).each do |filename|
28
+ file_path = File.join(File.dirname(__FILE__), 'responses', filename)
29
+ next unless File.file?(file_path)
30
+ responses.merge!(YAML.load(File.read(file_path))) if File.file?(file_path)
31
+ end
32
+ @@responses = responses
33
+ end
34
+
35
+
36
+ end
37
+
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class Giggly::FriendTest < Test::Unit::TestCase
4
+
5
+ context "A Giggly::Friend instance" do
6
+
7
+ setup do
8
+ setup_giggly_rest
9
+ FakeSocialize.setup_response(:friends_info, :success)
10
+ @friends = @giggly.friends_info
11
+ @friend = @friends.first
12
+ end
13
+
14
+ # this is overtesting
15
+ should "inherit from user" do
16
+ @friend.kind_of? Giggly::Friend
17
+ @friend.kind_of? Giggly::User
18
+ @friend.class.superclass.should == Giggly::User
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ # TODO
4
+
5
+ #class Giggly::Javascript::Helper < Test::Unit::TestCase
6
+
7
+ #end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class Giggly::Javascript::SocializeTest < Test::Unit::TestCase
4
+
5
+ context "A Giggly::Javascript::Socialize instance" do
6
+
7
+ setup do
8
+ @giggly = Giggly::Javascript::Socialize.new(:api_key => 'API_KEY', :enabled_providers => %w[facebook yahoo twitter])
9
+ end
10
+
11
+ should "should have a config hash with no nil values" do
12
+ @giggly.to_config.should == {'APIKey' => 'API_KEY', 'enabledProviders' => 'facebook,yahoo,twitter'}
13
+ end
14
+
15
+ should "return the config as a string for javascript" do
16
+ @giggly.config_to_js.class.should == String
17
+ @giggly.config_to_js.should =~ /APIKey/
18
+ @giggly.config_to_js.should =~ /enabledProviders/
19
+ end
20
+
21
+ should "return a script tag with the path to the giggly socialize js api file" do
22
+ @giggly.include_gigya_socialize =~ /giggly-socialize-min/
23
+ end
24
+
25
+ should "allow a custom path to the giggly socialize api file" do
26
+ @giggly.include_gigya_socialize('/somewhere') =~ /somewhere/
27
+ end
28
+
29
+ should "require the non minified giggly socialize api file if min is false" do
30
+ @giggly.include_gigya_socialize('/javascripts', false).should_not =~ /min\.js/
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class Giggly::Rest::RequestTest < Test::Unit::TestCase
4
+
5
+ context "A Giggly::Rest::Request instance" do
6
+ setup do
7
+ @request = Giggly::Rest::Request.new(
8
+ :api_key => 'TEST_KEY',
9
+ :secret_key => 'SECRET_KEY',
10
+ :user_id => 'GIGYA_USER_ID'
11
+ )
12
+ end
13
+
14
+ # this is a sanity check for the signature method
15
+ # the signature is the test was generated using the signature method which we know
16
+ # currently works with gigya, see http://twitter.com/_ah/status/4567114726
17
+ # which was posted from irb using Giggly
18
+ should "create a valid signature" do
19
+ Time.stubs(:now).returns(1254940826)
20
+ params = @request.sign('POST', "#{Giggly::Rest::Socialize::GIGYA_URL}getUserInfo", {})
21
+ 'olilJCSJzaBmzb9tsHH8LmJpYp0='.should == params['sig']
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,90 @@
1
+ require 'test_helper'
2
+
3
+ class Giggly::Rest::SocializeTest < Test::Unit::TestCase
4
+
5
+ context "A Giggly::Rest::Socialize Instance" do
6
+ setup do
7
+ setup_giggly_rest
8
+ end
9
+
10
+ should "disconnect the user" do
11
+ FakeSocialize.setup_response(:disconnect, :success)
12
+ response = @giggly.disconnect
13
+ response.should be_successful
14
+ end
15
+
16
+ should "get friends info and return array of Giggly::Friend objects" do
17
+ FakeSocialize.setup_response(:friends_info, :success)
18
+ friends = @giggly.friends_info
19
+ assert friends.size > 0
20
+ friends.each do |friend|
21
+ friend.class.should == Giggly::Friend
22
+ end
23
+ end
24
+
25
+ should "get raw data and return it as a string" do
26
+ FakeSocialize.setup_response(:raw_data, :success)
27
+ response = @giggly.raw_data('facebook', %w[birthday name])
28
+ response.should be_successful
29
+ response['data'].should == '[{"birthday":"May 31, 1972","name":"Bobo"}]'
30
+ end
31
+
32
+ should "get the session info for the current gigya connection" do
33
+ FakeSocialize.setup_response(:session_info, :success)
34
+ session_info = @giggly.session_info('facebook')
35
+ session_info.class.should == Giggly::SessionInfo
36
+ session_info.session.should_not be_nil
37
+ end
38
+
39
+ should "get the user's info as a user object" do
40
+ FakeSocialize.setup_response(:user_info, :success)
41
+ user = @giggly.user_info
42
+ user.class.should == Giggly::User
43
+ end
44
+
45
+ should "published the user action" do
46
+ FakeSocialize.setup_response(:publish_user_action, :success)
47
+ response = @giggly.publish_user_action 'this would be valid xml in real life ^_^'
48
+ response.should be_successful
49
+ end
50
+
51
+ should "set the users status via set_status" do
52
+ FakeSocialize.setup_response(:status=, :success)
53
+ response = @giggly.set_status 'the giggly gem is so awesome!', :enabled_providers => %w[facebook twitter]
54
+ response.should be_successful
55
+ end
56
+
57
+ should "set the users status via status=" do
58
+ FakeSocialize.setup_response(:status=, :success)
59
+ status = 'the giggly gem is so awesome!'
60
+ response = @giggly.status = status
61
+ response.class.should == String
62
+ response.should == status
63
+ end
64
+
65
+ should "raise an exception on failing to set the user's status in status=" do
66
+ FakeSocialize.setup_response(:status=, :failure)
67
+ lambda {@giggly.status = 'the giggly gem is so awesome!'}.should raise_error(Giggly::Rest::Unauthorized)
68
+ end
69
+
70
+ should "raise an exception if an invalid provider is given" do
71
+ FakeSocialize.setup_response(:status=, :success) # just incase of failure
72
+ fail = lambda {@giggly.set_status 'Fail! #ftw', :enabled_providers => %w[twitter google]}
73
+ fail.should raise_error(Giggly::Rest::Socialize::InvalidProvider)
74
+ end
75
+
76
+ should "throw an exception on failure" do
77
+ FakeSocialize.setup_response(:disconnect, :failure)
78
+ lambda {@giggly.disconnect}.should raise_error(Giggly::Rest::Unauthorized)
79
+ end
80
+
81
+ should "send notification to users friends" do
82
+ FakeSocialize.setup_response(:send_notification, :success)
83
+ #response = @giggly.send_notification %w[_gigya_uid1 _gigya_uid2], 'this is a test', 'this is only a test'
84
+ response = @giggly.send_notification 'foo', 'bar', 'baz'
85
+ response.should be_successful
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class Giggly::UserTest < Test::Unit::TestCase
4
+
5
+ context "A Giggly::User instance" do
6
+
7
+ setup do
8
+ setup_giggly_rest
9
+ FakeSocialize.setup_response(:user_info, :success)
10
+ @user = @giggly.user_info
11
+ end
12
+
13
+ should "have data" do
14
+ @user.data.should_not be_nil
15
+ end
16
+
17
+ should "have a user_id" do
18
+ @user.user_id.should_not be_nil
19
+ @user.user_id.should == '000000'
20
+ end
21
+
22
+ should "allow access to top level items in the data hash via method calls" do
23
+ @user.should_not respond_to(:nickname)
24
+ @user.nickname.should == 'Bobo'
25
+ @user.data['nickname'].should == 'Bobo'
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,19 @@
1
+ :disconnect:
2
+ :method_name: "disconnect"
3
+ :request_type: :post
4
+ :success: >
5
+ <?xml version="1.0" encoding="utf-8" ?>
6
+ <socialize.disconnectResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7
+ xsi:schemaLocation="urn:com:gigya:api http://socialize.api.gigya.com/schema/1.0.xsd" xmlns="urn:com:gigya:api">
8
+ <statusCode>200</statusCode>
9
+ <statusReason>OK</statusReason>
10
+ </socialize.disconnectResponse>
11
+ :failure: >
12
+ <?xml version="1.0" encoding="utf-8" ?>
13
+ <socialize.disconnectResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14
+ xsi:schemaLocation="urn:com:gigya:api http://socialize.api.gigya.com/schema/1.0.xsd" xmlns="urn:com:gigya:api">
15
+ <statusCode>401</statusCode>
16
+ <statusReason>OK</statusReason>
17
+ <errorCode>123456</errorCode>
18
+ <description>FAIL</description>
19
+ </socialize.disconnectResponse>