RFb 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,11 @@
1
+ # RFb
2
+
3
+ *RubyFacebook* - interact with Facebook REST API (json) - rails3 enabled
4
+
5
+
6
+ ## Installation
7
+
8
+ ## Usage
9
+
10
+
11
+ yes.. documentation will come....
data/RFb.gemspec ADDED
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{RFb}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Francesco 'makevoid' Canessa"]
12
+ s.date = %q{2010-02-11}
13
+ s.description = %q{RubyFacebook interacts with Facebook REST API via JSON. No XML to deal with, no parsers needed, reduced verbosity, increased happiness. Ruby1.9 required. Fork it, please!}
14
+ s.email = %q{makevoid@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "RFb.gemspec",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/r_fb.rb",
25
+ "lib/r_fb/configurable.rb",
26
+ "lib/r_fb/cookie.rb",
27
+ "lib/r_fb/exceptions.rb",
28
+ "lib/r_fb/fql.rb",
29
+ "lib/r_fb/methods.rb",
30
+ "lib/r_fb/parser.rb",
31
+ "lib/r_fb/session.rb",
32
+ "lib/r_fb/user.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/makevoid/RFb}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{RubyFacebook - interact with Facebook REST API (json) - rails3 enabled}
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
50
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # Rakefile
2
+ begin
3
+ require 'jeweler'
4
+ Jeweler::Tasks.new do |gemspec|
5
+ gemspec.name = "RFb"
6
+ gemspec.summary = "RubyFacebook - interact with Facebook REST API (json) - rails3 enabled"
7
+ gemspec.description = "RubyFacebook interacts with Facebook REST API via JSON. No XML to deal with, no parsers needed, reduced verbosity, increased happiness. Ruby1.9 required. Fork it, please!"
8
+ gemspec.email = "makevoid@gmail.com"
9
+ gemspec.homepage = "http://github.com/makevoid/RFb"
10
+ gemspec.authors = ["Francesco 'makevoid' Canessa"]
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
15
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'r_fb'
@@ -0,0 +1,41 @@
1
+ module Configurable
2
+ def environment=(env)
3
+ ENV["rfb_environment"] = env.to_sym
4
+ end
5
+ def environment
6
+ ENV["rfb_environment"]
7
+ end
8
+
9
+ def api_key=(key)
10
+ ENV["rfb_api_key"] = key[environment.to_sym]
11
+ end
12
+ def api_key
13
+ ENV["rfb_api_key"]
14
+ end
15
+
16
+ def secret_key=(secr)
17
+ ENV["rfb_secret"] = secr[environment.to_sym]
18
+ end
19
+ def secret_key
20
+ ENV["rfb_secret"]
21
+ end
22
+
23
+ def app_name=(name)
24
+ ENV["rfb_app_name"] = name
25
+ end
26
+ def app_name
27
+ ENV["rfb_app_name"]
28
+ end
29
+
30
+ def debug=(value)
31
+ ENV["rfb_debug"] = value ? "1" : nil
32
+ end
33
+ def debug
34
+ ENV["rfb_debug"]
35
+ end
36
+
37
+ def app_url
38
+ raise RFbAppNameUndefined if app_name.nil?
39
+ "http://apps.facebook.com/#{app_name}"
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ module RFb
2
+ class Cookie
3
+ METHODS = {
4
+ get: "Data.getCookie"
5
+ set: "Data.setCookie"
6
+ }
7
+
8
+ # TODO: remove if unused, implement if needed :)
9
+
10
+ def get
11
+
12
+ end
13
+
14
+ def set
15
+ response = RFb.request({
16
+ method: METHODS[:set],
17
+ name: "test",
18
+ value: 123
19
+ })
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module RFb
2
+ module Exceptions
3
+ class RFbAppException < Exception
4
+ def message
5
+ "Ruby Fb internal general error"
6
+ end
7
+ end
8
+ class RFbAppNameUndefined < RFbAppException
9
+ def message
10
+ "AppName not defined! - define it by adding this in your environment: RFb.app_name = 'your_app_name'"
11
+ end
12
+ end
13
+ class RFbEnvUndefined < RFbAppException
14
+ def message
15
+ "Environment not defined! - define it by adding this in your environment: RFb.environment = Rails.env for rails, RFb.environment = ENV['RACK_ENV'] for rack and so on..."
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/r_fb/fql.rb ADDED
@@ -0,0 +1,13 @@
1
+ module RFb
2
+ class Fql
3
+
4
+ def self.query(string)
5
+ RFb.request({
6
+ method: "facebook.Fql.query",
7
+ query: string,
8
+ session_key: Session.session_key
9
+ })
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module RFb
2
+ module Methods
3
+ AUTH_GET_SESSION = "facebook.Auth.getSession"
4
+ PHOTOS_GET_ALBUMS = "facebook.photos.getAlbums"
5
+ USERS_GET_INFO = "facebook.users.getInfo"
6
+ PHOTOS_GET_FROM_ALBUM = "facebook.photos.getFromAlbum"
7
+ PHOTOS_GET_OF_USER = "facebook.photos.getOfUser"
8
+ FRIENDS_GET = "facebook.friends.get"
9
+ WALL_GET_COUNT = "facebook.wall.getCount"
10
+ MESSAGES_GET_COUNT = "facebook.messages.getCount"
11
+ POKES_GET_COUNT = "facebook.pokes.getCount"
12
+ PHOTOS_GET_COMMENT_COUNT = "facebook.photos.getCommentCount"
13
+ SESSION_PING = "facebook.session.ping"
14
+
15
+ FRIENDS_GET_TYPED = "facebook.friends.getTyped"
16
+ FRIENDS_ARE_FRIENDS = "facebook.friends.areFriends"
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module RFb
2
+
3
+ class Parser
4
+ attr_accessor :doc
5
+
6
+ def self.parse(response)
7
+ parser = Parser.new(response)
8
+
9
+ if parser.doc.is_a?(Hash) && !parser.doc["error_code"].nil?
10
+ raise( { code: parser.doc["error_code"], message: parser.doc["error_msg"] }.inspect )
11
+ end
12
+
13
+ parser.doc.first
14
+ end
15
+
16
+ def initialize(response)
17
+ @doc = JSON.parse(response.body)
18
+
19
+ case response
20
+ when Net::HTTPSuccess
21
+ else
22
+ raise "Parser - Net::HTTP - not yet handled error: #{@doc.inspect}"
23
+ #raise( { code: error_code, message: @doc.xpath("//error_msg").text }.inspect )
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ module RFb
2
+
3
+ class Session
4
+ attr_accessor :params
5
+ def initialize(params={})
6
+ @params = filter_session_params(params)
7
+ @@session_key = params["fb_sig_session_key"]
8
+ end
9
+
10
+ def self.session_key
11
+ @@session_key
12
+ end
13
+
14
+ def logged_in?
15
+ !@params[:fb_sig_logged_out_facebook]
16
+ end
17
+
18
+ def user_has_added_app?
19
+ @params[:fb_sig_added]
20
+ end
21
+
22
+ def active?
23
+ !@params[:fb_sig_user].nil?
24
+ end
25
+
26
+ def filter_session_params(params)
27
+ to_filter = [:fb_sig_in_iframe, :fb_sig_in_new_facebook, :fb_sig_added, :fb_sig_logged_out_facebook].map(&:to_s)
28
+ params.map do |k, v|
29
+ params[k] = v == true || v.to_i == 1 if to_filter.include? k
30
+ end
31
+ params
32
+ end
33
+ end
34
+
35
+
36
+ end
data/lib/r_fb/user.rb ADDED
@@ -0,0 +1,69 @@
1
+ module RFb
2
+ class User
3
+
4
+ FIELDS = [:first_name, :name, :current_location, :has_added_app, :locale, :pic, :profile_url, :status, :timezone, :username]
5
+ EXCLUDED_FIELDS = [:current_location]
6
+
7
+ attr_accessor :fb_id
8
+ attr_accessor :user
9
+ FIELDS.map do |field|
10
+ attr_accessor field
11
+ end
12
+
13
+ def initialize(attrs={})
14
+ attrs.map do |attr_name, attr_value|
15
+ instance_variable_set("@#{attr_name}", attr_value)
16
+ end
17
+ @user = attrs[:user]
18
+ @fb_id = attrs[:fb_id]
19
+ end
20
+
21
+ def self.first(attrs={})
22
+ raise "RFb::User.first called!"
23
+ user = ::User.first(:fb_id => attrs[:fb_id])
24
+ u = User.new(attrs.merge(:user => user))
25
+ u.select_all
26
+ end
27
+
28
+ def self.find_or_create(attrs={})
29
+ # calls DataMapper model class User
30
+ user = ::User.first(:fb_id => attrs[:fb_id])
31
+ user = ::User.create(:fb_id => attrs[:fb_id]) if user.nil?
32
+ u = User.new(attrs.merge(:user => user)).select_all
33
+ end
34
+
35
+ def friends
36
+ Fql.query("SELECT #{FIELDS.join(", ")} FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=#{self.fb_id}) AND has_added_app = 1 ")
37
+ end
38
+
39
+ def is_app_user?
40
+ has_added_app
41
+ end
42
+
43
+ def select_all
44
+ fb_uids = self.fb_id.to_i
45
+ raise "Facebook user id is 0/nil for user: #{self.inspect}" if fb_uids == 0
46
+ parsed_response = RFb.request({
47
+ method: "facebook.Users.getInfo",
48
+ uids: [fb_uids].to_s,
49
+ fields: FIELDS.map(&:to_s).to_s
50
+ })
51
+ # puts "parsed response: [uids: #{[self.fb_id.to_i]} ] "
52
+ # puts parsed_response.inspect
53
+ FIELDS.map do |field|
54
+ value = parsed_response[field.to_s]
55
+ instance_variable_set("@#{field}", value)
56
+ user.send("#{field}=", value) unless EXCLUDED_FIELDS.include?(field)
57
+ end
58
+ #user.update_attributes(name: "Francesco")
59
+
60
+ unless user.save
61
+ raise "RFb::User#select_all failed, failed to #user.save, user: #{user.errors.inspect}"
62
+ end
63
+
64
+ self
65
+ end
66
+
67
+
68
+ end
69
+ end
data/lib/r_fb.rb ADDED
@@ -0,0 +1,82 @@
1
+ require 'digest/md5'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module RFb
6
+ @@path = "r_fb"#File.expand_path("../r_fb", __FILE__)
7
+ require "#{@@path}/exceptions"
8
+ require "#{@@path}/configurable"
9
+ require "#{@@path}/fql"
10
+ require "#{@@path}/methods"
11
+ require "#{@@path}/parser"
12
+ require "#{@@path}/session"
13
+ require "#{@@path}/user"
14
+
15
+ SERVER = "www.facebook.com"
16
+ API_PATH = "#{SERVER}/restserver.php"
17
+ LOGIN_PATH = "http://#{SERVER}/login.php"
18
+
19
+
20
+
21
+ include Exceptions
22
+ include Methods
23
+
24
+ class << self
25
+ include Configurable
26
+ end
27
+
28
+ # def self.api_url=(url)
29
+ # ENV["rfb_api_url"] = url
30
+ # end
31
+
32
+
33
+ def self.login_url(next_param = "")
34
+ next_param = "&next=#{next_param}" unless next_param.empty?
35
+ "#{LOGIN_PATH}?api_key=#{api_key}&v=1.0#{next_param}"
36
+ end
37
+
38
+ def self.get_session(auth_token)
39
+ response = request(method: Methods::AUTH_GET_SESSION, auth_token: auth_token)
40
+ end
41
+
42
+ def self.request(params)
43
+ response = make_request(params)
44
+ if debug
45
+ puts "RESPONSE"
46
+ puts response.body.inspect
47
+ end
48
+ parsed = Parser.parse(response)
49
+ end
50
+
51
+ def self.make_request(params)
52
+ params.stringify_keys!
53
+ params['api_key'] = api_key
54
+ params['call_id'] = Time.new.to_f.to_s
55
+ params['v'] = "1.0"
56
+ params['format'] = "json"
57
+
58
+ secure = false
59
+ api_url = URI.parse("#{(secure ? 'https' : 'http')}://#{API_PATH}")
60
+
61
+ # Generate signature and add it to the list of params
62
+ params_str = params.sort.map! { |p| "#{p[0]}=#{p[1]}" }.join
63
+ params['sig'] = Digest::MD5.hexdigest(params_str + ENV["rfb_secret"])
64
+
65
+ # Prepare POST request with params
66
+ req = Net::HTTP::Post.new(api_url.path)
67
+ req.set_form_data(params)
68
+
69
+ # Setup connection and make request. Use SSL if necessary.
70
+ connection = Net::HTTP.new(api_url.host, api_url.port)
71
+ connection.read_timeout = @http_read_timeout
72
+ connection.open_timeout = @http_open_timeout
73
+ if api_url.scheme == 'https'
74
+ connection.use_ssl = true
75
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
76
+ end
77
+ puts "calling #{params["method"]}" if debug
78
+ puts "request params: #{params.inspect}" if debug
79
+ request = connection.request(req)
80
+ end
81
+
82
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RFb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Francesco 'makevoid' Canessa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: RubyFacebook interacts with Facebook REST API via JSON. No XML to deal with, no parsers needed, reduced verbosity, increased happiness. Ruby1.9 required. Fork it, please!
17
+ email: makevoid@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - RFb.gemspec
27
+ - Rakefile
28
+ - VERSION
29
+ - init.rb
30
+ - lib/r_fb.rb
31
+ - lib/r_fb/configurable.rb
32
+ - lib/r_fb/cookie.rb
33
+ - lib/r_fb/exceptions.rb
34
+ - lib/r_fb/fql.rb
35
+ - lib/r_fb/methods.rb
36
+ - lib/r_fb/parser.rb
37
+ - lib/r_fb/session.rb
38
+ - lib/r_fb/user.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/makevoid/RFb
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: RubyFacebook - interact with Facebook REST API (json) - rails3 enabled
67
+ test_files: []
68
+