social 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/lib/social/cache.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Social
|
2
|
+
class Cache
|
3
|
+
|
4
|
+
def initialize(driver)
|
5
|
+
@driver = driver
|
6
|
+
end
|
7
|
+
|
8
|
+
def read(key)
|
9
|
+
driver.read(key) if driver.respond_to? :read
|
10
|
+
end
|
11
|
+
|
12
|
+
def write(key, data)
|
13
|
+
driver.write(key, data) if driver.respond_to? :write
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch(key, &block)
|
17
|
+
return nil if block_gived?
|
18
|
+
unless data = read(key)
|
19
|
+
data = block.call
|
20
|
+
end
|
21
|
+
|
22
|
+
data
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Social
|
2
|
+
module Config
|
3
|
+
|
4
|
+
@@network_name = nil
|
5
|
+
|
6
|
+
def self.included(network)
|
7
|
+
network.send(:include, InstanceMethods)
|
8
|
+
@@network_name = network.name.split('::').last.downcase
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
|
13
|
+
def network_name
|
14
|
+
@@network_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def network
|
18
|
+
@network
|
19
|
+
end
|
20
|
+
|
21
|
+
def config
|
22
|
+
@config_data ||= load_config_file
|
23
|
+
end
|
24
|
+
|
25
|
+
def config_file_path=(new_path)
|
26
|
+
@config_file_path = new_path
|
27
|
+
@config = load_config_file if @config_data
|
28
|
+
end
|
29
|
+
|
30
|
+
def config_root=(new_root)
|
31
|
+
@config_root = new_root
|
32
|
+
end
|
33
|
+
|
34
|
+
def config_root
|
35
|
+
@config_root ||= File.join(Rails.root, 'config') if defined?(Rails)
|
36
|
+
@config_root ||= ENV['SOCIAL_CONFIG_ROOT']
|
37
|
+
@config_root ||= File.join('.', 'config')
|
38
|
+
end
|
39
|
+
|
40
|
+
def config_file_path
|
41
|
+
@config_file_path ||= ENV['SOCIAL_CONFIG_PATH']
|
42
|
+
@config_file_path ||= File.joint(self.config_root, 'social.yml')
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_config_file
|
46
|
+
YAML.load_file(config_file_path).with_indifferent_access
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/social/env.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Social
|
2
|
+
class Env
|
3
|
+
|
4
|
+
include Singleton
|
5
|
+
attr_accessor :id, :type, :prefix
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def init(params)
|
10
|
+
raise "Cant find social env in params #{params.inspect}" unless params[:social_env]
|
11
|
+
|
12
|
+
env = params[:social_env]
|
13
|
+
instance.id = env[:id]
|
14
|
+
instance.type = env[:type]
|
15
|
+
instance.prefix = env[:prefix]
|
16
|
+
end
|
17
|
+
|
18
|
+
def id
|
19
|
+
instance.id
|
20
|
+
end
|
21
|
+
|
22
|
+
def type
|
23
|
+
instance.type
|
24
|
+
end
|
25
|
+
|
26
|
+
def prefix
|
27
|
+
instance.prefix
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Add helper methods for Social in base controller class
|
2
|
+
module Social
|
3
|
+
module Helper
|
4
|
+
module Controller
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.instance_eval do
|
8
|
+
helper_method :current_social_type, :current_social_type_id, :current_social_prefix
|
9
|
+
before_filter :init_social_env
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Helper, returned social type id from request
|
14
|
+
# (controller, view).current_social_type_id
|
15
|
+
def current_social_type_id
|
16
|
+
@current_social_type_id ||= params[:soc_id]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Helper, returned social type from request
|
20
|
+
# (controller, view).current_social_type
|
21
|
+
def current_social_type
|
22
|
+
@current_social_type ||= params[:soc_type]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Helper, returned social type prefix from request
|
26
|
+
# (controller, view).current_social_type_prefix
|
27
|
+
def current_social_prefix
|
28
|
+
@current_social_prefix ||= params[:soc_prefix]
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def init_social_env
|
34
|
+
#Social.current_id = params[:soc_id]
|
35
|
+
#Social.current_type = params[:soc_type]
|
36
|
+
#Social.current_prefix = params[:soc_prefix]
|
37
|
+
|
38
|
+
Social::Env.init(params)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Add helper methods for Social in user model class (Active record)
|
2
|
+
module Social
|
3
|
+
module Helper
|
4
|
+
module Model
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.instance_eval do
|
8
|
+
attr_accessor :auth_params
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Alias for model.current_social_type
|
13
|
+
# model.current_provider String
|
14
|
+
# @params social_type_id = nil
|
15
|
+
def current_provider(social_type_id = nil)
|
16
|
+
current_social_type(social_type_id)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return current social type or social type by social_type_id
|
20
|
+
# model.current_social_type String
|
21
|
+
# @params social_type_id = nil
|
22
|
+
def current_social_type(social_type_id = nil)
|
23
|
+
if social_type_id
|
24
|
+
@current_provider = Social.type_by_id(social_type_id)
|
25
|
+
elsif auth_params && auth_params[:social_type]
|
26
|
+
@current_provider = auth_params[:social_type]
|
27
|
+
else
|
28
|
+
@current_provider = Social::Env.type
|
29
|
+
end
|
30
|
+
|
31
|
+
@current_provider
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return current social type id
|
35
|
+
# model.current_social_type String
|
36
|
+
def current_social_type_id()
|
37
|
+
unless @current_social_type_id
|
38
|
+
@current_social_type_id = Social::Env.id
|
39
|
+
end
|
40
|
+
|
41
|
+
@current_social_type_id
|
42
|
+
end
|
43
|
+
|
44
|
+
# Make standart hash (for js) from model data
|
45
|
+
# model.to_hash hash
|
46
|
+
# @params params = {}
|
47
|
+
def to_hash(params = {})
|
48
|
+
{
|
49
|
+
:id => id,
|
50
|
+
:first_name => first_name,
|
51
|
+
:last_name => last_name,
|
52
|
+
:uid => social_references.last.uid,
|
53
|
+
:name => "#{first_name} #{last_name}",
|
54
|
+
:gender => (male? ? 'male' : 'female'),
|
55
|
+
:birthday => birthdate
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Social
|
2
|
+
module Network
|
3
|
+
module Graph
|
4
|
+
|
5
|
+
attr_reader :graph
|
6
|
+
|
7
|
+
def method_missing(name, *args)
|
8
|
+
return @graph[name] if @graph && @graph[name]
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def init_graph_for(graph_name, graphs)
|
16
|
+
|
17
|
+
graph_root = Social::Network::Graph.const_get(graph_name.to_s.classify)
|
18
|
+
|
19
|
+
generated_graphs = graphs.map do |space|
|
20
|
+
|
21
|
+
namespace = space.to_s.classify
|
22
|
+
|
23
|
+
begin
|
24
|
+
graph_root.const_get(namespace) # try load
|
25
|
+
|
26
|
+
graph_tail = graph_root.const_get(namespace).new
|
27
|
+
graph_tail.root = self
|
28
|
+
|
29
|
+
[space, graph_tail]
|
30
|
+
rescue NameError
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
@graph = Hash[generated_graphs.compact]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Social
|
2
|
+
module Network
|
3
|
+
module Graph
|
4
|
+
module Ok
|
5
|
+
class Base
|
6
|
+
|
7
|
+
include Social::Network::Graph::Tail
|
8
|
+
include Social::Config::Ok
|
9
|
+
|
10
|
+
def normalize_msg(msg)
|
11
|
+
[ ['&nbps;', ' '],
|
12
|
+
['—', '-'],
|
13
|
+
['&', ''],
|
14
|
+
['%', '']
|
15
|
+
].each { |rep| msg.gsub!(*rep) }
|
16
|
+
msg
|
17
|
+
end
|
18
|
+
|
19
|
+
def deliver(params)
|
20
|
+
url = URI.parse(config['api_server'])
|
21
|
+
retries = 0
|
22
|
+
begin
|
23
|
+
res = Net::HTTP.start(url.host, url.port) { |http|
|
24
|
+
http.read_timeout = 10
|
25
|
+
request_params = build_request_params(params)
|
26
|
+
http.get("/fb.do?#{Rack::Utils.universal_build(request_params)}")
|
27
|
+
}
|
28
|
+
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
29
|
+
retries += 1
|
30
|
+
retry if retries < 3
|
31
|
+
end
|
32
|
+
|
33
|
+
rescue => e
|
34
|
+
#Rails.logger.warn "Send problem"
|
35
|
+
#Rails.logger.warn params.inspect
|
36
|
+
#Rails.logger.warn e.inspect
|
37
|
+
ensure
|
38
|
+
res
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_sig(params, key)
|
42
|
+
params_to_sign_string = params.keys.inject([]) {|m,e| m << "#{e}=#{params[e]}"}.sort.join
|
43
|
+
Digest::MD5.hexdigest(params_to_sign_string + key)
|
44
|
+
end
|
45
|
+
|
46
|
+
def build_request_params(params = {})
|
47
|
+
params.merge!({ :format => "JSON", :application_key => config[:application_key]})
|
48
|
+
key = params.delete(:session_secret_key) || config[:secret_key]
|
49
|
+
params[:sig] = build_sig(params, key)
|
50
|
+
params
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Social
|
2
|
+
module Network
|
3
|
+
module Graph
|
4
|
+
module Ok
|
5
|
+
class Notification < Social::Network::Graph::Ok::Base
|
6
|
+
|
7
|
+
def send(options = {})
|
8
|
+
result = deliver('method' => 'notifications.sendSimple', 'text' => options[:message], 'uid' => options[:uids])
|
9
|
+
|
10
|
+
return result unless block_given?
|
11
|
+
yield(result) if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Social
|
2
|
+
module Network
|
3
|
+
module Graph
|
4
|
+
module Ok
|
5
|
+
class User < Social::Network::Graph::Ok::Base
|
6
|
+
|
7
|
+
FIELDS = 'uid,first_name,last_name,gender,birthday,pic_1,pic_2,pic_3,pic_4,url_profile,location,current_location,age,url_profile,current_status'
|
8
|
+
|
9
|
+
def get_info(uids, secret = nil, options = nil)
|
10
|
+
fields = Array.wrap(options.try(:[], :fields)).join(',')
|
11
|
+
fields = fields.present? ? fields : FIELDS
|
12
|
+
|
13
|
+
params = { "method" => 'users.getInfo', "fields" => fields,
|
14
|
+
:uids => Array.wrap(uids).join(","), :session_secret_key => secret }
|
15
|
+
response = self.send(:deliver, params)
|
16
|
+
result = response.present? ? ActiveSupport::JSON.decode(response.body) : []
|
17
|
+
result = result.is_a?(Hash) && result['error_msg'] ? nil : result
|
18
|
+
|
19
|
+
return result unless block_given?
|
20
|
+
yield(result) if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_friends(uid, secret = nil)
|
24
|
+
params = { "method" => 'friends.get', :uid => uid, :session_secret_key => secret }
|
25
|
+
response = self.send(:deliver, params)
|
26
|
+
|
27
|
+
result = (response.present? && response != true) ? ActiveSupport::JSON.decode(response.body) : []
|
28
|
+
result = (result.is_a?(Hash) && result['error_msg']) ? [] : result # if returned failure
|
29
|
+
|
30
|
+
return result unless block_given?
|
31
|
+
yield(result) if block_given?
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_friends_profiles(uid, secret = nil, options = nil)
|
35
|
+
friend_uids = get_friends_uids(uid, secret)
|
36
|
+
friend_profiles = friend_uids.map { |uid| get_info(uid, secret, options) }.flatten.compact
|
37
|
+
|
38
|
+
return friend_profiles unless block_given?
|
39
|
+
yield(friend_profiles) if block_given?
|
40
|
+
end
|
41
|
+
|
42
|
+
alias :get_friends_uids :get_friends
|
43
|
+
alias :get_friends_info :get_friends_profiles
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|