mini_fb 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +39 -10
  2. data/lib/mini_fb.rb +132 -12
  3. metadata +4 -4
data/README.markdown CHANGED
@@ -104,18 +104,16 @@ Oauth 2.0 Authentication and Original Rest Api
104
104
 
105
105
  You can use the Graph api Oauth 2.0 token with original api methods. BEWARE: This has only been tested against stream.publish at present.
106
106
 
107
- MiniFB.rest(@access_token, "rest.api.method", options)
107
+ MiniFB.rest(@access_token, "rest.api.method", options)
108
108
 
109
- eg:
109
+ eg:
110
110
 
111
- response = MiniFB.rest(@access_token, "stream.publish", {
112
- :uid => @user_id,
113
- :to => @to_user_id,
114
- :message => "....message.....",
115
- :method => :post
116
- })
117
-
118
- the :method will default to :get if no value is supplied and all responses will be json. In the instance of 'bad json' methods, the response will formatted {'response': '#{bad_response_string}'}
111
+ response = MiniFB.rest(@access_token, "stream.publish", :params => {
112
+ :uid => @user_id, :target_id => @target_user_id,
113
+ :message => "Hello other user!"
114
+ })
115
+
116
+ all responses will be json. In the instance of 'bad json' methods, the response will formatted {'response': '#{bad_response_string}'}
119
117
 
120
118
 
121
119
  Some Higher Level Objects for Common Uses
@@ -145,6 +143,37 @@ Or if you want other photos, try:
145
143
 
146
144
  photos = @fb.photos("pids"=>[12343243,920382343,9208348])
147
145
 
146
+
147
+ Higher Level Objects with OAuth2
148
+ --------------------------------
149
+
150
+ Get a MiniFB::OAuthSession with a Spanish locale:
151
+
152
+ @fb = MiniFB::OAuthSession.new(access_token, 'es_ES')
153
+
154
+ Using the session object to make requests:
155
+
156
+ @fb.get('117199051648010')
157
+ @fb.post('me', :type => :feed, :params => {
158
+ :message => "This is me from MiniFB"
159
+ })
160
+ @fb.fql('SELECT id FROM object_url WHERE url="http://www.imdb.com/title/tt1250777/"')
161
+ @fb.rest('notes.create', :params => {
162
+ :title => "ToDo", :content => "Try MiniFB"
163
+ })
164
+
165
+ Getting graph objects through the session:
166
+
167
+ @fb.me
168
+ @fb.me.name
169
+ @fb.me.connections
170
+ @fb.me.feed
171
+
172
+ @ssp = @fb.graph_object('117199051648010')
173
+ @ssp.mission
174
+ @ssp.photos
175
+
176
+
148
177
  Facebook Connect
149
178
  ----------------
150
179
 
data/lib/mini_fb.rb CHANGED
@@ -313,6 +313,105 @@ module MiniFB
313
313
  login_url
314
314
  end
315
315
 
316
+ # Manages access_token and locale params for an OAuth connection
317
+ class OAuthSession
318
+
319
+ def initialize(access_token, locale="en_US")
320
+ @access_token = access_token
321
+ @locale = locale
322
+ end
323
+
324
+ def get(id, options={})
325
+ MiniFB.get(@access_token, id, session_options(options))
326
+ end
327
+
328
+ def post(id, options={})
329
+ MiniFB.post(@access_token, id, session_options(options))
330
+ end
331
+
332
+ def fql(fql_query, options={})
333
+ MiniFB.fql(@access_token, fql_query, session_options(options))
334
+ end
335
+
336
+ def multifql(fql_queries, options={})
337
+ MiniFB.multifql(@access_token, fql_queries, session_options(options))
338
+ end
339
+
340
+ def rest(api_method, options={})
341
+ MiniFB.rest(@access_token, api_method, session_options(options))
342
+ end
343
+
344
+ # Returns a GraphObject for the given id
345
+ def graph_object(id)
346
+ MiniFB::GraphObject.new(self, id)
347
+ end
348
+
349
+ # Returns and caches a GraphObject for the user
350
+ def me
351
+ @me ||= graph_object('me')
352
+ end
353
+
354
+ private
355
+ def session_options(options)
356
+ (options[:params] ||= {})[:locale] ||= @locale
357
+ options
358
+ end
359
+ end
360
+
361
+ # Wraps a graph object for easily accessing its connections
362
+ class GraphObject
363
+ # Creates a GraphObject using an OAuthSession or access_token
364
+ def initialize(session_or_token, id)
365
+ @oauth_session = if session_or_token.is_a?(MiniFB::OAuthSession)
366
+ session_or_token
367
+ else
368
+ MiniFB::OAuthSession.new(session_or_token)
369
+ end
370
+ @id = id
371
+ @object = @oauth_session.get(id, :metadata => true)
372
+ @connections_cache = {}
373
+ end
374
+
375
+ def inspect
376
+ "<##{self.class.name} #{@object.inspect}>"
377
+ end
378
+
379
+ def connections
380
+ @object.metadata.connections.keys
381
+ end
382
+
383
+ undef :id, :type
384
+
385
+ def methods
386
+ super + @object.keys.include?(key) + connections.include?(key)
387
+ end
388
+
389
+ def respond_to?(method)
390
+ @object.keys.include?(key) || connections.include?(key) || super
391
+ end
392
+
393
+ def keys
394
+ @object.keys
395
+ end
396
+
397
+ def [](key)
398
+ @object[key]
399
+ end
400
+
401
+ def method_missing(method, *args, &block)
402
+ key = method.to_s
403
+ if @object.keys.include?(key)
404
+ @object[key]
405
+ elsif @connections_cache.has_key?(key)
406
+ @connections_cache[key]
407
+ elsif connections.include?(key)
408
+ @connections_cache[key] = @oauth_session.get(@id, :type => key)
409
+ else
410
+ super
411
+ end
412
+ end
413
+ end
414
+
316
415
  def self.graph_base
