Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rohit refactored stuff to be more like widget, some hardcoding left t…

…o be removed
  • Loading branch information...
commit f65abb099bcc8120029ae63f75d8bdce3527a642 1 parent ecef489
Rohit Garg rohitggarg authored

Showing 18 changed files with 116 additions and 99 deletions. Show diff stats Hide diff stats

  1. +3 3 app/assets/javascripts/facebook_share_widget/facebook_friend.js.coffee
  2. +6 3 app/assets/javascripts/facebook_share_widget/facebook_share.js.coffee.erb
  3. +16 9 app/controllers/facebook_share_widget/facebook_controller.rb
  4. +0 9 app/views/facebook_share_widget/facebook/_employers_list.html.haml
  5. +9 0 app/views/facebook_share_widget/facebook/_personal_data_list.html.haml
  6. +0 5 app/views/facebook_share_widget/facebook/change_employer.html.haml
  7. +0 6 app/views/facebook_share_widget/facebook/employers.html.haml
  8. +4 0 app/views/facebook_share_widget/facebook/friends_personal_data.html.haml
  9. +2 1  app/views/facebook_share_widget/facebook/index.html.haml
  10. +0 5 app/views/facebook_share_widget/facebook/no_employers.html.haml
  11. +0 4 app/views/facebook_share_widget/facebook/no_friends_employers.html.haml
  12. +4 0 app/views/facebook_share_widget/facebook/no_friends_personal_data.html.haml
  13. +5 0 app/views/facebook_share_widget/facebook/no_personal_data.html.haml
  14. +5 0 app/views/facebook_share_widget/facebook/personal_data.html.haml
  15. +2 2 config/routes.rb
  16. +30 19 lib/facebook_share_widget/facebook_helper.rb
  17. +20 20 spec/controllers/facebook_controller_spec.rb
  18. +10 13 spec/lib/facebook_helpers_spec.rb
6 app/assets/javascripts/facebook_share_widget/facebook_friend.js.coffee
@@ -35,11 +35,11 @@ $ ->
35 35 data = $.extend({}, $.parseJSON(template))
36 36 data.facebook_id = @id
37 37 redirect_uri = window.location.protocol + '//' + window.location.host + '/widget/facebook/share-redirect?facebook_id='+data.facebook_id+'&link='+data.link
38   -
39   - window.open @url.replace('{app_id}', data.appId).replace('{to}', data.facebook_id).replace('{link}', encodeURIComponent(data.link)).replace('{redirect_uri}',encodeURIComponent(redirect_uri)), 'sharer', 'toolbar=0,status=0,width=1000,height=600'
40   -
  38 +
  39 + FB.ui { method: 'feed', link: data.link, redirect_uri: redirect_uri, to: data.facebook_id }
41 40 friend.shared()
42 41 friend.success_callback(friend)
  42 + return
43 43
44 44 setMessageModel: (model) ->
45 45 @messageModel = model
9 app/assets/javascripts/facebook_share_widget/facebook_share.js.coffee.erb
@@ -52,12 +52,15 @@ $ ->
52 52 self = this
53 53 json_message = $.parseJSON($('.template').text())
54 54 params = { link: json_message.link }
55   - if json_message.compId
56   - params['compId'] = json_message.compId
  55 + if json_message.personal_dataId
  56 + params['personal_dataId'] = json_message.personal_dataId
  57 + if json_message.personal_data_type
  58 + params['personal_data_type'] = json_message.personal_data_type
57 59 @collection.fetch({ data: $.param(params) }).complete ->
  60 + FB.init({appId: json_message.appId, cookie: true })
58 61 self.render()
59 62
60   - render: =>
  63 + render: () =>
61 64 this.$el.html(HandlebarsTemplates['facebook_share_widget/templates/all_friends']())
62 65 filteredFriends = @collection.filterBy(@nameSearch.searchFilter())
63 66 @appendFriend friend for friend in filteredFriends
25 app/controllers/facebook_share_widget/facebook_controller.rb
... ... @@ -1,25 +1,27 @@
1 1 class FacebookShareWidget::FacebookController < FacebookShareWidget::ApplicationController
2 2 include FacebookShareWidget::FacebookHelper
3 3
  4 + before_filter :sanitize_params, :except => [:share]
  5 +
4 6 def index
5 7 end
6 8
7   - def employers
  9 + def personal_data
8 10 begin
9   - @employers = my_employers
10   - if @employers.empty?
11   - render :no_employers
  11 + @personal_data_results = my_personal_data @personal_data_type
  12 + if @personal_data_results.empty?
  13 + render :no_personal_data
12 14 end
13 15 rescue Exception => ex
14 16 render json: { message: "You are probably not logged in" }, status: :not_found
15 17 end
16 18 end
17 19
18   - def change_employer
  20 + def friends_personal_data
19 21 begin
20   - @employers = friends_employers
21   - if @employers.empty?
22   - render :no_friends_employers
  22 + @personal_data_results = fb_friends_personal_data @personal_data_type
  23 + if @personal_data_results.empty?
  24 + render :no_friends_personal_data
23 25 end
24 26 rescue Exception => ex
25 27 render json: { message: "You are probably not logged in" }, status: :not_found
@@ -28,7 +30,7 @@ def change_employer
28 30
29 31 def friends
30 32 begin
31   - render json: facebook_friends_for_link(params[:link], params[:compId] ? params[:compId].to_i : nil), status: :ok
  33 + render json: facebook_friends_for_link(params[:link], @personal_dataId, @personal_data_type), status: :ok
32 34 rescue Exception => ex
33 35 log_exception_and_render_as_json(ex)
34 36 end
@@ -48,6 +50,11 @@ def share
48 50 end
49 51
50 52 private
  53 +
  54 + def sanitize_params
  55 + @personal_dataId = params[:personal_dataId] ? params[:personal_dataId].to_i : nil
  56 + @personal_data_type = params[:personal_data_type].present? ? params[:personal_data_type].gsub('$','.') : nil
  57 + end
51 58
52 59 def log_exception_and_render_as_json(ex)
53 60 Rails.logger.warn ex.message
9 app/views/facebook_share_widget/facebook/_employers_list.html.haml
... ... @@ -1,9 +0,0 @@
1   -%table
2   - - @employers.each do |employer|
3   - - employer_id = friends_employers_list ? employer["id"] : employer.identifier
4   - - employer_name = friends_employers_list ? employer["name"] : employer.name
5   - %tr
6   - %td{width: '25px'}
7   - = radio_button_tag 'compId', employer_id
8   - %td
9   - = label_tag "compId_#{employer_id}", employer_name
9 app/views/facebook_share_widget/facebook/_personal_data_list.html.haml
... ... @@ -0,0 +1,9 @@
  1 += hidden_field_tag 'personal_data_type', params[:personal_data_type]
  2 +%table
  3 + - @personal_data_results.each do |personal_data|
  4 + - personal_data_id = personal_data["id"]
  5 + %tr
  6 + %td{width: '25px'}
  7 + = radio_button_tag 'personal_dataId', personal_data_id
  8 + %td
  9 + = label_tag "personal_dataId_#{personal_data_id}", personal_data["name"]
5 app/views/facebook_share_widget/facebook/change_employer.html.haml
... ... @@ -1,5 +0,0 @@
1   -= render partial: 'employers_list', locals: { friends_employers_list: true }
2   -:coffeescript
3   - $ ->
4   - window.Cs.submit_employer();
5   - window.Cs.no_employers();
6 app/views/facebook_share_widget/facebook/employers.html.haml
... ... @@ -1,6 +0,0 @@
1   -.employers
2   - = render partial: 'employers_list', locals: { friends_employers_list: false }
3   -:coffeescript
4   - $ ->
5   - window.Cs.submit_employer();
6   - window.Cs.employers();
4 app/views/facebook_share_widget/facebook/friends_personal_data.html.haml
... ... @@ -0,0 +1,4 @@
  1 += render partial: 'personal_data_list'
  2 +:coffeescript
  3 + $ ->
  4 + window.Cs.fb_share_widget.onload_friends_data()
