plataforma_social 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Capfile ADDED
@@ -0,0 +1,5 @@
1
+ load 'deploy'
2
+ # Uncomment if you are using Rails' asset pipeline
3
+ # load 'deploy/assets'
4
+ Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
5
+ load 'config/deploy' # remove this line to skip loading any of the default tasks
data/config/deploy.rb ADDED
@@ -0,0 +1,54 @@
1
+ set :application, 'plataforma_social'
2
+
3
+ $environment = 'staging' # | staging # production
4
+
5
+ set :scm, :git
6
+ set :scm_verbose, true
7
+ default_run_options[:pty] = true
8
+ set :deploy_via, :remote_cache
9
+
10
+ case $environment
11
+
12
+ when 'staging'
13
+ p "\n\n\nCAREFUL!!!! STAGING DEPLOY!!!\n\n"
14
+
15
+ set :restart_server_command, "/opt/local/nginx/sbin/nginx -s reload"
16
+
17
+ set :repository, "ssh://dito@projetos.dito.com.br:4444/home/git/repositories/plataforma_social.git"
18
+
19
+ set :branch, "master"
20
+
21
+ #usuario ssh
22
+ set :user, "dito"
23
+
24
+ role :app, "31.171.132.244"
25
+ role :web, "31.171.132.244"
26
+ role :db, "31.171.132.244", :primary => true
27
+
28
+ set :deploy_to, "/home/dito/www/plataforma_social"
29
+ set :use_sudo, true
30
+
31
+ after "deploy", "deploy:cleanup"
32
+ else
33
+ p "UNRECOGNIZED ENVIRONMENT"
34
+ end
35
+
36
+
37
+ before "deploy:create_symlink", "deploy:copy_files"
38
+
39
+
40
+ namespace :deploy do
41
+
42
+ task :copy_files, :roles => :app do
43
+ case $environment
44
+ when 'staging'
45
+ run "cd #{release_path} && /home/dito/.rvm/gems/ruby-1.9.3-p327@global/bin/bundle install"
46
+ end
47
+ end
48
+
49
+ desc "Restart the web server"
50
+ task :restart, :roles => :app do
51
+ # sudo restart_server_command
52
+ end
53
+
54
+ end
@@ -1,54 +1,28 @@
1
1
  require "plataforma_social/version"
2
2
  require "plataforma_social/configuration"
3
+ require "plataforma_social/domains"
3
4
  require "plataforma_social/facebook"
4
5
  require "plataforma_social/request"
5
6
  require "plataforma_social/helpers"
6
7
 
7
8
  module PlataformaSocial
8
9
  class << self
9
- def domains module_name
10
- domains = {
11
- :js => 'js',
12
- :analytics => 'analytics',
13
- :login => 'login',
14
- :events => 'events',
15
- :share => 'share',
16
- :comments => 'comments',
17
- :ranking => 'ranking',
18
- :badge => 'badge',
19
- :notification => 'notification'
20
- }
21
-
22
- if domains[module_name.to_sym].present?
23
- name = domains[module_name.to_sym]
24
-
25
- url = case Rails.env
26
- when "production"
27
- "https://#{name}.plataformasocial.com.br"
28
- when "development"
29
- "http://#{name}.dev.plataformasocial.com.br"
30
- when "test"
31
- "http://#{name}.dev.plataformasocial.com.br"
32
- when "staging"
33
- "http://#{name}.dev.plataformasocial.com.br"
34
- end
35
-
36
- url
37
- end
10
+ def configuration
11
+ @configuration ||= Configuration.new
38
12
  end
39
13
 
40
14
  def configure
41
15
  yield(configuration)
42
16
  end
43
-
44
- def configuration
45
- @configuration ||= Configuration.new
46
- end
47
17
 
48
18
  def api_key
49
19
  configuration.api_key
50
20
  end
51
21
 
22
+ def secret
23
+ configuration.secret
24
+ end
25
+
52
26
  def facebook_app_id
53
27
  configuration.facebook_app_id
54
28
  end
@@ -61,14 +35,14 @@ module PlataformaSocial
61
35
  configuration.facebook_app_namespace
62
36
  end
63
37
 
