mini_fb 0.2.5 → 1.0.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.
Files changed (3) hide show
  1. data/README.markdown +65 -9
  2. data/lib/mini_fb.rb +93 -0
  3. metadata +4 -4
data/README.markdown CHANGED
@@ -3,13 +3,76 @@ MiniFB - the simple miniature facebook library
3
3
 
4
4
  MiniFB is a small, lightweight Ruby library for interacting with the [Facebook API](http://wiki.developers.facebook.com/index.php/API).
5
5
 
6
+ Support
7
+ --------
8
+
9
+ Join our Discussion Group at: http://groups.google.com/group/mini_fb
10
+
6
11
  Installation
7
12
  -------------
8
13
 
9
- We're using gemcutter so be sure to have gemcutter as a source, then:
10
-
11
14
  gem install mini_fb
12
15
 
16
+
17
+ Facebook Graph API
18
+ ==================
19
+
20
+ For an overview of what this is all about, see http://developers.facebook.com/docs/api.
21
+
22
+ Authentication
23
+ --------------
24
+
25
+ Facebook now uses Oauth 2 for authentication, but don't worry, this part is easy.
26
+
27
+ # Get your oauth url
28
+ @oauth_url = MiniFB.oauth_url(FB_APP_ID, # your Facebook App ID (NOT API_KEY)
29
+ "http://www.yoursite.com/sessions/create", # redirect url
30
+ :scope=>MiniFB.scopes.join(",")) # This asks for all permissions
31
+ # Have your users click on a link to @oauth_url
32
+ .....
33
+ # Then in your /sessions/create
34
+ access_token_hash = MiniFB.oauth_access_token(FB_APP_ID, "http://www.yoursite.com/sessions/create", FB_SECRET, params[:code])
35
+ @access_token = access_token_hash["access_token"]
36
+ # TODO: This is where you'd want to store the token in your database
37
+ # but for now, we'll just keep it in the cookie so we don't need a database
38
+ cookies[:access_token] = @access_token
39
+
40
+ That's it. You now need to hold onto this access_token. We've put it in a cookie for now, but you probably
41
+ want to store it in your database or something.
42
+
43
+ Getting Data from Facebook
44
+ --------------------------
45
+
46
+ It's very simple:
47
+
48
+ @id = {some ID of something in facebook} || "me"
49
+ @type = {some facebook type like feed, friends, or photos} # (optional) nil will just return the object data directly
50
+ @response_hash = MiniFB.get(@access_token, @id, :type=>@type)
51
+
52
+ Posting Data to Facebook
53
+ ------------------------
54
+
55
+ Also pretty simple:
56
+
57
+ @id = {some ID of something in facebook}
58
+ @type = {some type of post like comments, likes, feed} # required here
59
+ @response_hash = MiniFB.post(@access_token, @id, :type=>@type)
60
+
61
+
62
+ Logging
63
+ -------
64
+
65
+ To enabled logging:
66
+
67
+ MiniFB.enable_logging
68
+
69
+
70
+ Original Facebook API
71
+ =====================
72
+
73
+ This API will probably go away at some point, so you should use the Graph API above in most cases.
74
+
75
+
13
76
  General Usage
14
77
  -------------
15
78
 
@@ -85,10 +148,3 @@ This is as simple as calling:
85
148
  @fb.call("photos.upload", "filename"=>"<full path to file>")
86
149
 
87
150
  The file_name parameter will be used as the file data.
88
-
89
-
90
- Support
91
- --------
92
-
93
- Join our Discussion Group at: http://groups.google.com/group/mini_fb
94
-
data/lib/mini_fb.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'digest/md5'
2
2
  require 'erb'
3
3
  require 'json' unless defined? JSON
4
+ require 'rest_client'
5
+ require 'hashie'
4
6
 
5
7
  module MiniFB
6
8
 
@@ -298,6 +300,97 @@ module MiniFB
298
300
  login_url
299
301
  end
300
302
 
303
+ def self.graph_base
304
+ "https://graph.facebook.com/"
305
+ end
306
+
307
+ # options:
308
+ # - scope: comma separated list of extends permissions. see http://developers.facebook.com/docs/authentication/permissions
309
+ def self.oauth_url(app_id, redirect_uri, options={})
310
+ oauth_url = "#{graph_base}oauth/authorize"
311
+ oauth_url << "?client_id=#{app_id}"
312
+ oauth_url << "&redirect_uri=#{URI.escape(redirect_uri)}"
313
+ oauth_url << "&scope=#{options[:scope]}" if options[:scope]
314
+ end
315
+
316
+ # returns a hash with one value being 'access_token', the other being 'expires'
317
+ def self.oauth_access_token(app_id, redirect_uri, secret, code)
318
+ oauth_url = "#{graph_base}oauth/access_token"
319
+ oauth_url << "?client_id=#{app_id}"
320
+ oauth_url << "&redirect_uri=#{URI.escape(redirect_uri)}"
321
+ oauth_url << "&client_secret=#{secret}"
322
+ oauth_url << "&code=#{URI.escape(code)}"
323
+ resp = RestClient.get oauth_url
324
+ puts 'resp=' + resp.body.to_s if @@logging
325
+ params = {}
326
+ params_array = resp.split("&")
327
+ params_array.each do |p|
328
+ ps = p.split("=")
329
+ params[ps[0]] = ps[1]
330
+ end
331
+ return params
332
+ end
333
+
334
+ # options:
335
+ # - type: eg: feed, home, etc
336
+ # - metadata: to include metadata in response. true/false
337
+ def self.get(access_token, id, options={})
338
+ url = "#{graph_base}#{id}"
339
+ url << "/#{options[:type]}" if options[:type]
340
+ url << "?access_token=#{URI.escape(access_token)}"
341
+ url << "&metadata=1" if options[:metadata]
342
+ puts 'url=' + url if @@logging
343
+ begin
344
+ resp = RestClient.get url
345
+ rescue RestClient::Exception => ex
346
+ puts ex.http_code.to_s
347
+ puts 'ex.http_body=' + ex.http_body if @@logging
348
+ res_hash = JSON.parse(ex.http_body) # probably should ensure it has a good response
349
+ raise MiniFB::FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}")
350
+ end
351
+ puts 'resp=' + resp.body.to_s if @@logging
352
+ res_hash = JSON.parse(resp.body)
353
+ res_hash = Hashie::Mash.new(res_hash)
354
+ return res_hash
355
+ end
356
+
357
+ # options:
358
+ # - type: eg: feed, home, etc
359
+ # - metadata: to include metadata in response. true/false
360
+ def self.post(access_token, id, options={})
361
+ url = "#{graph_base}#{id}"
362
+ url << "/#{options[:type]}" if options[:type]
363
+ params = {}
364
+ params["access_token"] = "#{(access_token)}"
365
+ params["metadata"] = "1" if options[:metadata]
366
+ puts 'url=' + url if @@logging
367
+ begin
368
+ resp = RestClient.post url, params
369
+ rescue RestClient::Exception => ex
370
+ puts ex.http_code.to_s
371
+ puts 'ex.http_body=' + ex.http_body if @@logging
372
+ res_hash = JSON.parse(ex.http_body) # probably should ensure it has a good json response
373
+ raise MiniFB::FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}")
374
+ end
375
+ puts 'resp=' + resp.body.to_s if @@logging
376
+ res_hash = JSON.parse(resp.body)
377
+ res_hash = Hashie::Mash.new(res_hash)
378
+ return res_hash
379
+ end
380
+
381
+ # Returns all available scopes.
382
+ def self.scopes
383
+ all_scopes = []
384
+ scope_names = ["about_me", "activities", "birthday", "education_history", "events", "groups",
385
+ "interests", "likes",
386
+ "location", "notes", "online_presence", "photo_video_tags", "photos", "relationships",
387
+ "religion_politics", "status", "videos", "website", "work_history"]
388
+ scope_names.each { |x| all_scopes << "user_" + x; all_scopes << "friends_" + x}
389
+ all_scopes << "read_friendlists"
390
+ all_scopes << "read_stream"
391
+ all_scopes
392
+ end
393
+
301
394
  # This function expects arguments as a hash, so
302
395
  # it is agnostic to different POST handling variants in ruby.
303
396
  #
metadata CHANGED
@@ -3,10 +3,10 @@ name: mini_fb
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
+ - 1
6
7
  - 0
7
- - 2
8
- - 5
9
- version: 0.2.5
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Reeder
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-20 00:00:00 -07:00
18
+ date: 2010-04-29 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency