faceb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.document +5 -0
  2. data/.gitignore +24 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE +20 -0
  5. data/README.md +70 -0
  6. data/Rakefile +76 -0
  7. data/VERSION +1 -0
  8. data/bin/console +3 -0
  9. data/faceb.gemspec +164 -0
  10. data/features/faceb-session.feature +7 -0
  11. data/features/step_definitions/facebook-session_steps.rb +14 -0
  12. data/features/support/env.rb +4 -0
  13. data/lib/faceb.rb +7 -0
  14. data/lib/faceb/api.rb +69 -0
  15. data/lib/faceb/base.rb +51 -0
  16. data/lib/faceb/rails.rb +12 -0
  17. data/lib/faceb/rails/api.rb +21 -0
  18. data/lib/faceb/rails/base.rb +30 -0
  19. data/lib/faceb/rails/configuration.rb +31 -0
  20. data/lib/faceb/rails/controller.rb +49 -0
  21. data/lib/faceb/rails/generators.rb +3 -0
  22. data/lib/faceb/rails/generators/config/USAGE +8 -0
  23. data/lib/faceb/rails/generators/config/config_generator.rb +16 -0
  24. data/lib/faceb/rails/generators/config/templates/faceb.yml +6 -0
  25. data/lib/faceb/rails/generators/fb_connect/USAGE +7 -0
  26. data/lib/faceb/rails/generators/fb_connect/fb_connect_generator.rb +16 -0
  27. data/lib/faceb/rails/generators/fb_connect/templates/xd_receiver.html +10 -0
  28. data/lib/faceb/rails/generators/fb_connect/templates/xd_receiver_ssl.html +10 -0
  29. data/lib/faceb/rails/rails_23_init.rb +21 -0
  30. data/lib/faceb/rails/railtie.rb +39 -0
  31. data/lib/faceb/rails/tasks.rb +2 -0
  32. data/lib/faceb/rails/tasks/faceb.tasks +6 -0
  33. data/lib/faceb/rails/tasks/tunnel.tasks +46 -0
  34. data/lib/faceb/session.rb +38 -0
  35. data/spec/faceb_api_spec.rb +107 -0
  36. data/spec/faceb_base_spec.rb +48 -0
  37. data/spec/faceb_session_spec.rb +64 -0
  38. data/spec/rails/config_generator_spec.rb +39 -0
  39. data/spec/rails/configuration_sepc.rb +11 -0
  40. data/spec/rails/controller_spec.rb +39 -0
  41. data/spec/rails/fb_connect_generator_spec.rb +28 -0
  42. data/spec/rails/railtie_spec.rb +23 -0
  43. data/spec/rails_app/app/controllers/application_controller.rb +3 -0
  44. data/spec/rails_app/app/controllers/posts_controller.rb +7 -0
  45. data/spec/rails_app/app/helpers/application_helper.rb +2 -0
  46. data/spec/rails_app/config.ru +4 -0
  47. data/spec/rails_app/config/application.rb +46 -0
  48. data/spec/rails_app/config/boot.rb +9 -0
  49. data/spec/rails_app/config/database.yml +22 -0
  50. data/spec/rails_app/config/environment.rb +5 -0
  51. data/spec/rails_app/config/environments/development.rb +19 -0
  52. data/spec/rails_app/config/environments/production.rb +33 -0
  53. data/spec/rails_app/config/environments/test.rb +32 -0
  54. data/spec/rails_app/config/faceb.yml +3 -0
  55. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  56. data/spec/rails_app/config/initializers/cookie_verification_secret.rb +7 -0
  57. data/spec/rails_app/config/initializers/inflections.rb +10 -0
  58. data/spec/rails_app/config/initializers/mime_types.rb +5 -0
  59. data/spec/rails_app/config/initializers/session_store.rb +10 -0
  60. data/spec/rails_app/config/locales/en.yml +5 -0
  61. data/spec/rails_app/config/routes.rb +58 -0
  62. data/spec/rails_app/log/development.log +0 -0
  63. data/spec/rails_app/log/production.log +0 -0
  64. data/spec/rails_app/log/server.log +0 -0
  65. data/spec/rails_app/log/test.log +0 -0
  66. data/spec/rails_app/public/404.html +26 -0
  67. data/spec/rails_app/public/422.html +26 -0
  68. data/spec/rails_app/public/500.html +26 -0
  69. data/spec/rails_app/public/favicon.ico +0 -0
  70. data/spec/rails_app/public/javascripts/application.js +2 -0
  71. data/spec/rails_app/public/javascripts/controls.js +965 -0
  72. data/spec/rails_app/public/javascripts/dragdrop.js +974 -0
  73. data/spec/rails_app/public/javascripts/effects.js +1123 -0
  74. data/spec/rails_app/public/javascripts/prototype.js +4874 -0
  75. data/spec/rails_app/public/javascripts/rails.js +110 -0
  76. data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
  77. data/spec/rails_app/script/rails +9 -0
  78. data/spec/rails_spec_helper.rb +6 -0
  79. data/spec/spec.opts +1 -0
  80. data/spec/spec_helper.rb +27 -0
  81. metadata +247 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .yardoc
23
+ doc
24
+ spec/tmp
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://gemcutter.org"
2
+
3
+ # Need to install Rails from source
4
+ gem "rails", :git => "git://github.com/rails/rails.git"
5
+ gem "httparty", "0.5.2"
6
+ gem "rack-facebook", "0.0.3"
7
+
8
+ group :test do
9
+ gem "rspec", ">= 1.2.9"
10
+ gem "cucumber", ">= 0"
11
+ gem "fakeweb", ">= 1.2.8"
12
+ gem "rr", ">= 0.10.10"
13
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mathieu Fosse
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # FaceB
2
+
3
+ FaceB is a Facebook Library which aims to be more modular than Facebooker. You can use it only for the Facebook API client or if you use Rails you can have a lot of stuff helping you to develop your Facebook application (controller, helper methods).
4
+
5
+ ## Installation
6
+
7
+ $ [sudo] gem install faceb
8
+
9
+ ## Getting started
10
+
11
+ ### Facebook API client
12
+
13
+ FaceB is modular so you can use it just as a tool for interact with Facebook API.
14
+ You are free to use this syntax :
15
+
16
+ require 'faceb'
17
+ FaceB.call('api-key', 'secret-key', 'method.name', :param1 => 'value1')
18
+
19
+ Or if you call more than once API methods, you can create a persistent session like that :
20
+
21
+ require 'faceb'
22
+ FaceB.new('api-key', 'secret-key')
23
+ FaceB.current_session.call('method.name1', :param1 => 'value1')
24
+ FaceB.current_session.call('method.name2', :param1 => 'value1')
25
+
26
+ ### Rails integration
27
+
28
+ For now FaceB works only with Rails 3. A version compatible with Rails 2.3.x is in progress.
29
+
30
+ First thing to do is to add FaceB as dependency of your new project, add this line to your `Gemfile` :
31
+
32
+ gem 'faceb'
33
+
34
+ Then call the initialization script like this :
35
+
36
+ $ ./script/rails generate face_b:config
37
+
38
+ This generator will create a new file named `faceb.yml` in your `config` directory.
39
+ Edit this new file with your Facebook application properties.
40
+
41
+
42
+ ## Rails 2.3.x
43
+
44
+ A version compatible with Rails 2.3.x is in progress.
45
+
46
+
47
+ ## TODO
48
+
49
+ - Add mutli Facebook application settings (alternative api key)
50
+ - Add view helpers
51
+ - Add Facebook connect helpers
52
+ - Add Rails 2.3.x compatibility
53
+
54
+ ## Contributors
55
+
56
+ - Mathieu Fosse
57
+
58
+ ## Note on Patches/Pull Requests
59
+
60
+ * Fork the project.
61
+ * Make your feature addition or bug fix.
62
+ * Add tests for it. This is important so I don't break it in a
63
+ future version unintentionally.
64
+ * Commit, do not mess with rakefile, version, or history.
65
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
66
+ * Send me a pull request. Bonus points for topic branches.
67
+
68
+ ## Copyright
69
+
70
+ Copyright (c) 2010 Mathieu Fosse. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,76 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'yard'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "faceb"
9
+ gem.summary = %Q{Facebook library}
10
+ gem.description = %Q{Facebook library}
11
+ gem.email = "mathieu@tigerlilyapps.com"
12
+ gem.homepage = "http://github.com/tigerlily/faceb"
13
+ gem.authors = ["Mathieu Fosse"]
14
+ gem.add_dependency "httparty", ">= 0.5.2"
15
+ gem.add_dependency "rack-facebook", ">= 0.0.3"
16
+
17
+ # Tests
18
+ gem.add_development_dependency "rspec", ">= 1.2.9"
19
+ gem.add_development_dependency "cucumber", ">= 0"
20
+ gem.add_development_dependency "fakeweb", ">= 1.2.8"
21
+ gem.add_development_dependency "rr", ">= 0.10.10"
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+ require 'spec/rake/spectask'
29
+ Spec::Rake::SpecTask.new(:spec) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.spec_files = FileList['spec/*_spec.rb']
32
+ end
33
+
34
+ Spec::Rake::SpecTask.new(:spec_rails) do |spec|
35
+ ruby_path = `which ruby`.chomp
36
+ spec.ruby_cmd = "FACEB_TEST=rails #{ruby_path}"
37
+ spec.libs << 'lib' << 'spec'
38
+ spec.spec_files = FileList['spec/rails/*_spec.rb']
39
+ end
40
+
41
+ desc 'Run all FaceB tests.'
42
+ task :spec_all => [:spec, :spec_rails]
43
+
44
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
45
+ spec.libs << 'lib' << 'spec'
46
+ spec.pattern = 'spec/**/*_spec.rb'
47
+ spec.rcov = true
48
+ end
49
+
50
+ task :spec => :check_dependencies
51
+
52
+ begin
53
+ require 'cucumber/rake/task'
54
+ Cucumber::Rake::Task.new(:features)
55
+
56
+ task :features => :check_dependencies
57
+ rescue LoadError
58
+ task :features do
59
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
60
+ end
61
+ end
62
+
63
+ task :default => :spec
64
+
65
+ require 'rake/rdoctask'
66
+ Rake::RDocTask.new do |rdoc|
67
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
68
+
69
+ rdoc.rdoc_dir = 'rdoc'
70
+ rdoc.title = "FaceB #{version}"
71
+ rdoc.rdoc_files.include('README*')
72
+ rdoc.rdoc_files.include('lib/**/*.rb')
73
+ end
74
+
75
+ # Yard
76
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/console ADDED
@@ -0,0 +1,3 @@
1
+ #!/opt/local/bin/ruby
2
+ require 'rubygems'
3
+ require 'irb'
data/faceb.gemspec ADDED
@@ -0,0 +1,164 @@
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{faceb}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mathieu Fosse"]
12
+ s.date = %q{2010-04-05}
13
+ s.default_executable = %q{console}
14
+ s.description = %q{Facebook library}
15
+ s.email = %q{mathieu@tigerlilyapps.com}
16
+ s.executables = ["console"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "Gemfile",
25
+ "LICENSE",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/console",
30
+ "faceb.gemspec",
31
+ "features/faceb-session.feature",
32
+ "features/step_definitions/facebook-session_steps.rb",
33
+ "features/support/env.rb",
34
+ "lib/faceb.rb",
35
+ "lib/faceb/api.rb",
36
+ "lib/faceb/base.rb",
37
+ "lib/faceb/rails.rb",
38
+ "lib/faceb/rails/api.rb",
39
+ "lib/faceb/rails/base.rb",
40
+ "lib/faceb/rails/configuration.rb",
41
+ "lib/faceb/rails/controller.rb",
42
+ "lib/faceb/rails/generators.rb",
43
+ "lib/faceb/rails/generators/config/USAGE",
44
+ "lib/faceb/rails/generators/config/config_generator.rb",
45
+ "lib/faceb/rails/generators/config/templates/faceb.yml",
46
+ "lib/faceb/rails/generators/fb_connect/USAGE",
47
+ "lib/faceb/rails/generators/fb_connect/fb_connect_generator.rb",
48
+ "lib/faceb/rails/generators/fb_connect/templates/xd_receiver.html",
49
+ "lib/faceb/rails/generators/fb_connect/templates/xd_receiver_ssl.html",
50
+ "lib/faceb/rails/rails_23_init.rb",
51
+ "lib/faceb/rails/railtie.rb",
52
+ "lib/faceb/rails/tasks.rb",
53
+ "lib/faceb/rails/tasks/faceb.tasks",
54
+ "lib/faceb/rails/tasks/tunnel.tasks",
55
+ "lib/faceb/session.rb",
56
+ "spec/faceb_api_spec.rb",
57
+ "spec/faceb_base_spec.rb",
58
+ "spec/faceb_session_spec.rb",
59
+ "spec/rails/config_generator_spec.rb",
60
+ "spec/rails/configuration_sepc.rb",
61
+ "spec/rails/controller_spec.rb",
62
+ "spec/rails/fb_connect_generator_spec.rb",
63
+ "spec/rails/railtie_spec.rb",
64
+ "spec/rails_app/app/controllers/application_controller.rb",
65
+ "spec/rails_app/app/controllers/posts_controller.rb",
66
+ "spec/rails_app/app/helpers/application_helper.rb",
67
+ "spec/rails_app/config.ru",
68
+ "spec/rails_app/config/application.rb",
69
+ "spec/rails_app/config/boot.rb",
70
+ "spec/rails_app/config/database.yml",
71
+ "spec/rails_app/config/environment.rb",
72
+ "spec/rails_app/config/environments/development.rb",
73
+ "spec/rails_app/config/environments/production.rb",
74
+ "spec/rails_app/config/environments/test.rb",
75
+ "spec/rails_app/config/faceb.yml",
76
+ "spec/rails_app/config/initializers/backtrace_silencers.rb",
77
+ "spec/rails_app/config/initializers/cookie_verification_secret.rb",
78
+ "spec/rails_app/config/initializers/inflections.rb",
79
+ "spec/rails_app/config/initializers/mime_types.rb",
80
+ "spec/rails_app/config/initializers/session_store.rb",
81
+ "spec/rails_app/config/locales/en.yml",
82
+ "spec/rails_app/config/routes.rb",
83
+ "spec/rails_app/log/development.log",
84
+ "spec/rails_app/log/production.log",
85
+ "spec/rails_app/log/server.log",
86
+ "spec/rails_app/log/test.log",
87
+ "spec/rails_app/public/404.html",
88
+ "spec/rails_app/public/422.html",
89
+ "spec/rails_app/public/500.html",
90
+ "spec/rails_app/public/favicon.ico",
91
+ "spec/rails_app/public/javascripts/application.js",
92
+ "spec/rails_app/public/javascripts/controls.js",
93
+ "spec/rails_app/public/javascripts/dragdrop.js",
94
+ "spec/rails_app/public/javascripts/effects.js",
95
+ "spec/rails_app/public/javascripts/prototype.js",
96
+ "spec/rails_app/public/javascripts/rails.js",
97
+ "spec/rails_app/public/stylesheets/.gitkeep",
98
+ "spec/rails_app/script/rails",
99
+ "spec/rails_spec_helper.rb",
100
+ "spec/spec.opts",
101
+ "spec/spec_helper.rb"
102
+ ]
103
+ s.homepage = %q{http://github.com/tigerlily/faceb}
104
+ s.rdoc_options = ["--charset=UTF-8"]
105
+ s.require_paths = ["lib"]
106
+ s.rubygems_version = %q{1.3.6}
107
+ s.summary = %q{Facebook library}
108
+ s.test_files = [
109
+ "spec/faceb_api_spec.rb",
110
+ "spec/faceb_base_spec.rb",
111
+ "spec/faceb_session_spec.rb",
112
+ "spec/rails/config_generator_spec.rb",
113
+ "spec/rails/configuration_sepc.rb",
114
+ "spec/rails/controller_spec.rb",
115
+ "spec/rails/fb_connect_generator_spec.rb",
116
+ "spec/rails/railtie_spec.rb",
117
+ "spec/rails_app/app/controllers/application_controller.rb",
118
+ "spec/rails_app/app/controllers/posts_controller.rb",
119
+ "spec/rails_app/app/helpers/application_helper.rb",
120
+ "spec/rails_app/config/application.rb",
121
+ "spec/rails_app/config/boot.rb",
122
+ "spec/rails_app/config/environment.rb",
123
+ "spec/rails_app/config/environments/development.rb",
124
+ "spec/rails_app/config/environments/production.rb",
125
+ "spec/rails_app/config/environments/test.rb",
126
+ "spec/rails_app/config/initializers/backtrace_silencers.rb",
127
+ "spec/rails_app/config/initializers/cookie_verification_secret.rb",
128
+ "spec/rails_app/config/initializers/inflections.rb",
129
+ "spec/rails_app/config/initializers/mime_types.rb",
130
+ "spec/rails_app/config/initializers/session_store.rb",
131
+ "spec/rails_app/config/routes.rb",
132
+ "spec/rails_spec_helper.rb",
133
+ "spec/spec_helper.rb"
134
+ ]
135
+
136
+ if s.respond_to? :specification_version then
137
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
138
+ s.specification_version = 3
139
+
140
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
141
+ s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
142
+ s.add_runtime_dependency(%q<rack-facebook>, [">= 0.0.3"])
143
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
144
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
145
+ s.add_development_dependency(%q<fakeweb>, [">= 1.2.8"])
146
+ s.add_development_dependency(%q<rr>, [">= 0.10.10"])
147
+ else
148
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
149
+ s.add_dependency(%q<rack-facebook>, [">= 0.0.3"])
150
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
151
+ s.add_dependency(%q<cucumber>, [">= 0"])
152
+ s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
153
+ s.add_dependency(%q<rr>, [">= 0.10.10"])
154
+ end
155
+ else
156
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
157
+ s.add_dependency(%q<rack-facebook>, [">= 0.0.3"])
158
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
159
+ s.add_dependency(%q<cucumber>, [">= 0"])
160
+ s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
161
+ s.add_dependency(%q<rr>, [">= 0.10.10"])
162
+ end
163
+ end
164
+
@@ -0,0 +1,7 @@
1
+ Feature: Facebook session managment
2
+ In order to use a Facebook session
3
+
4
+ Scenario: Retrieve Facebook session
5
+ Given I have facebook instance configured
6
+ When I ask for the current Facebook session
7
+ Then I should retrieve the current Facebook session
@@ -0,0 +1,14 @@
1
+ Given /^I have facebook instance configured$/ do
2
+ FaceB.new({
3
+ :api_key => 'api-key',
4
+ :secret_key => 'secret-key'
5
+ })
6
+ end
7
+
8
+ When /^I ask for the current Facebook session$/ do
9
+ FaceB.current_session
10
+ end
11
+
12
+ Then /^I should retrieve the current Facebook session$/ do
13
+ pending # express the regexp above with the code you wish you had
14
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'faceb'
3
+
4
+ require 'spec/expectations'
data/lib/faceb.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'faceb/base'
2
+ require 'faceb/session'
3
+ require 'faceb/api'
4
+
5
+ # Rails
6
+ require 'faceb/rails' if defined?(Rails)
7
+
data/lib/faceb/api.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'httparty'
2
+ require 'digest/md5'
3
+
4
+ module FaceB
5
+ # Api
6
+ class Api
7
+ include HTTParty
8
+ default_params :format => 'JSON', :v => '1.0'
9
+ API_SERVER_URL = "http://api.facebook.com/restserver.php"
10
+
11
+ def initialize(session)
12
+ @session = session
13
+ end
14
+
15
+ ##
16
+ # Call a Facebook API method
17
+ #
18
+ # @param [String] method The API method to call
19
+ # @param [Hash] params Params to pass to API method call
20
+ # @option params [Boolean, String] :session_key if true use the current Facebook session key else if string use this string as session key
21
+ #
22
+ # @return [FaceB::Api::Response] The API method response
23
+ def call(method, params = {})
24
+ params[:call_id] = Time.now.tv_sec.to_s if params.delete(:call_id)
25
+ params[:session_key] = @session.session_key if params[:session_key] == true && !!@session.session_key
26
+
27
+ p = Api.default_params.merge(:method => method, :api_key => @session.api_key)
28
+ p.merge!(params)
29
+
30
+ Response.new(Api.post(API_SERVER_URL, :body => generate_signature(p, @session.secret_key)))
31
+ end
32
+
33
+ private
34
+ def generate_signature(params, secret)
35
+ p = params.map { |name, value| [name, value].join('=') }.sort.join
36
+ params.merge(:sig => Digest::MD5.hexdigest([p, secret].join))
37
+ end
38
+
39
+ # Response
40
+ class Response
41
+ attr_reader :data
42
+
43
+ def initialize(data)
44
+ @data = data
45
+ raise Error.new(data["error_code"], data["error_msg"]) if data.respond_to?(:include?) && data.include?("error_msg")
46
+ end
47
+
48
+ def method_missing(name, *args)
49
+ if @data.is_a?(Hash)
50
+ @data[name.to_s]
51
+ elsif @data.is_a?(Array) && @data.size == 1 && @data.first.is_a?(Hash)
52
+ @data.first[name.to_s]
53
+ end
54
+ end
55
+
56
+ def error
57
+ {:code => @data['error_code'], :msg => @data['error_msg'] }
58
+ end
59
+ end
60
+
61
+ # Api Error
62
+ class Error < StandardError
63
+ def initialize(error_code, error_msg)
64
+ super("Facebook error #{error_code} : '#{error_msg}'")
65
+ end
66
+ end
67
+
68
+ end
69
+ end