38
+ def facebook_scope
39
+ configuration.facebook_scope
40
+ end
64
41
 
65
- def facebook
66
- @facebook ||= Facebook.new
42
+ def facebook_callback_url
43
+ protocol = Rails.env.development? || Rails.env.staging? ? 'http' : 'https'
44
+ configuration.facebook_callback_url || "#{protocol}://apps.facebook.com/#{configuration.facebook_app_namespace}/"
67
45
  end
68
46
 
69
47
  end
70
- end
71
-
72
- ActiveSupport.on_load(:action_view) do
73
- include PlataformaSocial::Helpers
74
48
  end
@@ -1,5 +1,5 @@
1
1
  module PlataformaSocial
2
2
  class Configuration
3
- attr_accessor :api_key, :facebook_app_secret, :facebook_app_id, :facebook_app_namespace
3
+ attr_accessor :api_key, :secret, :facebook_app_secret, :facebook_app_id, :facebook_app_namespace, :facebook_scope, :facebook_callback_url
4
4
  end
5
5
  end
@@ -0,0 +1,32 @@
1
+ module PlataformaSocial
2
+ def self.domains module_name
3
+ domains = {
4
+ :js => 'js',
5
+ :analytics => 'analytics',
6
+ :login => 'login',
7
+ :events => 'events',
8
+ :share => 'share',
9
+ :comments => 'comments',
10
+ :ranking => 'ranking',
11
+ :badge => 'badge',
12
+ :notification => 'notification'
13
+ }
14
+
15
+ if domains[module_name.to_sym].present?
16
+ name = domains[module_name.to_sym]
17
+
18
+ url = case Rails.env
19
+ when "production"
20
+ "https://#{name}.plataformasocial.com.br"
21
+ when "development"
22
+ "http://#{name}.dev.plataformasocial.com.br"
23
+ when "test"
24
+ "http://#{name}.dev.plataformasocial.com.br"
25
+ when "staging"
26
+ "http://#{name}.dev.plataformasocial.com.br"
27
+ end
28
+
29
+ url
30
+ end
31
+ end
32
+ end
@@ -1,28 +1,72 @@
1
- module PlataformaSocial
2
- class Facebook
3
- def self.parse_signed_request signed_request, max_age = 3600
4
- secret = PlataformaSocial.facebook_app_secret
5
- encoded_sig, encoded_envelope = signed_request.split('.', 2)
6
- envelope = JSON.parse(base64_url_decode(encoded_envelope))
7
- algorithm = envelope['algorithm']
8
-
9
- raise 'Invalid request. (Unsupported algorithm.)' \
10
- if algorithm != 'HMAC-SHA256'
11
-
12
- raise 'Invalid request. (Too old.)' \
13
- if envelope['issued_at'] < Time.now.to_i - max_age
1
+ require 'koala'
14
2
 
15
- raise 'Invalid request. (Invalid signature.)' \
16
- if base64_url_decode(encoded_sig) != OpenSSL::HMAC.hexdigest('sha256', secret, encoded_envelope).split.pack('H*')
17
-
18
- envelope
3
+ module PlataformaSocial
4
+ module Facebook
5
+ def facebook_login
6
+ load_oauth
7
+ load_graph
8
+
9
+ redirect = false
10
+
11
+ if params[:code]
12
+ begin
13
+ @oauth.get_access_token(params[:code])
14
+ rescue Koala::Facebook::OAuthTokenRequestError
15
+ redirect = true if parse_facebook_signed_request && !parse_facebook_signed_request['oauth_token']
16
+ end
17
+ else
18
+ redirect = true if parse_facebook_signed_request && !parse_facebook_signed_request['oauth_token']
19
+ end
20
+
21
+ if parse_facebook_signed_request && parse_facebook_signed_request['oauth_token']
22
+ if !session[:not_verify_facebook_permissions] === true
23
+ permissions = @graph.get_object('me', :fields => 'permissions')['permissions']['data'][0]
24
+ session[:not_verify_facebook_permissions] = true
25
+
26
+ PlataformaSocial.facebook_scope.split(',').each do |permission|
27
+ redirect = true unless permissions[permission.strip]
28
+ end
29
+ end
30
+ end
31
+
32
+ render :text => %|<script type="text/javascript">window.top.location.href="#{@oauth.url_for_oauth_code(:permissions => PlataformaSocial.configuration.facebook_scope)}";</script>| if redirect
19
33
  end
