rgigya 1.0.2 → 1.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.
data/rgigya.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rgigya"
8
- s.version = "1.0.2"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Sampson"]
12
- s.date = "2013-04-17"
12
+ s.date = "2013-05-17"
13
13
  s.description = "This is a ruby SDK for the Gigya api."
14
14
  s.email = "scott@cloudspace.com"
15
15
  s.extra_rdoc_files = [
@@ -25,9 +25,14 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "lib/rgigya.rb",
28
+ "lib/rgigya/base.rb",
29
+ "lib/rgigya/hash.rb",
30
+ "lib/rgigya/sig_utils.rb",
28
31
  "rgigya.gemspec",
29
32
  "spec/helpers.rb",
30
- "spec/rgigya_spec.rb",
33
+ "spec/rgigya_secure_spec.rb",
34
+ "spec/rgigya_shared_examples.rb",
35
+ "spec/rgigya_signature_spec.rb",
31
36
  "spec/spec_helper.rb",
32
37
  "test/dummy/Gemfile",
33
38
  "test/dummy/Gemfile.lock",
data/spec/helpers.rb CHANGED
@@ -5,4 +5,30 @@ module Helpers
5
5
  })
6
6
  return response['UID']
7
7
  end
8
+
9
+ def sample_frends_json_data
10
+ return JSON('{
11
+ "friends": [{
12
+ "UID": "00",
13
+ "isSiteUser": false,
14
+ "isSiteUID": false,
15
+ "identities": [{
16
+ "provider": "facebook",
17
+ "providerUID": "00000",
18
+ "isLoginIdentity": false,
19
+ "nickname": "Baba",
20
+ "photoURL": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/00.jpg",
21
+ "thumbnailURL": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/00.jpg"}],
22
+ "nickname": "Baba",
23
+ "photoURL": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/00.jpg",
24
+ "thumbnailURL": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/00.jpg",
25
+ "signatureTimestamp": "11111111111",
26
+ "friendshipSignature": "kfwFFiXqP+NySU79E+CZY0Pu1Mc="
27
+ }],
28
+ "statusCode": 200,
29
+ "errorCode": 0,
30
+ "statusReason": "OK",
31
+ "callId": "e2b7c39de36541b4940087d66d4c1d77"
32
+ }')
33
+ end
8
34
  end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/rgigya_shared_examples.rb')
3
+
4
+ describe "RGigyaSecure" do
5
+ before :each do
6
+ RGigya.config({
7
+ :api_key => GIGYA_API_KEY,
8
+ :api_secret => GIGYA_API_SECRET,
9
+ :use_ssl => true,
10
+ :domain => "us1"
11
+ })
12
+ @protocol = "https"
13
+ @method = "GET"
14
+ end
15
+
16
+
17
+ it_behaves_like RGigya
18
+ end
@@ -0,0 +1,315 @@
1
+ shared_examples_for RGigya do
2
+ it "should use the socialize url when making a socialize api call" do
3
+ RGigya.stub(:required_parameters) {
4
+ ''
5
+ }
6
+ # we pass in the "_" replaced with "." already
7
+ url = RGigya.build_url("socialize.getUserInfo","#{@method}",{})
8
+ url.should match(/#{@protocol}:\/\/socialize.us1.gigya.com/)
9
+ end
10
+
11
+
12
+
13
+ it "should use the gm url when making a game mechanic api call" do
14
+ RGigya.stub(:required_parameters) {
15
+ ''
16
+ }
17
+ # we pass in the "_" replaced with "." already
18
+ url = RGigya.build_url("gm.notifyAction","#{@method}",{})
19
+ url.should match(/#{@protocol}:\/\/gm.us1.gigya.com/)
20
+ end
21
+
22
+
23
+ it "should use the comments url when making a comment api call" do
24
+ RGigya.stub(:required_parameters) {
25
+ ''
26
+ }
27
+ # we pass in the "_" replaced with "." already
28
+ url = RGigya.build_url("comments.getTopStreams","#{@method}",{})
29
+ url.should match(/#{@protocol}:\/\/comments.us1.gigya.com/)
30
+ end
31
+
32
+ it "should use the accounts url when making an accounts api call" do
33
+ RGigya.stub(:required_parameters) {
34
+ ''
35
+ }
36
+ # we pass in the "_" replaced with "." already
37
+ url = RGigya.build_url("accounts.getPolicies","#{@method}",{})
38
+ url.should match(/#{@protocol}:\/\/accounts.us1.gigya.com/)
39
+ end
40
+
41
+ it "should use the reports url when making a reports api call" do
42
+ RGigya.stub(:required_parameters) {
43
+ ''
44
+ }
45
+ # we pass in the "_" replaced with "." already
46
+ url = RGigya.build_url("reports.getChatStats","#{@method}",{})
47
+ url.should match(/#{@protocol}:\/\/reports.us1.gigya.com/)
48
+ end
49
+
50
+
51
+ it "should use the chats url when making a chats api call" do
52
+ RGigya.stub(:required_parameters) {
53
+ ''
54
+ }
55
+ # we pass in the "_" replaced with "." already
56
+ url = RGigya.build_url("chat.getMessages","#{@method}",{})
57
+ url.should match(/#{@protocol}:\/\/chat.us1.gigya.com/)
58
+ end
59
+
60
+ it "should use the ds url when making a data store api call" do
61
+ RGigya.stub(:required_parameters) {
62
+ ''
63
+ }
64
+ # we pass in the "_" replaced with "." already
65
+ url = RGigya.build_url("ds.get","#{@method}",{})
66
+ url.should match(/#{@protocol}:\/\/ds.us1.gigya.com/)
67
+ end
68
+
69
+
70
+
71
+ it "should raise a bad param error if UID is nil" do
72
+ expect {
73
+ RGigya.build_url('socialize.getUserInfo', "#{@method}",{:uid => nil})
74
+ }.to raise_error(RGigya::UIDParamIsNil)
75
+ end
76
+
77
+ it "should raise a bad param error if siteUID is nil" do
78
+ expect {
79
+ RGigya.build_url('socialize.getUserInfo', "#{@method}",{:siteUID => nil})
80
+ }.to raise_error(RGigya::SiteUIDParamIsNil)
81
+ end
82
+
83
+ it "should fill in the required parameters on request" do
84
+ # we pass in the "_" replaced with "." already
85
+ params = RGigya.required_parameters
86
+ params.should match(/apiKey=#{Regexp.escape(CGI.escape(GIGYA_API_KEY))}/)
87
+ params.should match(/secret=#{Regexp.escape(CGI.escape(GIGYA_API_SECRET))}/)
88
+ params.should match(/format=json/)
89
+ end
90
+
91
+ it "should succeed with a result code of 0 from gigya" do
92
+ params = RGigya.check_for_errors({'errorCode' => 0})
93
+ params.should have_key('errorCode')
94
+ end
95
+
96
+
97
+ it "should return nil and not raise an error with a limit reached error" do
98
+ results = RGigya.check_for_errors({'errorCode' => 400124})
99
+ results.should be_nil
100
+ end
101
+
102
+ it "should raise an error when we pass in a bad parameter" do
103
+ expect {
104
+ RGigya.check_for_errors({
105
+ 'errorCode' => 400002
106
+ })
107
+ }.to raise_error(RGigya::BadParamsOrMethodName)
108
+ end
109
+
110
+ it "should raise an error when an errorcode other than 0,400002, or 400124 is returned" do
111
+ # Buffering the log
112
+ buffer = StringIO.new
113
+ $stdout = buffer
114
+ expect {
115
+ RGigya.check_for_errors({
116
+ 'errorCode' => 4034934
117
+ })
118
+ }.to raise_error(RGigya::ErrorCodeReturned)
119
+ $stdout = STDOUT
120
+ end
121
+
122
+ it "should report method missing if method does not start with socialize" do
123
+ expect {
124
+ RGigya.method_missing(:abc,{})
125
+ }.to raise_error(NameError)
126
+ end
127
+
128
+
129
+ it "should report method missing if method does not start with gm" do
130
+ expect {
131
+ RGigya.method_missing(:abc,{})
132
+ }.to raise_error(NameError)
133
+ end
134
+
135
+
136
+ it "should report method missing if method does not start with socialize,gm, accounts,reports, chat,ds or comments" do
137
+ expect {
138
+ RGigya.method_missing(:abc,{})
139
+ }.to raise_error(NameError)
140
+ end
141
+
142
+ it "should not respond to method starting without socialize,gm, accounts,reports, chat,ds or comments" do
143
+ RGigya.respond_to?(:abc,false).should be_false
144
+ end
145
+
146
+ it "should respond to method starting with socialize" do
147
+ RGigya.respond_to?(:socialize_getUserInfo,false).should be_true
148
+ end
149
+
150
+
151
+ it "should respond to method starting with gm" do
152
+ RGigya.respond_to?(:gm_notifyAction,false).should be_true
153
+ end
154
+
155
+ it "should respond_to method starting with comments" do
156
+ RGigya.respond_to?(:comments_getComments,false).should be_true
157
+ end
158
+
159
+ it "should respond_to method starting with accounts" do
160
+ RGigya.respond_to?(:accounts_getPolicies,false).should be_true
161
+ end
162
+
163
+ it "should respond_to method starting with reports" do
164
+ RGigya.respond_to?(:reports_getChatStats,false).should be_true
165
+ end
166
+
167
+ it "should respond_to method starting with chat" do
168
+ RGigya.respond_to?(:chat_getMessages,false).should be_true
169
+ end
170
+
171
+ it "should respond_to method starting with ds" do
172
+ RGigya.respond_to?(:ds_get,false).should be_true
173
+ end
174
+
175
+
176
+ it "should print log to standard out" do
177
+ str = "This should print to the screen"
178
+ buffer = StringIO.new
179
+ $stdout = buffer
180
+ RGigya.log(str)
181
+ $stdout = STDOUT
182
+ buffer.rewind
183
+ buffer.read.should == str + "\n"
184
+ end
185
+
186
+
187
+ it "should print log to standard out" do
188
+ # buffer the input and check
189
+ str = "This should print to the screen"
190
+ buffer = StringIO.new
191
+ $stdout = buffer
192
+ RGigya.log(str)
193
+ $stdout = STDOUT
194
+ buffer.rewind
195
+ buffer.read.should == str + "\n"
196
+ end
197
+
198
+
199
+ it "should log to the Rails log if rails exists" do
200
+ # Mock and Stub Rails.logger.info
201
+ Rails = mock("Rails")
202
+ Rails.stub(:logger) {
203
+ logger = mock("Logger");
204
+ logger.stub(:info) do |str|
205
+ puts "mocked_data=#{str}"
206
+ end
207
+ logger
208
+ }
209
+
210
+ # buffer the input and check
211
+ str = "This should print to the screen"
212
+ buffer = StringIO.new
213
+ $stdout = buffer
214
+ RGigya.log(str)
215
+ $stdout = STDOUT
216
+ buffer.rewind
217
+ buffer.read.should == "mocked_data=" + str + "\n"
218
+ # Remove the rails object so it doesn't interfere with tests below
219
+ Object.send(:remove_const, :Rails)
220
+ end
221
+
222
+ it "should return a result of false if we pass a bad method" do
223
+ HTTParty.stub(:get) do |url,options|
224
+ nil
225
+ end
226
+
227
+ RGigya.parse_results("socialize_notAMethod",{}).should be_false
228
+ end
229
+
230
+ it "should raise json error if gigya returns bad json" do
231
+ HTTParty.stub(:get) do |url,options|
232
+ Response = mock("Response")
233
+ Response.stub(:body) {
234
+ '{'
235
+ }
236
+ Response
237
+ end
238
+ HTTParty.stub(:post) do |url,options|
239
+ Response = mock("Response")
240
+ Response.stub(:body) {
241
+ '{'
242
+ }
243
+ Response
244
+ end
245
+
246
+
247
+ expect {
248
+ RGigya.parse_results("socialize_notAMethod",{}).should be_false
249
+ }.to raise_error(RGigya::JSONParseError)
250
+ end
251
+
252
+
253
+ it "should raise a response error if the gigya call fails" do
254
+ HTTParty.stub(:get) do |url,options|
255
+ raise SocketError
256
+ end
257
+ HTTParty.stub(:post) do |url,options|
258
+ raise SocketError
259
+ end
260
+
261
+ expect {
262
+ RGigya.parse_results("socialize_notAMethod",{}).should be_false
263
+ }.to raise_error(RGigya::ResponseError)
264
+ end
265
+
266
+ it "should raise a response error if the gigya call times out" do
267
+ HTTParty.stub(:get) do |url,options|
268
+ raise Timeout::Error
269
+ end
270
+ HTTParty.stub(:post) do |url,options|
271
+ raise Timeout::Error
272
+ end
273
+
274
+ expect {
275
+ RGigya.parse_results("socialize_notAMethod",{}).should be_false
276
+ }.to raise_error(RGigya::ResponseError)
277
+
278
+ end
279
+
280
+
281
+ # Actual tests that modify the gigya account below
282
+
283
+ it "should login a user" do
284
+
285
+ userInfo = {
286
+ 'nickname' => 'Gigems',
287
+ 'email' => 'ralph@cloudspace.com',
288
+ 'firstName' => 'Ralph',
289
+ 'lastName' => 'Masterson'
290
+ }
291
+
292
+ response = RGigya.socialize_notifyLogin({
293
+ :siteUID => '1',
294
+ :userInfo => userInfo.to_json
295
+ })
296
+ end
297
+
298
+ it "should register a user" do
299
+ uid = get_uid
300
+ RGigya.socialize_notifyRegistration({
301
+ :UID => uid,
302
+ :siteUID => '1',
303
+ })
304
+
305
+ end
306
+
307
+ it "should get the users info", :user_info => true do
308
+ uid = get_uid
309
+ response = RGigya.socialize_getUserInfo({
310
+ 'UID' => uid,
311
+ })
312
+ response['nickname'].should match 'Gigems'
313
+ end
314
+
315
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/rgigya_shared_examples.rb')
3
+
4
+ describe "RGigyaSignature" do
5
+ before :each do
6
+ RGigya.config({
7
+ :api_key => GIGYA_API_KEY,
8
+ :api_secret => GIGYA_API_SECRET,
9
+ :use_ssl => false,
10
+ :domain => "us1"
11
+ })
12
+ @protocol = "http"
13
+ @method = "POST"
14
+ end
15
+
16
+
17
+ it_behaves_like RGigya
18
+
19
+ it "should verify signature after successful login" do
20
+ userInfo = {
21
+ 'nickname' => 'Gigems',
22
+ 'email' => 'ralph@cloudspace.com',
23
+ 'firstName' => 'Ralph',
24
+ 'lastName' => 'Masterson'
25
+ }
26
+
27
+ response = RGigya.socialize_notifyLogin({
28
+ :siteUID => '1',
29
+ :userInfo => userInfo.to_json
30
+ })
31
+
32
+ RGigya::SigUtils::validate_user_signature(response['UID'], response['signatureTimestamp'], response['UIDSignature']).should be_true
33
+
34
+ end
35
+
36
+ # Note: I stub everything here including the signature so this test really isn't valid
37
+ # But, it explains how it works
38
+ it "should verify friends signature after successful api call" do
39
+ RGigya.stub(:socialize_getFriendsInfo) do |url,options|
40
+ # this method is in helpers.rb
41
+ sample_frends_json_data
42
+ end
43
+
44
+ response = RGigya.socialize_getFriendsInfo({
45
+ :uid => '1',
46
+ :signIDs => true
47
+ })
48
+
49
+ friend = response['friends'].first
50
+
51
+ RGigya::SigUtils::validate_friend_signature("1", friend['signatureTimestamp'], friend['UID'], friend['friendshipSignature']).should be_true
52
+ end
53
+
54
+
55
+ # Not much of test here because the signature always change
56
+ # Lets just run it to make sure its not throwing any errors
57
+ it "should return the value for the dynamic session cookie" do
58
+ two_hours_from_now = (Time.now + (60*60*2)).utc.strftime("%s").to_i
59
+ RGigya::SigUtils::get_dynamic_session_signature("111111111", two_hours_from_now).should_not == ""
60
+ end
61
+
62
+ end
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,8 @@ RSpec.configure do |config|
4
4
  # some (optional) config here
5
5
  config.include Helpers
6
6
 
7
-
8
- # these need to be filled out for tests to work
9
- GIGYA_API_KEY = "<add api key here>"
10
- GIGYA_API_SECRET = "<add api secret here>"
11
-
7
+ # To verify Values
8
+ # GIGYA_API_KEY = "<add api key here>"
9
+ # GIGYA_API_SECRET = "<add api secret here>"
10
+
12
11
  end
data/test/dummy/Gemfile CHANGED
@@ -9,7 +9,6 @@ gem 'sqlite3'
9
9
 
10
10
  gem 'rgigya'
11
11
 
12
-
13
12
  # Gems used only for assets and not required
14
13
  # in production environments by default.
15
14
  group :assets do
@@ -49,13 +49,12 @@ GEM
49
49
  jquery-rails (2.2.1)
50
50
  railties (>= 3.0, < 5.0)
51
51
  thor (>= 0.14, < 2.0)
52
- json (1.7.7)
53
- mail (2.5.3)
54
- i18n (>= 0.4.0)
52
+ json (1.8.0)
53
+ mail (2.5.4)
55
54
  mime-types (~> 1.16)
56
55
  treetop (~> 1.4.8)
57
- mime-types (1.22)
58
- multi_json (1.7.2)
56
+ mime-types (1.23)
57
+ multi_json (1.7.3)
59
58
  multi_xml (0.5.3)
60
59
  polyglot (0.3.3)
61
60
  rack (1.4.5)
@@ -83,9 +82,9 @@ GEM
83
82
  rake (10.0.4)
84
83
  rdoc (3.12.2)
85
84
  json (~> 1.4)
86
- rgigya (0.1.0)
85
+ rgigya (1.0.2)
87
86
  httparty (~> 0.11.0)
88
- sass (3.2.7)
87
+ sass (3.2.9)
89
88
  sass-rails (3.2.6)
90
89
  railties (~> 3.2.0)
91
90
  sass (>= 3.1.10)
@@ -97,12 +96,12 @@ GEM
97
96
  tilt (~> 1.1, != 1.3.0)
98
97
  sqlite3 (1.3.7)
99
98
  thor (0.18.1)
100
- tilt (1.3.7)
99
+ tilt (1.4.1)
101
100
  treetop (1.4.12)
102
101
  polyglot
103
102
  polyglot (>= 0.3.1)
104
103
  tzinfo (0.3.37)
105
- uglifier (2.0.1)
104
+ uglifier (2.1.0)
106
105
  execjs (>= 0.3.0)
107
106
  multi_json (~> 1.0, >= 1.0.2)
108
107
 
@@ -38,5 +38,9 @@ end
38
38
 
39
39
 
40
40
  # The Gigya api key and secret need to be set here
41
- GIGYA_API_KEY = "<add api key here>"
42
- GIGYA_API_SECRET = "<add api secret here>"
41
+ RGigya.config({
42
+ :api_key => "<add api key here>",
43
+ :api_secret => "<add api secret here>",
44
+ :use_ssl => false,
45
+ :domain => "us1"
46
+ })