3  app/views/facebook_share_widget/facebook/index.html.haml
... ... @@ -1,2 +1,3 @@
1 1 = javascript_include_tag 'facebook_share_widget/facebook_friend_view'
2   -= facebook_share_widget_helper template: {link: params[:link], name: params[:name], compId: params[:compId], appId: params[:appId] }
  2 += javascript_include_tag "http://connect.facebook.net/#{I18n.locale}/all.js"
  3 += facebook_share_widget_helper template: {link: params[:link], name: params[:name], personal_dataId: params[:personal_dataId], appId: params[:appId], personal_data_type: params[:personal_data_type]}
5 app/views/facebook_share_widget/facebook/no_employers.html.haml
... ... @@ -1,5 +0,0 @@
1   -.employers
2   - Oops! Looks like you have no employers listed in your Facebook profile
3   -:coffeescript
4   - $ ->
5   - window.Cs.employers();
4 app/views/facebook_share_widget/facebook/no_friends_employers.html.haml
... ... @@ -1,4 +0,0 @@
1   -Oops! Looks like your friends have not listed any employers on their Facebook profile
2   -:coffeescript
3   - $ ->
4   - window.Cs.no_employers();
4 app/views/facebook_share_widget/facebook/no_friends_personal_data.html.haml
... ... @@ -0,0 +1,4 @@
  1 += "Oops! Looks like your friends have not listed any #{params[:personal_data_type]} on their Facebook profile"
  2 +:coffeescript
  3 + $ ->
  4 + window.Cs.fb_share_widget.onload_no_friends_data();
5 app/views/facebook_share_widget/facebook/no_personal_data.html.haml
... ... @@ -0,0 +1,5 @@
  1 +.personal_data
  2 + = "Oops! Looks like you have no #{params[:personal_data_type]} listed in your Facebook profile"
  3 +:coffeescript
  4 + $ ->
  5 + window.Cs.fb_share_widget.onload_no_self_data();
5 app/views/facebook_share_widget/facebook/personal_data.html.haml
... ... @@ -0,0 +1,5 @@
  1 +.personal_data
  2 + = render partial: 'personal_data_list'
  3 +:coffeescript
  4 + $ ->
  5 + window.Cs.fb_share_widget.onload_self_data()
4 config/routes.rb
... ... @@ -1,7 +1,7 @@
1 1 FacebookShareWidget::Engine.routes.draw do
2 2 get 'facebook' => 'facebook#index'
3 3 get 'facebook/friends' => 'facebook#friends'
4   - get 'facebook/employers' => 'facebook#employers'
5   - get 'facebook/change_employer' => 'facebook#change_employer'
  4 + get 'facebook/:personal_data_type/data' => 'facebook#personal_data'
  5 + get 'facebook/:personal_data_type/friends_data' => 'facebook#friends_personal_data'
6 6 get 'facebook/share-redirect' => 'facebook#share'
7 7 end
49 lib/facebook_share_widget/facebook_helper.rb
@@ -18,34 +18,45 @@ def facebook_user(facebook_id)
18 18 FbGraph::User.new(facebook_id, access_token: self.facebook_access_token)
19 19 end
20 20
21   - def my_employers
22   - Rails.cache.fetch("employers_of_#{self.facebook_access_token}", :expires_in => 24.hours) do
23   - employers = []
24   - facebook_me.fetch.work.each do |work_object|
25   - employers << work_object.employer
  21 + def my_personal_data personal_data_type
  22 + Rails.cache.fetch("#{personal_data_type}_of_#{self.facebook_access_token}", :expires_in => 24.hours) do
  23 + data = []
  24 + FbGraph::Query.new("SELECT #{personal_data_type} FROM user WHERE uid = me()").
  25 + fetch(self.facebook_access_token).each do |data_object|
  26 + data += get_data_array_for(personal_data_type, data_object)