317
416
  "https://graph.facebook.com/"
318
417
  end
@@ -398,6 +497,22 @@ module MiniFB
398
497
  return fetch(url, options)
399
498
  end
400
499
 
500
+ # Executes multiple FQL queries
501
+ # Example:
502
+ #
503
+ # MiniFB.multifql(access_token, { :statuses => "SELECT status_id, message FROM status WHERE uid = 12345",
504
+ # :privacy => "SELECT object_id, description FROM privacy WHERE object_id IN (SELECT status_id FROM #statuses)" })
505
+ def self.multifql(access_token, fql_queries, options={})
506
+ url = "https://api.facebook.com/method/fql.multiquery"
507
+ params = options[:params] || {}
508
+ params["access_token"] = "#{(access_token)}"
509
+ params["metadata"] = "1" if options[:metadata]
510
+ params["queries"] = JSON[fql_queries]
511
+ params[:format] = "JSON"
512
+ options[:params] = params
513
+ return fetch(url, options)
514
+ end
515
+
401
516
  # Uses new Oauth 2 authentication against old Facebook REST API
402
517
  # options:
403
518
  # - params: Any additional parameters you would like to submit
@@ -439,11 +554,11 @@ module MiniFB
439
554
  else
440
555
  res_hash = Hashie::Mash.new(res_hash)
441
556
  end
442
-
557
+
443
558
  if res_hash.include?("error_msg")
444
559
  raise FaceBookError.new(res_hash["error_code"] || 1, res_hash["error_msg"])
445
560
  end
446
-
561
+
447
562
  return res_hash
448
563
  rescue RestClient::Exception => ex
449
564
  puts ex.http_code.to_s
@@ -456,16 +571,21 @@ module MiniFB
456
571
 
457
572
  # Returns all available scopes.
458
573
  def self.scopes
459
- all_scopes = []
460
- scope_names = ["about_me", "activities", "birthday", "education_history", "events", "groups",
461
- "interests", "likes",
462
- "location", "notes", "online_presence", "photo_video_tags", "photos", "relationships",
463
- "religion_politics", "status", "videos", "website", "work_history"]
464
- scope_names.each { |x| all_scopes << "user_" + x; all_scopes << "friends_" + x }
465
- all_scopes << "read_friendlists"
466
- all_scopes << "read_stream"
467
- all_scopes << "publish_stream"
468
- all_scopes
574
+ scopes = %w{
575
+ about_me activities birthday education_history events groups
576
+ hometown interests likes location notes online_presence
577
+ photo_video_tags photos relationships religion_politics
578
+ status videos website work_history
579
+ }
580
+ scopes.map! do |scope|
581
+ ["user_#{scope}", "friends_#{scope}"]
582
+ end.flatten!
583
+
584
+ scopes += %w{
585
+ read_insights read_stream read_mailbox read_friendlists read_requests
586
+ email ads_management xmpp_login
587
+ publish_stream create_event rsvp_event sms offline_access
588
+ }
469
589
  end
470
590
 
471
591
  # This function expects arguments as a hash, so
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: 27
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 6
10
- version: 1.0.6
10
+ version: 1.1.0
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-25 00:00:00 -07:00
19
+ date: 2010-07-05 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency