social 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +78 -0
- data/Rakefile +3 -0
- data/lib/social.rb +124 -0
- data/lib/social/auth.rb +4 -0
- data/lib/social/auth/controller.rb +144 -0
- data/lib/social/balance.rb +40 -0
- data/lib/social/builder.rb +22 -0
- data/lib/social/cache.rb +26 -0
- data/lib/social/config.rb +52 -0
- data/lib/social/config/ok.rb +12 -0
- data/lib/social/config/vk.rb +13 -0
- data/lib/social/env.rb +34 -0
- data/lib/social/helper.rb +4 -0
- data/lib/social/helper/controller.rb +43 -0
- data/lib/social/helper/model.rb +61 -0
- data/lib/social/network.rb +4 -0
- data/lib/social/network/base.rb +14 -0
- data/lib/social/network/graph.rb +41 -0
- data/lib/social/network/graph/ok.rb +6 -0
- data/lib/social/network/graph/ok/base.rb +57 -0
- data/lib/social/network/graph/ok/notification.rb +18 -0
- data/lib/social/network/graph/ok/user.rb +49 -0
- data/lib/social/network/graph/tail.rb +23 -0
- data/lib/social/network/graph/vk.rb +6 -0
- data/lib/social/network/graph/vk/base.rb +37 -0
- data/lib/social/network/graph/vk/notification.rb +21 -0
- data/lib/social/network/graph/vk/user.rb +62 -0
- data/lib/social/network/ok.rb +21 -0
- data/lib/social/network/params.rb +31 -0
- data/lib/social/network/stub.rb +9 -0
- data/lib/social/network/vk.rb +21 -0
- data/lib/social/networks.rb +29 -0
- data/lib/social/provider.rb +42 -0
- data/lib/social/version.rb +3 -0
- data/social.gemspec +30 -0
- data/spec/lib/social_spec.rb +12 -0
- data/spec/spec_helper.rb +17 -0
- metadata +157 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kazantsev Nickolay
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
Social API wrapper and Tools
|
2
|
+
====================
|
3
|
+
|
4
|
+
This is social networks api wrapper and authorization tools for social applications.
|
5
|
+
|
6
|
+
Now it is a compilation of code from various projects in production. Without tests. =(
|
7
|
+
NOT RECOMMENDED USE IN PRODUCTION.
|
8
|
+
|
9
|
+
Now supported networks:
|
10
|
+
|
11
|
+
* vk.com
|
12
|
+
* odnoklassniki.ru
|
13
|
+
|
14
|
+
Now supported features:
|
15
|
+
|
16
|
+
* Get user profile
|
17
|
+
* Get user friend profiles
|
18
|
+
* Get user balance and charge off (only vk.com)
|
19
|
+
* Initialize environment by request (example /odkl/* and /vk/*)
|
20
|
+
* Authorization user for Rails application (support transition between pages)
|
21
|
+
|
22
|
+
Install
|
23
|
+
|
24
|
+
gem install social
|
25
|
+
|
26
|
+
|
27
|
+
Use wrapper
|
28
|
+
---------------------
|
29
|
+
|
30
|
+
Social::Network(:ok).user.get_info(uid)
|
31
|
+
|
32
|
+
or
|
33
|
+
|
34
|
+
Social::Network::Vk.user.get_info(uid)
|
35
|
+
|
36
|
+
or
|
37
|
+
|
38
|
+
Social::Network(socio_type).notification.send(:message => msg, :uids => [ ... ])
|
39
|
+
|
40
|
+
Use Tools
|
41
|
+
---------------------
|
42
|
+
|
43
|
+
In config.ru
|
44
|
+
|
45
|
+
builder = Social::Builder::produce(App::Application)
|
46
|
+
run builder
|
47
|
+
|
48
|
+
Adds an initializing environment.
|
49
|
+
|
50
|
+
request['social_env'] = {
|
51
|
+
'prefix' => <SOCIAL_PREFIX>,
|
52
|
+
'type' => <SOCIAL_TYPE_NAME>,
|
53
|
+
'id' => <SOCIAL_TYPE_ID>
|
54
|
+
}
|
55
|
+
|
56
|
+
For example, on request /odkl/*
|
57
|
+
|
58
|
+
params[:social_env][:id] # => 1
|
59
|
+
|
60
|
+
and after
|
61
|
+
|
62
|
+
include Social::Helper::Controller
|
63
|
+
|
64
|
+
in your Rails application controller
|
65
|
+
|
66
|
+
Social::Env.id # => 1
|
67
|
+
Social::Env.type # => :ok
|
68
|
+
Social::Env.prefix # => :odkl
|
69
|
+
|
70
|
+
TODO
|
71
|
+
---------------------
|
72
|
+
|
73
|
+
* Tests, tests, tests ...
|
74
|
+
* More network
|
75
|
+
* More and beautiful API for Rails, Sinatra and Rack applications
|
76
|
+
* More supported features
|
77
|
+
|
78
|
+
### Welcome to contributing!
|
data/Rakefile
ADDED
data/lib/social.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
require 'rack'
|
5
|
+
|
6
|
+
require 'social/version'
|
7
|
+
require 'social/provider'
|
8
|
+
require 'social/builder'
|
9
|
+
require 'social/cache'
|
10
|
+
require 'social/config/vk'
|
11
|
+
require 'social/config/ok'
|
12
|
+
require 'social/config'
|
13
|
+
require 'social/env'
|
14
|
+
require 'social/helper/controller'
|
15
|
+
require 'social/helper/model'
|
16
|
+
require 'social/helper'
|
17
|
+
require 'social/auth/controller'
|
18
|
+
require 'social/auth'
|
19
|
+
require 'social/network/graph/tail'
|
20
|
+
require 'social/network/params'
|
21
|
+
require 'social/network/base'
|
22
|
+
require 'social/network/graph'
|
23
|
+
require 'social/network/ok'
|
24
|
+
require 'social/network/vk'
|
25
|
+
require 'social/network/graph/ok/base'
|
26
|
+
require 'social/network/graph/ok/notification'
|
27
|
+
require 'social/network/graph/ok/user'
|
28
|
+
require 'social/network/graph/ok'
|
29
|
+
require 'social/network/graph/vk/base'
|
30
|
+
require 'social/network/graph/vk/notification'
|
31
|
+
require 'social/network/graph/vk/user'
|
32
|
+
require 'social/network/graph/vk'
|
33
|
+
require 'social/network/stub'
|
34
|
+
require 'social/network'
|
35
|
+
require 'social/networks'
|
36
|
+
|
37
|
+
module Social
|
38
|
+
|
39
|
+
SOCIAL_TYPES = {
|
40
|
+
1 => :ok,
|
41
|
+
2 => :vk,
|
42
|
+
3 => :fb
|
43
|
+
}
|
44
|
+
|
45
|
+
SOCIAL_PREFIX = {
|
46
|
+
:ok => 'odkl',
|
47
|
+
:vk => 'vk',
|
48
|
+
:fb => 'fb'
|
49
|
+
}
|
50
|
+
|
51
|
+
class << self
|
52
|
+
|
53
|
+
def current_id=(id)
|
54
|
+
@id = id
|
55
|
+
end
|
56
|
+
|
57
|
+
def current_id
|
58
|
+
@id
|
59
|
+
end
|
60
|
+
|
61
|
+
def current_type=(type)
|
62
|
+
@type = type
|
63
|
+
end
|
64
|
+
|
65
|
+
def current_type
|
66
|
+
@type
|
67
|
+
end
|
68
|
+
|
69
|
+
def current_prefix=(prefix)
|
70
|
+
@prefix = prefix
|
71
|
+
end
|
72
|
+
|
73
|
+
def current_prefix
|
74
|
+
@prefix
|
75
|
+
end
|
76
|
+
|
77
|
+
def Network(network, params = nil)
|
78
|
+
raise 'Not set network' unless network
|
79
|
+
|
80
|
+
unless SOCIAL_TYPES.values.include?(network.to_sym)
|
81
|
+
raise "Can`t find network type: #{network}"
|
82
|
+
end
|
83
|
+
|
84
|
+
Social::Networks.instance.site(network.to_sym, params)
|
85
|
+
end
|
86
|
+
|
87
|
+
def type_by_id(id = nil)
|
88
|
+
id ||= 1
|
89
|
+
SOCIAL_TYPES[id.to_i]
|
90
|
+
end
|
91
|
+
|
92
|
+
def id_by_type(type = nil)
|
93
|
+
type ||= 'ok'
|
94
|
+
SOCIAL_TYPES.detect { |id, t| t == type.to_sym }.first
|
95
|
+
end
|
96
|
+
|
97
|
+
def type_by_prefix(prefix)
|
98
|
+
SOCIAL_PREFIX.detect { |type, pref| pref == prefix.to_s }.first
|
99
|
+
end
|
100
|
+
|
101
|
+
def id_by_prefix(prefix)
|
102
|
+
id_by_type(type_by_prefix(prefix))
|
103
|
+
end
|
104
|
+
|
105
|
+
def typing(index)
|
106
|
+
return type_by_id(index) if SOCIAL_TYPES.keys.include?(index.to_i)
|
107
|
+
return index.to_sym if SOCIAL_TYPES.values.include?(index.to_sym)
|
108
|
+
end
|
109
|
+
|
110
|
+
def type_codes
|
111
|
+
SOCIAL_TYPES.values
|
112
|
+
end
|
113
|
+
|
114
|
+
def type_ids
|
115
|
+
SOCIAL_TYPES.keys
|
116
|
+
end
|
117
|
+
|
118
|
+
def type_prefixes
|
119
|
+
SOCIAL_PREFIX.values
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
data/lib/social/auth.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
module Social
|
2
|
+
module Auth
|
3
|
+
module Controller
|
4
|
+
|
5
|
+
def self.included(controller)
|
6
|
+
controller.send(:include, InstanceMethods)
|
7
|
+
controller.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def self.extended(controller)
|
12
|
+
|
13
|
+
controller.helper_method :current_user, :signed?, :signed_out?
|
14
|
+
controller.hide_action :current_user, :current_user=,
|
15
|
+
:signed?, :signed_out?,
|
16
|
+
:sign_in, :sign_out,
|
17
|
+
:deny_access
|
18
|
+
|
19
|
+
controller.before_filter :ie_hack
|
20
|
+
controller.before_filter :deny_access, :safari_cookie_set
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
|
27
|
+
def current_user
|
28
|
+
@current_user ||= try_get_user
|
29
|
+
end
|
30
|
+
|
31
|
+
def signed?
|
32
|
+
current_user.present?
|
33
|
+
end
|
34
|
+
|
35
|
+
def signed_out?
|
36
|
+
!signed?
|
37
|
+
end
|
38
|
+
|
39
|
+
def deny_access
|
40
|
+
unless signed?
|
41
|
+
store_location
|
42
|
+
redirect_to "/signin?#{Rack::Utils.build_nested_query(params)}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def sign_in(user, auth_params)
|
47
|
+
remember_token = user.remember_token
|
48
|
+
if remember_token
|
49
|
+
cookies[:remember_token] = {
|
50
|
+
:value => remember_token,
|
51
|
+
:expires => 1.day.from_now.utc
|
52
|
+
}
|
53
|
+
|
54
|
+
cookies[:social_auth_params] = {
|
55
|
+
:value => auth_params.to_json,
|
56
|
+
:expires => 1.day.from_now.utc
|
57
|
+
}
|
58
|
+
|
59
|
+
cookies[:user_id] = { :value => user.id, :expires => 1.day.from_now.utc }
|
60
|
+
|
61
|
+
cookies[:social_type] = {
|
62
|
+
:value => auth_params[:social_type],
|
63
|
+
:expires => 1.day.from_now.utc
|
64
|
+
}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def sign_out
|
69
|
+
current_user.reset_remember_token! if current_user
|
70
|
+
cookies.delete(:remember_token)
|
71
|
+
cookies.delete(:social_auth_params)
|
72
|
+
self.current_user = nil
|
73
|
+
end
|
74
|
+
|
75
|
+
def try_get_user
|
76
|
+
#auth_params = cookies[:social_auth_params] and ActiveSupport::JSON.decode(cookies[:social_auth_params]).with_indifferent_access
|
77
|
+
|
78
|
+
auth_params = ActiveSupport::JSON.decode(cookies[:social_auth_params])
|
79
|
+
auth_params = (auth_params || {}).with_indifferent_access
|
80
|
+
|
81
|
+
finded_user = nil
|
82
|
+
|
83
|
+
if token = cookies[:remember_token]
|
84
|
+
if cookies[:user_id] && (user = Profile.find(cookies[:user_id])) && (user.remember_token == token || token == 'hello_psyfaces')
|
85
|
+
user.auth_params = auth_params
|
86
|
+
finded_user = user
|
87
|
+
else
|
88
|
+
finded_user = Profile.authenticate_by_token(token, auth_params)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
self.send(:after_authenticate_user, finded_user) if finded_user && self.respond_to?(:after_authenticate_user)
|
93
|
+
|
94
|
+
finded_user
|
95
|
+
end
|
96
|
+
|
97
|
+
def redirect_back_or(default)
|
98
|
+
redirect_to(return_to || default)
|
99
|
+
clear_return_to
|
100
|
+
end
|
101
|
+
|
102
|
+
def return_to
|
103
|
+
session[:return_to] || params[:return_to]
|
104
|
+
end
|
105
|
+
|
106
|
+
def clear_return_to
|
107
|
+
session[:return_to] = nil
|
108
|
+
end
|
109
|
+
|
110
|
+
def redirect_to_root
|
111
|
+
redirect_to('/')
|
112
|
+
end
|
113
|
+
|
114
|
+
protected
|
115
|
+
|
116
|
+
def auth_params
|
117
|
+
@auth_params ||= {
|
118
|
+
:session_key => params[:session_key],
|
119
|
+
:auth_sig => params[:auth_sig],
|
120
|
+
:apiconnection => params[:apiconnection],
|
121
|
+
:session_secret_key => params[:session_secret_key]
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
def safari_cookie_set
|
126
|
+
cookies['safari_cookie_fix'] = 'OK'
|
127
|
+
end
|
128
|
+
|
129
|
+
def ie_hack
|
130
|
+
response.headers["P3P"]='CP="CAO PSA OUR"'
|
131
|
+
#header('P3P: CP="NOI ADM DEV COM NAV OUR STP"');
|
132
|
+
end
|
133
|
+
|
134
|
+
def store_location
|
135
|
+
if request.get?
|
136
|
+
session[:return_to] = request.fullpath
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Social
|
2
|
+
module Balance
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.instance_eval do
|
6
|
+
|
7
|
+
include ::Balance::Observer
|
8
|
+
with_balance :charge_off_callback => :charge_off_callback
|
9
|
+
|
10
|
+
# Списание средств если нужно извещать 3ю сторону
|
11
|
+
# TODO: переделать на транзакции
|
12
|
+
def charge_off_callback(money)
|
13
|
+
soc_type = Social.type_by_id(current_social_type_id)
|
14
|
+
network = Social::Network(soc_type)
|
15
|
+
#if network && network.respond_to?(:rate) && network.rate != 1
|
16
|
+
if soc_type.to_sym == :vk
|
17
|
+
result = network.user.charge_off_balance(current_uid, money)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def payments
|
25
|
+
Payment.where(:uid => self.uid, :social_type_id => self.social_type_id)
|
26
|
+
end
|
27
|
+
|
28
|
+
def payments_create(hash)
|
29
|
+
param = {:uid => self.uid, :social_type_id => self.social_type_id, :user_id => nil}.merge(hash)
|
30
|
+
param.delete(:initiatable) if param[:initiatable].is_a? Profile
|
31
|
+
Payment.create(param)
|
32
|
+
end
|
33
|
+
|
34
|
+
def initiated
|
35
|
+
[]
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Social
|
2
|
+
class Builder < Rack::Builder
|
3
|
+
def self.produce(app)
|
4
|
+
new do
|
5
|
+
|
6
|
+
map '/' do
|
7
|
+
run app
|
8
|
+
end
|
9
|
+
|
10
|
+
Social.type_prefixes.each_with_index do |prefix, index|
|
11
|
+
|
12
|
+
map '/' + prefix do
|
13
|
+
use Social::Provider.build(prefix)
|
14
|
+
run app
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|