26 27 end
27   - employers
  28 + data
28 29 end
29 30 end
  31 +
  32 + def get_data_array_for personal_data_type, data_object, first=false
  33 + tokens = personal_data_type.split('.').collect(&:to_sym)
  34 + iteratable_value = data_object[tokens.delete(tokens[0])]
  35 + iteratable_value = [iteratable_value[0]] if first
  36 + tokens.each do |token|
  37 + iteratable_value = iteratable_value.collect {|a| a[token]}
  38 + end
  39 + iteratable_value
  40 + end
30 41
31   - def friends_employers
32   - Rails.cache.fetch("friends_for_#{self.facebook_access_token}", :expires_in => 1.hour) do
33   - employers = Hash.new(0)
34   - FbGraph::Query.new('SELECT uid, name, work.employer FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND work.employer').
  42 + def fb_friends_personal_data personal_data_type
  43 + Rails.cache.fetch("friends_for_#{self.facebook_access_token}_of_#{personal_data_type}", :expires_in => 1.hour) do
  44 + data = Hash.new(0)
  45 + FbGraph::Query.new("SELECT uid, name, #{personal_data_type} FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me()) AND #{personal_data_type}").
35 46 fetch(self.facebook_access_token).each do|f|
36   - employers[f[:work][0][:employer]] += 1
  47 + data[get_data_array_for(personal_data_type, f, true)] += 1
37 48 end
38   - Hash[employers.sort {|a,b| b[1]<=>a[1]}].keys[0..4]
  49 + Hash[data.sort {|a,b| b[1]<=>a[1]}].keys[0..4]
39 50 end
40 51 end
41 52
42   - def facebook_friends(compId)
43   - Rails.cache.fetch("friends_for_#{self.facebook_access_token}_at_#{compId}", :expires_in => 1.hour) do
  53 + def facebook_friends(personal_dataId, personal_data_type)
  54 + Rails.cache.fetch("friends_for_#{self.facebook_access_token}_for_#{personal_data_type}_as_#{personal_dataId}", :expires_in => 1.hour) do
44 55 friends = {}
45   - if(compId != nil)
46   - FbGraph::Query.new('SELECT uid, name, work.employer.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())').
  56 + if(personal_dataId != nil)
  57 + FbGraph::Query.new("SELECT uid, name, #{personal_data_type}.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())").
47 58 fetch(self.facebook_access_token).each do|f|
48   - if(f[:work].collect {|a| a[:employer]}.collect {|a| a[:id]}.include?(compId))
  59 + if(get_data_array_for(personal_data_type, f).collect {|a| a[:id]}.include?(personal_dataId))
49 60 friends[f[:uid].to_s] = { id: f[:uid], name: f[:name] }
50 61 end
51 62 end
@@ -58,8 +69,8 @@ def facebook_friends(compId)
58 69 end
59 70 end
60 71
61   - def facebook_friends_for_link(url, compId)
62   - friends = append_shares_loaded(facebook_friends(compId), url)
  72 + def facebook_friends_for_link(url, personal_dataId, personal_data_type)
  73 + friends = append_shares_loaded(facebook_friends(personal_dataId, personal_data_type), url)
63 74 friends.collect {|key, value| value }
64 75 end
65 76
40 spec/controllers/facebook_controller_spec.rb
@@ -15,7 +15,7 @@
15 15 describe "#friends" do
16 16 it "should return friend list" do
17 17 friends = [{ id: "1", name: "test"}]
18   - controller.should_receive(:facebook_friends_for_link).with('http://google.com/', nil) { friends }
  18 + controller.should_receive(:facebook_friends_for_link).with('http://google.com/', nil, nil) { friends }
19 19
20 20 get :friends, link: 'http://google.com/'
21 21
@@ -25,9 +25,9 @@
25 25
26 26 it "should return friend list with CompId" do
27 27 friends = [{ id: "1", name: "test"}]
28   - controller.should_receive(:facebook_friends_for_link).with('http://google.com/', 1234) { friends }
  28 + controller.should_receive(:facebook_friends_for_link).with('http://google.com/', 1234, 'a.test') { friends }
29 29
30   - get :friends, link: 'http://google.com/', compId: 1234
  30 + get :friends, link: 'http://google.com/', personal_dataId: 1234, personal_data_type: 'a$test'
31 31
32 32 response.should be_successful
33 33 response.body.should == friends.to_json
@@ -35,7 +35,7 @@
35 35
36 36 it "should return error message on fail" do
37 37 error = Exception.new("some error")
38   - controller.should_receive(:facebook_friends_for_link).with(anything(), anything()).and_raise(error)
  38 + controller.should_receive(:facebook_friends_for_link).with(anything(), anything(), anything()).and_raise(error)
39 39
40 40 get :friends
41 41
@@ -46,20 +46,20 @@
46 46 describe "#employers" do
47 47 it "should return employers list" do
48 48 employers = [{ id: "1", name: "Thoughtworks" }]
49   - controller.should_receive(:my_employers) { employers }
  49 + controller.should_receive(:my_personal_data).with('work.employer') { employers }
50 50
51   - get :employers
  51 + get :personal_data, personal_data_type: 'work$employer'
52 52
53 53 response.should be_successful
54   - response.should render_template "facebook_share_widget/facebook/employers"
55   - assigns(:employers).to_json.should == employers.to_json
  54 + response.should render_template "facebook_share_widget/facebook/personal_data"
  55 + assigns(:personal_data_results).to_json.should == employers.to_json
56 56 end
57 57
58 58 it "should return error message on fail" do
59 59 error = Exception.new("some error")
60   - controller.should_receive(:my_employers).and_raise(error)
  60 + controller.should_receive(:my_personal_data).with('work.employer').and_raise(error)
61 61
62   - get :employers
  62 + get :personal_data, personal_data_type: 'work$employer'
63 63
64 64 response.should_not be_successful
65 65 response.body.should == { message: "You are probably not logged in" }.to_json
@@ -67,12 +67,12 @@
67 67
68 68 it "should return no employers message when no employers are listed" do
69 69 employers = []
70   - controller.should_receive(:my_employers) { employers }
  70 + controller.should_receive(:my_personal_data).with('work.employer') { employers }
71 71
72   - get :employers
  72 + get :personal_data, personal_data_type: 'work$employer'
73 73
74 74 response.should be_successful
75   - response.should render_template "facebook_share_widget/facebook/no_employers"
  75 + response.should render_template "facebook_share_widget/facebook/no_personal_data"
76 76 end
77 77 end
78 78 describe "#share" do
@@ -111,23 +111,23 @@
111 111 describe "#change employer" do
112 112 it "should return atmost top five employers list" do
113 113 employers = [{ id: "1", name: "Thoughtworks" }, {id: "2", name: "ControlShift"}]
114   - controller.should_receive(:friends_employers) { employers }
  114 + controller.should_receive(:fb_friends_personal_data).with('work.employer') { employers }
115 115
116   - get :change_employer
  116 + get :friends_personal_data, personal_data_type: 'work$employer'
117 117
118 118 response.should be_successful
119   - response.should render_template "facebook_share_widget/facebook/change_employer"
120   - assigns(:employers).to_json.should == employers.to_json
  119 + response.should render_template "facebook_share_widget/facebook/friends_personal_data"
  120 + assigns(:personal_data_results).to_json.should == employers.to_json
121 121 end
122 122
123 123 it "should return no employers message when no friends' employers are found" do
124 124 employers = []
125   - controller.should_receive(:friends_employers) { employers }
  125 + controller.should_receive(:fb_friends_personal_data).with('work.employer') { employers }
126 126
127   - get :change_employer
  127 + get :friends_personal_data, personal_data_type: 'work$employer'
128 128
129 129 response.should be_successful
130   - response.should render_template "facebook_share_widget/facebook/no_friends_employers"
  130 + response.should render_template "facebook_share_widget/facebook/no_friends_personal_data"
131 131 end
132 132 end
133 133 end
23 spec/lib/facebook_helpers_spec.rb
@@ -49,10 +49,10 @@ def initialize
49 49
50 50 it "should get the friends" do
51 51 raw_friends = [OpenStruct.new({ identifier: "1", name: "name" })]
52   - subject.should_receive(:facebook_friends).with(anything()).and_return(raw_friends)
  52 + subject.should_receive(:facebook_friends).with(anything(), anything()).and_return(raw_friends)
53 53 subject.should_receive(:append_shares_loaded).with(raw_friends, 'http://www.google.com/').and_return(raw_friends)
54 54
55   - subject.facebook_friends_for_link('http://www.google.com/',123)
  55 + subject.facebook_friends_for_link('http://www.google.com/',123,123)
56 56 end
57 57
58 58 it "should append the status" do
@@ -91,7 +91,7 @@ def initialize
91 91 me.stub(:friends) { raw_friends }
92 92 subject.stub(:facebook_me) { me }
93 93
94   - subject.facebook_friends(nil).should == {"1" => { id: "1", name: "name" }}
  94 + subject.facebook_friends(nil, nil).should == {"1" => { id: "1", name: "name" }}
95 95 end
96 96 end
97 97
@@ -100,19 +100,17 @@ def initialize
100 100 raw_friends = [{ uid: "1", name: "name" ,work: [{employer: {id: 123}}]}]
101 101 FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_friends)
102 102 subject.stub(:facebook_access_token).and_return('test')
103   - subject.facebook_friends(123).should == {"1" => { id: "1", name: "name" }}
  103 + subject.facebook_friends(123,"work.employer").should == {"1" => { id: "1", name: "name" }}
104 104 end
105 105 end
106 106
107   - describe "#my_employers" do
  107 + describe "#my_personal_data" do
108 108 it "should return back list of my employers" do
109   - raw_employers = [OpenStruct.new(employer: { identifier: "1", name: "name" }, position: {id: "123",
110   - name: "Senior Consultant"}, start_date: "2012-03")]
111   - fetch = mock
112   - fetch.stub(:work) { raw_employers }
113   - subject.stub(:facebook_me) { mock(fetch: fetch) }
  109 + raw_employers = [{ uid: "1", name: "name" , work: [{employer: {id: "123",
  110 + name: "Senior Consultant"}, start_date: "2012-03"}] }]
  111 + FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_employers)
114 112
115   - subject.my_employers.should == [{ identifier: "1", name: "name" }]
  113 + subject.my_personal_data('work.employer').should == [{ id: "123", name: "Senior Consultant" }]
116 114 end
117 115 end
118 116
@@ -128,8 +126,7 @@ def initialize
128 126 { uid: "8", name: "name8" ,work: [{employer: {id: 66, name: "name66"}}]},
129 127 { uid: "9", name: "name9" ,work: [{employer: {id: 66, name: "name66"}}]}]
130 128 FbGraph::Query.any_instance.stub(:fetch).with(anything()).and_return(raw_friends)
131   - subject.stub(:facebook_access_token).and_return('test')
132   - subject.friends_employers.size.should == 5
  129 + subject.fb_friends_personal_data('work.employer').size.should == 5
133 130 end
134 131 end
135 132

1 comment on commit f65abb0

Nathan Woodhull
Owner

Neat, I like this commit.

Can you add a line in the README about how to use this personal_data functionality?

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