Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support fb questions, question options and votes

  • Loading branch information...
commit a3dbc93674612869c3faa5bb3ac30d8920b18e66 1 parent 6d88c66
@mahkhaled authored
View
2  lib/fb_graph.rb
@@ -96,6 +96,8 @@ def self.debug(&block)
require 'fb_graph/place'
require 'fb_graph/post'
require 'fb_graph/property'
+require 'fb_graph/question'
+require 'fb_graph/question_option'
require 'fb_graph/review'
require 'fb_graph/status'
require 'fb_graph/tab'
View
18 lib/fb_graph/connections/question_options.rb
@@ -0,0 +1,18 @@
+module FbGraph
+ module Connections
+ module QuestionOptions
+ def options(opts = {})
+ question_options = if @_options_ && opts.blank?
+ self.connection(:question_options, opts.merge(:cached_collection => @_options_))
+ else
+ self.connection(:question_options, opts)
+ end
+ question_options.map! do |option|
+ QuestionOption.new(option[:id], option.merge(
+ :access_token => opts[:access_token] || self.access_token
+ ))
+ end
+ end
+ end
+ end
+end
View
9 lib/fb_graph/connections/votes.rb
@@ -0,0 +1,9 @@
+module FbGraph
+ module Connections
+ module Votes
+ def users_votes(options = {})
@nov
nov added a note

Can I ask why you use "users_votes" for the method name?
In other places, fb_graph uses connection names as the method (with Rubyish underscores like friendlists -> friend_lists).

@nov
nov added a note

Ah, never mind. I found "votes" in QuestionOption class.

@nov
nov added a note

Can I change this method to votes and QuestionOptions's @votes to @vote_count?
That's the way I'm handling numeric likes (@like_count = attributes[:likes]) in FbGraph::Page / FbGraph::Comment.

@mahkhaled Owner

sure, I like that vote_count more than users_votes

