mini_fb 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mini_fb.rb +68 -46
- metadata +4 -4
data/lib/mini_fb.rb
CHANGED
@@ -36,9 +36,9 @@ module MiniFB
|
|
36
36
|
class FaceBookError < StandardError
|
37
37
|
attr_accessor :code
|
38
38
|
# Error that happens during a facebook call.
|
39
|
-
def initialize(
|
39
|
+
def initialize(error_code, error_msg)
|
40
40
|
@code = error_code
|
41
|
-
super("Facebook error #{error_code}: #{error_msg}"
|
41
|
+
super("Facebook error #{error_code}: #{error_msg}")
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -152,7 +152,7 @@ module MiniFB
|
|
152
152
|
|
153
153
|
# The secret argument should be an instance of FacebookSecret
|
154
154
|
# to hide value from simple introspection.
|
155
|
-
def MiniFB.call(
|
155
|
+
def MiniFB.call(api_key, secret, method, kwargs)
|
156
156
|
|
157
157
|
puts 'kwargs=' + kwargs.inspect if @@logging
|
158
158
|
|
@@ -185,7 +185,7 @@ module MiniFB
|
|
185
185
|
else
|
186
186
|
|
187
187
|
begin
|
188
|
-
response = Net::HTTP.post_form(
|
188
|
+
response = Net::HTTP.post_form(URI.parse(FB_URL), post_params(kwargs))
|
189
189
|
rescue SocketError => err
|
190
190
|
# why are we catching this and throwing as different error? hmmm..
|
191
191
|
# raise IOError.new( "Cannot connect to the facebook server: " + err )
|
@@ -200,9 +200,9 @@ module MiniFB
|
|
200
200
|
|
201
201
|
puts 'response=' + body.inspect if @@logging
|
202
202
|
begin
|
203
|
-
data = JSON.parse(
|
204
|
-
if data.include?(
|
205
|
-
raise FaceBookError.new(
|
203
|
+
data = JSON.parse(body)
|
204
|
+
if data.include?("error_msg")
|
205
|
+
raise FaceBookError.new(data["error_code"] || 1, data["error_msg"])
|
206
206
|
end
|
207
207
|
|
208
208
|
rescue JSON::ParserError => ex
|
@@ -242,12 +242,12 @@ module MiniFB
|
|
242
242
|
|
243
243
|
# Call Facebook with POST multipart/form-data request
|
244
244
|
uri = URI.parse(FB_URL)
|
245
|
-
Net::HTTP.start(uri.host) {|http| http.post uri.path, query, header}
|
245
|
+
Net::HTTP.start(uri.host) { |http| http.post uri.path, query, header }
|
246
246
|
end
|
247
247
|
|
248
248
|
# Returns true is signature is valid, false otherwise.
|
249
|
-
def MiniFB.verify_signature(
|
250
|
-
signature = arguments.delete(
|
249
|
+
def MiniFB.verify_signature(secret, arguments)
|
250
|
+
signature = arguments.delete("fb_sig")
|
251
251
|
return false if signature.nil?
|
252
252
|
|
253
253
|
unsigned = Hash.new
|
@@ -263,12 +263,12 @@ module MiniFB
|
|
263
263
|
|
264
264
|
arg_string = String.new
|
265
265
|
signed.sort.each { |kv| arg_string << kv[0] << "=" << kv[1] }
|
266
|
-
if Digest::MD5.hexdigest(
|
266
|
+
if Digest::MD5.hexdigest(arg_string + secret) == signature
|
267
267
|
return true
|
268
268
|
end
|
269
269
|
return false
|
270
270
|
end
|
271
|
-
|
271
|
+
|
272
272
|
# Parses cookies in order to extract the facebook cookie and parse it into a useable hash
|
273
273
|
#
|
274
274
|
# options:
|
@@ -276,8 +276,8 @@ module MiniFB
|
|
276
276
|
# * secret - the connect application secret
|
277
277
|
# * cookies - the cookies given by facebook - it is ok to just pass all of the cookies, the method will do the filtering for you.
|
278
278
|
def MiniFB.parse_cookie_information(app_id, cookies)
|
279
|
-
|
280
|
-
|
279
|
+
return nil if cookies["fbs_#{app_id}"].nil?
|
280
|
+
Hash[*cookies["fbs_#{app_id}"].split('&').map { |v| v.gsub('"', '').split('=', 2) }.flatten]
|
281
281
|
end
|
282
282
|
|
283
283
|
# Validates that the cookies sent by the user are those that were set by facebook. Since your
|
@@ -288,17 +288,17 @@ module MiniFB
|
|
288
288
|
# * secret - the connect application secret
|
289
289
|
# * cookies - the cookies given by facebook - it is ok to just pass all of the cookies, the method will do the filtering for you.
|
290
290
|
def MiniFB.verify_cookie_signature(app_id, secret, cookies)
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
291
|
+
fb_keys = MiniFB.parse_cookie_information(app_id, cookies)
|
292
|
+
return false if fb_keys.nil?
|
293
|
+
|
294
|
+
signature = fb_keys.delete('sig')
|
295
|
+
return signature == Digest::MD5.hexdigest(fb_keys.map { |k, v| "#{k}=#{v}" }.sort.join + secret)
|
296
296
|
end
|
297
|
-
|
297
|
+
|
298
298
|
# <b>DEPRECATED:</b> Please use <tt>verify_cookie_signature</tt> instead.
|
299
299
|
def MiniFB.verify_connect_signature(api_key, secret, cookies)
|
300
|
-
|
301
|
-
|
300
|
+
warn "DEPRECATION WARNING: 'verify_connect_signature' has been renamed to 'verify_cookie_signature' as Facebook no longer calls this 'connect'"
|
301
|
+
MiniFB.verify_cookie_signature(api_key, secret, cookies)
|
302
302
|
end
|
303
303
|
|
304
304
|
# Returns the login/add app url for your application.
|
@@ -324,7 +324,7 @@ module MiniFB
|
|
324
324
|
oauth_url << "?client_id=#{app_id}"
|
325
325
|
oauth_url << "&redirect_uri=#{URI.escape(redirect_uri)}"
|
326
326
|
# oauth_url << "&scope=#{options[:scope]}" if options[:scope]
|
327
|
-
oauth_url << ("&" + options.
|
327
|
+
oauth_url << ("&" + options.map { |k, v| "%s=%s" % [k, v] }.join('&')) unless options.empty?
|
328
328
|
oauth_url
|
329
329
|
end
|
330
330
|
|
@@ -357,7 +357,8 @@ module MiniFB
|
|
357
357
|
params = options[:params] || {}
|
358
358
|
params["access_token"] = "#{(access_token)}"
|
359
359
|
params["metadata"] = "1" if options[:metadata]
|
360
|
-
|
360
|
+
options[:params] = params
|
361
|
+
return fetch(url, options)
|
361
362
|
end
|
362
363
|
|
363
364
|
# Posts data to the Facebook Graph API
|
@@ -368,10 +369,20 @@ module MiniFB
|
|
368
369
|
def self.post(access_token, id, options={})
|
369
370
|
url = "#{graph_base}#{id}"
|
370
371
|
url << "/#{options[:type]}" if options[:type]
|
372
|
+
options.delete(:type)
|
371
373
|
params = options[:params] || {}
|
374
|
+
options.each do |key, value|
|
375
|
+
if value.kind_of?(File)
|
376
|
+
params[key] = value
|
377
|
+
else
|
378
|
+
params[key] = "#{value}"
|
379
|
+
end
|
380
|
+
end
|
372
381
|
params["access_token"] = "#{(access_token)}"
|
373
382
|
params["metadata"] = "1" if options[:metadata]
|
374
|
-
|
383
|
+
options[:params] = params
|
384
|
+
options[:method] = :post
|
385
|
+
return fetch(url, options)
|
375
386
|
|
376
387
|
end
|
377
388
|
|
@@ -383,45 +394,56 @@ module MiniFB
|
|
383
394
|
params["metadata"] = "1" if options[:metadata]
|
384
395
|
params["query"] = fql_query
|
385
396
|
params["format"] = "JSON"
|
386
|
-
|
397
|
+
options[:params] = params
|
398
|
+
return fetch(url, options)
|
387
399
|
end
|
388
400
|
|
389
401
|
# Uses new Oauth 2 authentication against old Facebook REST API
|
402
|
+
# options:
|
403
|
+
# - params: Any additional parameters you would like to submit
|
390
404
|
def self.rest(access_token, api_method, options={})
|
391
405
|
url = "https://api.facebook.com/method/#{api_method}"
|
392
406
|
params = options[:params] || {}
|
393
407
|
params[:access_token] = access_token
|
394
408
|
params[:format] = "JSON"
|
395
|
-
|
409
|
+
options[:params] = params
|
410
|
+
return fetch(url, options)
|
396
411
|
end
|
397
412
|
|
398
413
|
|
399
414
|
def self.fetch(url, options={})
|
400
|
-
|
401
|
-
begin
|
402
|
-
if options[:method] == :post
|
415
|
+
|
416
|
+
begin
|
417
|
+
if options[:method] == :post
|
418
|
+
puts 'url_post=' + url if @@logging
|
403
419
|
resp = RestClient.post url, options[:params]
|
404
420
|
else
|
405
421
|
if options[:params] && options[:params].size > 0
|
406
|
-
|
422
|
+
url += '?' + options[:params].map { |k, v| URI.escape("%s=%s" % [k, v]) }.join('&')
|
407
423
|
end
|
424
|
+
puts 'url_get=' + url if @@logging
|
408
425
|
resp = RestClient.get url
|
409
426
|
end
|
410
|
-
|
427
|
+
|
411
428
|
puts 'resp=' + resp.body.to_s if @@logging
|
412
|
-
|
413
|
-
begin
|
414
|
-
|
429
|
+
|
430
|
+
begin
|
431
|
+
res_hash = JSON.parse(resp.body)
|
415
432
|
rescue
|
416
|
-
|
417
|
-
|
418
|
-
end
|
419
|
-
|
420
|
-
if res_hash.is_a? Array
|
421
|
-
res_hash.collect! {|x| Hashie::Mash.new(x) }
|
433
|
+
# quick fix for things like stream.publish that don't return json
|
434
|
+
res_hash = JSON.parse("{\"response\": #{resp.body.to_s}}")
|
435
|
+
end
|
436
|
+
|
437
|
+
if res_hash.is_a? Array # fql return this
|
438
|
+
res_hash.collect! { |x| Hashie::Mash.new(x) }
|
422
439
|
else
|
423
440
|
res_hash = Hashie::Mash.new(res_hash)
|
424
441
|
end
|
442
|
+
|
443
|
+
if res_hash.include?("error_msg")
|
444
|
+
raise FaceBookError.new(res_hash["error_code"] || 1, res_hash["error_msg"])
|
445
|
+
end
|
446
|
+
|
425
447
|
return res_hash
|
426
448
|
rescue RestClient::Exception => ex
|
427
449
|
puts ex.http_code.to_s
|
@@ -439,7 +461,7 @@ module MiniFB
|
|
439
461
|
"interests", "likes",
|
440
462
|
"location", "notes", "online_presence", "photo_video_tags", "photos", "relationships",
|
441
463
|
"religion_politics", "status", "videos", "website", "work_history"]
|
442
|
-
scope_names.each { |x| all_scopes << "user_" + x; all_scopes << "friends_" + x}
|
464
|
+
scope_names.each { |x| all_scopes << "user_" + x; all_scopes << "friends_" + x }
|
443
465
|
all_scopes << "read_friendlists"
|
444
466
|
all_scopes << "read_stream"
|
445
467
|
all_scopes << "publish_stream"
|
@@ -464,9 +486,9 @@ module MiniFB
|
|
464
486
|
# to hide value from simple introspection.
|
465
487
|
#
|
466
488
|
# DEPRECATED, use verify_signature instead
|
467
|
-
def MiniFB.validate(
|
489
|
+
def MiniFB.validate(secret, arguments)
|
468
490
|
|
469
|
-
signature = arguments.delete(
|
491
|
+
signature = arguments.delete("fb_sig")
|
470
492
|
return arguments if signature.nil?
|
471
493
|
|
472
494
|
unsigned = Hash.new
|
@@ -482,7 +504,7 @@ module MiniFB
|
|
482
504
|
|
483
505
|
arg_string = String.new
|
484
506
|
signed.sort.each { |kv| arg_string << kv[0] << "=" << kv[1] }
|
485
|
-
if Digest::MD5.hexdigest(
|
507
|
+
if Digest::MD5.hexdigest(arg_string + secret) != signature
|
486
508
|
unsigned # Hash is incorrect, return only unsigned fields.
|
487
509
|
else
|
488
510
|
unsigned.merge signed
|
@@ -494,7 +516,7 @@ module MiniFB
|
|
494
516
|
# Proc cannot be dumped or introspected by normal tools.
|
495
517
|
attr_reader :value
|
496
518
|
|
497
|
-
def initialize(
|
519
|
+
def initialize(value)
|
498
520
|
@value = Proc.new { value }
|
499
521
|
end
|
500
522
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_fb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 6
|
10
|
+
version: 1.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Travis Reeder
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-06-
|
19
|
+
date: 2010-06-25 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|