20
-
21
- private
34
+
35
+ def load_user_data
36
+ facebook_data = facebook_user_data.symbolize_keys if facebook_user_data
37
+ plataforma_social_data = plataforma_social_user_data.symbolize_keys if plataforma_social_user_data
22
38
 
23
- def self.base64_url_decode(str)
24
- str += '=' * (4 - str.length.modulo(4))
25
- Base64.decode64(str.tr('-_','+/'))
39
+ @user_data = { :facebook => facebook_data, :plataforma_social => plataforma_social_data }
40
+ end
41
+
42
+ def plataforma_social_user_data
43
+ if params[:signed_request]
44
+ data = Request.post("#{PlataformaSocial.domains('login')}/register", { :signed_request => params[:signed_request] })
45
+ { :reference => data['id'] }
46
+ end
47
+ end
48
+
49
+ def facebook_user_data
50
+ if load_graph
51
+ @graph.get_object('me')
26
52
  end
53
+ end
54
+
55
+ def parse_facebook_signed_request
56
+ load_oauth
57
+ @oauth.parse_signed_request params[:signed_request] if params[:signed_request].present?
58
+ end
59
+
60
+ def load_oauth
61
+ @oauth = Koala::Facebook::OAuth.new(PlataformaSocial.facebook_app_id, PlataformaSocial.facebook_app_secret, PlataformaSocial.facebook_callback_url)
62
+ end
63
+
64
+ def load_graph
65
+ @graph = Koala::Facebook::API.new parse_facebook_signed_request['oauth_token'] if parse_facebook_signed_request.present?
66
+ end
27
67
  end
68
+ end
69
+
70
+ class ActionController::Base
71
+ include PlataformaSocial::Facebook
28
72
  end
@@ -1,7 +1,66 @@
1
1
  module PlataformaSocial
2
2
  module Helpers
3
+
4
+ def plataforma_social_javascripts
5
+ plataforma_social_script_tag + plataforma_social_config_script
6
+ end
7
+
3
8
  def plataforma_social_script_tag
4
- "<script src=\"#{PlataformaSocial.domains('js')}/socialp.js\" type=\"text/javascript\" charset=\"utf-8\"></script>".html_safe
9
+ %Q{<script src="#{PlataformaSocial.domains('js')}/socialp.js" type="text/javascript" charset="utf-8"></script>}.html_safe
10
+ end
11
+
12
+ def plataforma_social_config_script
13
+ %Q{<script type="text/javascript" charset="utf-8">
14
+ $P.init({ apiKey: '#{PlataformaSocial.api_key}' });
15
+ window._FACEBOOK_APP_URL = 'http://apps.facebook.com/#{PlataformaSocial.facebook_app_namespace}';
16
+ </script>}.html_safe
17
+ end
18
+
19
+ def custom_redirect_to path, options = {}
20
+ options.each_pair do |key, value|
21
+ cookies[key] = value
22
+ end
23
+
24
+ render :text => %Q{
25
+ <script type="text/javascript" charset="utf-8">
26
+ window.top.location = "//apps.facebook.com/#{PlataformaSocial.facebook_app_namespace}#{path}";
27
+ </script>
28
+ }
29
+ end
30
+
31
+ def link_to *args, &block
32
+ if args[2][:normal] === true
33
+ super
34
+ else
35
+ unless block_given?
36
+ referer = request.referer
37
+ facebook_domain = 'apps.facebook.com'
38
+ if params["controller"].include?("admin/")
39
+ super
40
+ else
41
+ server_domain = request.env['HTTP_HOST']
42
+ if args[1].index(request.env['HTTP_HOST'])
43
+ args[1].gsub! server_domain, facebook_domain + '/' + PlataformaSocial.facebook_app_namespace
44
+ else
45
+ args[1] = request.protocol + facebook_domain + '/' + PlataformaSocial.facebook_app_namespace + args[1]
46
+ end
47
+
48
+ args[2] ||= {}
49
+ args[2][:target] = '_top'
50
+
51
+ end
52
+ end
53
+
54
+ super
55
+ end
5
56
  end
6
57
  end
7
- end
58
+ end
59
+
60
+ ActiveSupport.on_load(:action_view) do
61
+ include PlataformaSocial::Helpers
62
+ end
63
+
64
+ class ActionController::Base
65
+ include PlataformaSocial::Helpers
66
+ end
@@ -1,5 +1,6 @@
1
1
  require 'rsa'
2
2
  require 'openssl'
3
+ require 'httparty'
3
4
 
4
5
  module PlataformaSocial
5
6
  class Request
@@ -13,7 +14,7 @@ module PlataformaSocial
13
14
  end
14
15
 
15
16
  def self.request url, params = {}, method = "get"
16
- secret = PlataformaSocial.facebook_app_secret
17
+ secret = PlataformaSocial.secret
17
18
  platform_api_key = PlataformaSocial.api_key
18
19
  secret_key = OpenSSL::PKey::RSA.new(File.read("#{Rails.root}/plataforma_social/keys/public.pem")).public_encrypt secret
19
20
  params_name = method == 'get' ? :query : :body
@@ -21,8 +22,8 @@ module PlataformaSocial
21
22
 
22
23
  begin
23
24
  response = HTTParty.send(method.to_sym, url, params_name => params)
24
- rescue
25
- return nil
25
+ rescue => e
26
+ return e.to_s
26
27
  end
27
28
 
28
29
  return nil if response.nil?
@@ -1,3 +1,3 @@
1
1
  module PlataformaSocial
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
 
21
-
21
+ gem.add_dependency "capistrano", "~>2.11.2"
22
22
  gem.add_dependency 'rsa', '~> 0.1.4'
23
+ gem.add_dependency 'koala', '~> 1.6.0'
24
+ gem.add_dependency 'httparty', '~> 0.10.0'
23
25
  end
metadata CHANGED
@@ -1,55 +1,97 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: plataforma_social
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Marcos Nogueira
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-01-21 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capistrano
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.2
30
+ - !ruby/object:Gem::Dependency
21
31
  name: rsa
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.4
38
+ type: :runtime
22
39
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
24
41
  none: false
25
- requirements:
42
+ requirements:
26
43
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 0
31
- - 1
32
- - 4
44
+ - !ruby/object:Gem::Version
33
45
  version: 0.1.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: koala
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.6.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: httparty
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.10.0
34
70
  type: :runtime
35
- version_requirements: *id001
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.10.0
36
78
  description: Plataforma Social's integration gem
37
- email:
79
+ email:
38
80
  - marcos.nogueira@dito.com.br
39
81
  executables: []
40
-
41
82
  extensions: []
42
-
43
83
  extra_rdoc_files: []
44
-
45
- files:
84
+ files:
46
85
  - .gitignore
86
+ - Capfile
47
87
  - Gemfile
48
88
  - LICENSE.txt
49
89
  - README.md
50
90
  - Rakefile
91
+ - config/deploy.rb
51
92
  - lib/plataforma_social.rb
52
93
  - lib/plataforma_social/configuration.rb
94
+ - lib/plataforma_social/domains.rb
53
95
  - lib/plataforma_social/facebook.rb
54
96
  - lib/plataforma_social/helpers.rb
55
97
  - lib/plataforma_social/request.rb
@@ -57,36 +99,26 @@ files:
57
99
  - plataforma_social.gemspec
58
100
  homepage: http://www.dito.com.br/
59
101
  licenses: []
60
-
61
102
  post_install_message:
62
103
  rdoc_options: []
63
-
64
- require_paths:
104
+ require_paths:
65
105
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
106
+ required_ruby_version: !ruby/object:Gem::Requirement
67
107
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
113
  none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
- version: "0"
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
84
118
  requirements: []
85
-
86
119
  rubyforge_project:
87
- rubygems_version: 1.8.10
120
+ rubygems_version: 1.8.24
88
121
  signing_key:
89
122
  specification_version: 3
90
123
  summary: Plataforma Social's integration gem
91
124
  test_files: []
92
-