@nov
nov added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.connection(:votes, options)
+ end
+ end
+ end
+end
View
26 lib/fb_graph/question.rb
@@ -0,0 +1,26 @@
+module FbGraph
+ class Question < Node
+ include Connections::QuestionOptions
+
+ attr_accessor :from, :question, :created_time, :updated_time
+
+ def initialize(identifier, attributes = {})
+ super
+ @from = if attributes[:from]
+ User.new(attributes[:from][:id], attributes[:from])
+ end
+ @question = attributes[:question]
+ @created_time = if attributes[:created_time]
+ Time.parse(attributes[:created_time]).utc
+ end
+ @updated_time = if attributes[:updated_time]
+ Time.parse(attributes[:updated_time]).utc
+ end
+
+ @_options_ = if attributes[:options]
+ # cached options
+ Collection.new(attributes[:options])
+ end
+ end
+ end
+end
View
22 lib/fb_graph/question_option.rb
@@ -0,0 +1,22 @@
+module FbGraph
+ class QuestionOption < Node
+ include Connections::Votes
+
+ attr_accessor :from, :name, :votes, :object, :created_time
+
+ def initialize(identifier, attributes = {})
+ super
+ @from = if attributes[:from]
+ User.new(attributes[:from][:id], attributes[:from])
+ end
+ @name = attributes[:name]
+ @votes = attributes[:votes]
+ @object = if attributes[:object]
+ Page.new(attributes[:object][:id], attributes[:object])
+ end
+ @created_time = if attributes[:created_time]
+ Time.parse(attributes[:created_time]).utc
+ end
+ end
+ end
+end
View
14 spec/fb_graph/connections/question_options_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe FbGraph::Connections::QuestionOptions, '#options' do
+ context 'when included by FbGraph::Question' do
+ it 'should return options as FbGraph::QuestionOption' do
+ mock_graph :get, '12345/question_options', 'questions/options/matake_private', :access_token => 'access_token' do
+ options = FbGraph::Question.new('12345', :access_token => 'access_token').options
+ options.each do |option|
+ option.should be_instance_of(FbGraph::QuestionOption)
+ end
+ end
+ end
+ end
+end
View
15 spec/fb_graph/connections/votes_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe FbGraph::Connections::Votes, '#users_votes' do
+ context 'when included by FbGraph::QuestionOption' do
+ it 'should return votes with user id and name' do
+ mock_graph :get, '12345/votes', 'questions/options/votes/matake_private', :access_token => 'access_token' do
+ votes = FbGraph::QuestionOption.new('12345', :access_token => 'access_token').users_votes
+ votes.each do |vote|
+ vote["id"].should_not be_blank
+ vote["name"].should_not be_blank
+ end
+ end
+ end
+ end
+end
View
35 spec/fb_graph/qeustion_option_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe FbGraph::QuestionOption do
+
+ describe '.new' do
+ it 'should setup all supported attributes' do
+ attributes = {
+ :id => '12345',
+ :from => {
+ :id => '23456',
+ :name => 'Mahmoud Khaled'
+ },
+ :name => 'option 1',
+ :votes => 5,
+ :object => {:name => "Ruby programming language",
+ :category => "Interest",
+ :id => "109932262369283"
+ },
+ :created_time => '2009-12-29T15:24:50+0000'
+ }
+ question = FbGraph::QuestionOption.new(attributes.delete(:id), attributes)
+ question.identifier.should == '12345'
+ question.from.should == FbGraph::User.new('23456', :name => 'Mahmoud Khaled')
+ question.name.should == 'option 1'
+ question.votes.should == 5
+ question.object.should be_instance_of(FbGraph::Page)
+ question.object.name.should == "Ruby programming language"
+ question.object.category.should == "Interest"
+ question.object.identifier.should == "109932262369283"
+ question.created_time.should == Time.parse('2009-12-29T15:24:50+0000')
+ end
+
+ end
+
+end
View
72 spec/fb_graph/question_spec.rb
@@ -0,0 +1,72 @@
+require 'spec_helper'
+
+describe FbGraph::Question do
+
+ describe '.new' do
+ it 'should setup all supported attributes' do
+ attributes = {
+ :id => '12345',
+ :from => {
+ :id => '23456',
+ :name => 'Mahmoud Khaled'
+ },
+ :question => 'question 1',
+ :created_time => '2009-12-29T15:24:50+0000',
+ :updated_time => '2010-01-02T15:37:41+0000',
+ :options => {
+ :data => [
+ {
+ :id => "34567",
+ :from => {
+ :name => "Mahmoud Khaled",
+ :id => "23456"
+ },
+ :name => "option 1",
+ :votes => 2,
+ :created_time => "2011-11-07T19:49:51+0000"
+ },
+ {
+ :id => "34568",
+ :from => {
+ :name => "Mustafa Badawy",
+ :id => "23457"
+ },
+ :name => "option 2",
+ :votes => 0,
+ :created_time => "2011-11-07T19:49:48+0000"
+ }
+ ]
+ }
+ }
+ question = FbGraph::Question.new(attributes.delete(:id), attributes)
+ question.identifier.should == '12345'
+ question.from.should == FbGraph::User.new('23456', :name => 'Mahmoud Khaled')
+ question.question.should == 'question 1'
+ question.created_time.should == Time.parse('2009-12-29T15:24:50+0000')
+ question.updated_time.should == Time.parse('2010-01-02T15:37:41+0000')
+ question.options.should == [FbGraph::QuestionOption.new(
+ '34567',
+ :from => {
+ :id => '23456',
+ :name => 'Mahmoud Khaled',
+ },
+ :name => "option 1",
+ :votes => 2,
+ :created_time => "2011-11-07T19:49:51+0000"
+ ),
+ FbGraph::QuestionOption.new(
+ '34568',
+ :from => {
+ :id => '23457',
+ :name => 'Mustafa Badawy',
+ },
+ :name => "option 2",
+ :votes => 0,
+ :created_time => "2011-11-07T19:49:48+0000"
+ )
+ ]
+ end
+
+ end
+
+end
View
52 spec/mock_json/questions/options/matake_private.json
@@ -0,0 +1,52 @@
+{
+ "data": [
+ {
+ "id": "240094212711030",
+ "from": {
+ "name": "Mahmoud Khaled",
+ "id": "7168"
+ },
+ "name": "C++ programming language",
+ "votes": 0,
+ "object": {
+ "name": "C++ programming language",
+ "category": "Interest",
+ "id": "1065"
+ },
+ "created_time": "2011-11-07T21:03:06+0000"
+ },
+ {
+ "id": "1737",
+ "from": {
+ "name": "Mahmoud Khaled",
+ "id": "7168"
+ },
+ "name": "Ruby programming language",
+ "votes": 5,
+ "object": {
+ "name": "Ruby programming language",
+ "category": "Interest",
+ "id": "1099"
+ },
+ "created_time": "2011-11-07T21:03:07+0000"
+ },
+ {
+ "id": "3163",
+ "from": {
+ "name": "Mahmoud Khaled",
+ "id": "7168"
+ },
+ "name": "Java Programming Language.",
+ "votes": 0,
+ "object": {
+ "name": "Java Programming Language.",
+ "category": "Computers/internet",
+ "id": "3010"
+ },
+ "created_time": "2011-11-07T21:03:08+0000"
+ }
+ ],
+ "paging": {
+ "next": "https://graph.facebook.com/12345/options?format=json&limit=25&offset=25"
+ }
+}
View
15 spec/mock_json/questions/options/votes/matake_private.json
@@ -0,0 +1,15 @@
+{
+ "data": [
+ {
+ "name": "Mahmoud Khaled",
+ "id": "5579"
+ },
+ {
+ "name": "Mahmoud Khaled",
+ "id": "7168"
+ }
+ ],
+ "paging": {
+ "next": "https://graph.facebook.com/12345/votes?format=json&limit=5000&offset=5000"
+ }
+}
@nov

Can I ask why you use "users_votes" for the method name?
In other places, fb_graph uses connection names as the method (with Rubyish underscores like friendlists -> friend_lists).

@nov

Ah, never mind. I found "votes" in QuestionOption class.

@nov

Can I change this method to votes and QuestionOptions's @votes to @vote_count?
That's the way I'm handling numeric likes (@like_count = attributes[:likes]) in FbGraph::Page / FbGraph::Comment.

@mahkhaled

sure, I like that vote_count more than users_votes

@nov
Please sign in to comment.
Something went wrong with that request. Please try again.