social 0.0.11 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +72 -40
  3. data/lib/social.rb +65 -30
  4. data/lib/social/balance.rb +1 -14
  5. data/lib/social/config/ok.rb +16 -18
  6. data/lib/social/config/vk.rb +15 -17
  7. data/lib/social/determinant.rb +4 -0
  8. data/lib/social/determinant/request_param.rb +56 -0
  9. data/lib/social/determinant/social_prefix.rb +81 -0
  10. data/lib/social/env.rb +21 -15
  11. data/lib/social/network.rb +9 -1
  12. data/lib/social/network/base.rb +60 -9
  13. data/lib/social/network/graph.rb +1 -40
  14. data/lib/social/network/graph/ok.rb +1 -5
  15. data/lib/social/network/graph/ok/base.rb +50 -56
  16. data/lib/social/network/graph/ok/notification.rb +9 -15
  17. data/lib/social/network/graph/ok/user.rb +61 -67
  18. data/lib/social/network/graph/tail.rb +14 -20
  19. data/lib/social/network/graph/vk.rb +1 -5
  20. data/lib/social/network/graph/vk/base.rb +27 -33
  21. data/lib/social/network/graph/vk/notification.rb +11 -17
  22. data/lib/social/network/graph/vk/user.rb +55 -61
  23. data/lib/social/network/ok.rb +9 -17
  24. data/lib/social/network/stub.rb +2 -7
  25. data/lib/social/network/vk.rb +9 -17
  26. data/lib/social/version.rb +1 -1
  27. data/social.gemspec +1 -0
  28. data/spec/lib/social/ok_api_spec.rb +8 -8
  29. data/spec/lib/social/vk_api_spec.rb +2 -2
  30. data/spec/spec_helper.rb +1 -4
  31. metadata +80 -48
  32. data/lib/social/auth.rb +0 -4
  33. data/lib/social/auth/controller.rb +0 -144
  34. data/lib/social/builder.rb +0 -22
  35. data/lib/social/cache.rb +0 -26
  36. data/lib/social/helper.rb +0 -4
  37. data/lib/social/helper/controller.rb +0 -43
  38. data/lib/social/helper/model.rb +0 -61
  39. data/lib/social/network/params.rb +0 -31
  40. data/lib/social/provider.rb +0 -42
@@ -0,0 +1,81 @@
1
+ class Social::Determinant
2
+
3
+ # Класс Rack::Builder'a обеспечивающий
4
+ # работу подхода social-prefix
5
+ #
6
+ # Этот подход заключается в том, что если на сервер
7
+ # приходит запрос вида /<SOCIAL_PREFIX>/*, где
8
+ # SOCIAL_PREFIX может принимать значения типа vk, odkl
9
+ # и п.р.
10
+ #
11
+ # Этот подход выполняет 2 задачи
12
+ #
13
+ # 1) Обеспечивает содержание в URL для Одноклассников слова odkl
14
+ # для показа аватаров пользователей, без добавления лишних роутов
15
+ # в приложение.
16
+ # 2) Обеспечивает выбор и определение используемой социальной сети
17
+ # без добавления этой логики в приложение
18
+ class SocialPrefix < self
19
+
20
+ # Класс провайдерa для обеспечения
21
+ # social-prefix, принимает
22
+ # prefix от builder'a и подмешивает
23
+ # social_env в параметры запроса
24
+ class Provider
25
+
26
+ # @param [String]
27
+ # @return [Class]
28
+ def self.build(prefix)
29
+
30
+ klass = Class.new do
31
+
32
+ class << self
33
+ attr_accessor :prefix
34
+ end
35
+
36
+ def initialize(app)
37
+ @app = app
38
+ end
39
+
40
+ def call(env)
41
+ request = Rack::Request.new(env)
42
+ prefix = self.class.prefix
43
+ type = Social.type_by_prefix(prefix)
44
+ id = Social.id_by_type(type)
45
+
46
+ request.GET['social_env'] = {
47
+ 'prefix' => prefix, 'type' => type, 'id' => id
48
+ }
49
+
50
+ @app.call(request.env)
51
+ end
52
+
53
+ end
54
+
55
+ klass.prefix = prefix
56
+ klass
57
+ end
58
+
59
+ end
60
+
61
+ def self.produce(app)
62
+ new do
63
+
64
+ map '/' do
65
+ run app
66
+ end
67
+
68
+ Social.type_prefixes.each_with_index do |prefix, index|
69
+
70
+ map '/' + prefix do
71
+ use Provider.build(prefix)
72
+ run app
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -4,29 +4,35 @@ module Social
4
4
  include Singleton
5
5
  attr_accessor :id, :type, :prefix
6
6
 
7
+ # Класс объекта определяющего соц.окружения по
8
+ # параметрам запроса
7
9
  class << self
8
10
 
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]
11
+ def init(social_type)
12
+ instance.type = social_type
13
+ instance.id = Social.id_by_type(social_type)
14
+ instance.prefix = Social.prefix_by_type(social_type)
15
+ instance.instance_variable_set(:"@inited", true)
16
16
  end
17
17
 
18
- def id
19
- instance.id
18
+ def init_by_params(params)
19
+ unless social_type = Social.request_social_type(params)
20
+ raise "Can't find social env in params #{params.inspect}"
21
+ else
22
+ init(social_type)
23
+ end
20
24
  end
21
25
 
22
- def type
23
- instance.type
24
- end
26
+ delegate :id, :type, :prefix, :inited?, to: :instance
25
27
 
26
- def prefix
27
- instance.prefix
28
- end
28
+ end
29
+
30
+ def initialize
31
+ @inited = false
32
+ end
29
33
 
34
+ def inited?
35
+ @inited
30
36
  end
31
37
 
32
38
  end
@@ -1,4 +1,12 @@
1
1
  module Social
2
- module Network
2
+ class Network
3
+
4
+ # Возвращает объект текущей соцсети после инициализации Social::Env
5
+ #
6
+ # @return [Social::Network::base]
7
+ def self.current
8
+ ::Social::Network(Social::Env.type) if Env.inited?
9
+ end
10
+
3
11
  end
4
12
  end
@@ -1,14 +1,65 @@
1
- module Social
2
- module Network
3
- module Base
1
+ class Social::Network::Base
4
2
 
5
- include Social::Network::Graph
6
- include Social::Network::Params
3
+ include Singleton
7
4
 
8
- def initialize(root, list, params = nil)
9
- init_params(params)
10
- init_graph_for(root, list)
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
+ def initialize(root, list, params = nil)
13
+ init_params(params)
14
+ init_graph_for(root, list)
15
+ end
16
+
17
+ def params
18
+ @params
19
+ end
20
+
21
+ def params!(params)
22
+ @params = params
23
+ self
24
+ end
25
+
26
+ def param(key)
27
+ @params[key]
28
+ end
29
+
30
+ def param!(key, value)
31
+ @params[key] = value
32
+ self
33
+ end
34
+
35
+ protected
36
+
37
+ def init_params(params)
38
+ @params = params.is_a?(Hash) ? params : {}
39
+ end
40
+
41
+ def init_graph_for(graph_name, graphs)
42
+
43
+ graph_root = Social::Network::Graph.const_get(graph_name.to_s.classify)
44
+
45
+ generated_graphs = graphs.map do |space|
46
+
47
+ namespace = space.to_s.classify
48
+
49
+ begin
50
+ graph_root.const_get(namespace) # try load
51
+
52
+ graph_tail = graph_root.const_get(namespace).new
53
+ graph_tail.root = self
54
+
55
+ [space, graph_tail]
56
+ rescue NameError
57
+ nil
11
58
  end
59
+
12
60
  end
13
- end
61
+
62
+ @graph = Hash[generated_graphs.compact]
63
+ end
64
+
14
65
  end
@@ -1,41 +1,2 @@
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
1
+ module Social::Network::Graph
41
2
  end
@@ -1,6 +1,2 @@
1
- module Social
2
- module Network
3
- class Ok
4
- end
5
- end
1
+ module Social::Network::Graph::Ok
6
2
  end
@@ -25,67 +25,61 @@ module Rack
25
25
  end
26
26
  end
27
27
 
28
- module Social
29
- module Network
30
- module Graph
31
- module Ok
32
- class Base
33
-
34
- include Social::Network::Graph::Tail
35
- include Social::Config::Ok
28
+ module Social::Network::Graph::Ok
29
+ class Base
30
+
31
+ include Social::Network::Graph::Tail
32
+ include Social::Config::Ok
36
33
 
37
- def normalize_msg(msg)
38
- [ ['&nbps;', ' '],
39
- ['&mdash;', '-'],
40
- ['&', ''],
41
- ['%', '']
42
- ].each { |rep| msg.gsub!(*rep) }
43
- msg
44
- end
45
-
46
- def http_query(query)
47
- result = []
48
- url = URI.parse(config['api_server'])
49
- retries = 0
50
- result = Net::HTTP.start(url.host, url.port) { |http|
51
- http.read_timeout = 10
52
- http.get(query)
53
- }
54
- rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
55
- retries += 1
56
- retry if retries < 3
57
- ensure
58
- result
59
- end
34
+ def normalize_msg(msg)
35
+ [ ['&nbps;', ' '],
36
+ ['&mdash;', '-'],
37
+ ['&', ''],
38
+ ['%', '']
39
+ ].each { |rep| msg.gsub!(*rep) }
40
+ msg
41
+ end
60
42
 
61
- def deliver(params)
62
- result = []
63
- request_params = build_request_params(params)
64
- query = "/fb.do?#{Rack::Utils.universal_build(request_params)}"
65
- result = self.http_query(query)
66
- rescue => e
67
- puts "====== Send problem"
68
- puts params.inspect
69
- puts e.to_s
70
- puts e.backtrace
71
- ensure
72
- result
73
- end
43
+ def http_query(query)
44
+ result = []
45
+ url = URI.parse(config['api_server'])
46
+ retries = 0
47
+ result = Net::HTTP.start(url.host, url.port) { |http|
48
+ http.read_timeout = 10
49
+ http.get(query)
50
+ }
51
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
52
+ retries += 1
53
+ retry if retries < 3
54
+ ensure
55
+ result
56
+ end
74
57
 
75
- def build_sig(params, key)
76
- params_to_sign_string = params.keys.inject([]) {|m,e| m << "#{e}=#{params[e]}"}.sort.join
77
- Digest::MD5.hexdigest(params_to_sign_string + key)
78
- end
58
+ def deliver(params)
59
+ result = []
60
+ request_params = build_request_params(params)
61
+ query = "/fb.do?#{Rack::Utils.universal_build(request_params)}"
62
+ result = self.http_query(query)
63
+ rescue => e
64
+ puts "====== Send problem"
65
+ puts params.inspect
66
+ puts e.to_s
67
+ puts e.backtrace
68
+ ensure
69
+ result
70
+ end
79
71
 
80
- def build_request_params(params = {})
81
- params.merge!({ :format => "JSON", :application_key => config[:application_key]})
82
- key = params.delete(:session_secret_key) || config[:secret_key]
83
- params[:sig] = build_sig(params, key)
84
- params
85
- end
72
+ def build_sig(params, key)
73
+ params_to_sign_string = params.keys.inject([]) {|m,e| m << "#{e}=#{params[e]}"}.sort.join
74
+ Digest::MD5.hexdigest(params_to_sign_string + key)
75
+ end
86
76
 
87
- end
88
- end
77
+ def build_request_params(params = {})
78
+ params.merge!({ :format => "JSON", :application_key => config[:application_key]})
79
+ key = params.delete(:session_secret_key) || config[:secret_key]
80
+ params[:sig] = build_sig(params, key)
81
+ params
89
82
  end
83
+
90
84
  end
91
85
  end
@@ -1,18 +1,12 @@
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
1
+ module Social::Network::Graph::Ok
2
+ class Notification < Base
3
+
4
+ def send(options = {})
5
+ result = deliver('method' => 'notifications.sendSimple', 'text' => options[:message], 'uid' => options[:uids])
6
+
7
+ return result unless block_given?
8
+ yield(result) if block_given?
16
9
  end
10
+
17
11
  end
18
12
  end
@@ -1,78 +1,72 @@
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(*args)
1
+ module Social::Network::Graph::Ok
2
+ class User < Base
3
+
4
+ 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'
5
+
6
+ def get_info(*args)
10
7
 
11
- args.pop if args.last.nil?
8
+ args.pop if args.last.nil?
12
9
 
13
- if args.last.kind_of?(Hash)
14
- options = args.pop #.with_indefferend_access
15
- secret = options[:secret]
16
- fields = options[:fields]
17
-
18
- fields = fields.join(',') if fields.kind_of?(Array)
19
-
20
- if fields
21
- avalible_fields = FIELDS.split(',')
22
- fields = fields.split(',').select { |field|
23
- avalible_fields.include?(field)
24
- }.sort.join(',')
25
- fields = nil if fields.empty?
26
- end
27
- end
10
+ if args.last.kind_of?(Hash)
11
+ options = args.pop #.with_indefferend_access
12
+ secret = options[:secret]
13
+ fields = options[:fields]
14
+
15
+ fields = fields.join(',') if fields.kind_of?(Array)
16
+
17
+ if fields
18
+ avalible_fields = FIELDS.split(',')
19
+ fields = fields.split(',').select { |field|
20
+ avalible_fields.include?(field)
21
+ }.sort.join(',')
22
+ fields = nil if fields.empty?
23
+ end
24
+ end
28
25
 
29
- uids = Array.wrap(args)
30
- params = { "method" => 'users.getInfo', "fields" => (fields || FIELDS),
31
- :uids => uids.join(",") }
32
- params[:session_secret_key] = secret if secret
26
+ uids = Array.wrap(args)
27
+ params = { "method" => 'users.getInfo', "fields" => (fields || FIELDS),
28
+ :uids => uids.join(",") }
29
+ params[:session_secret_key] = secret if secret
33
30
 
34
- code, response = self.deliver(params)
35
- result = response.present? ? JSON.load(response) : { 'error_msg' => 'Empty response' }
36
- result = result.is_a?(Hash) && result['error_msg'] ? [] : result
37
-
38
- return result unless block_given?
39
- yield(result) if block_given?
40
- end
41
-
42
- def get_friends(uid, secret = nil)
43
- params = { "method" => 'friends.get', :uid => uid, :session_secret_key => secret }
44
- response = self.send(:deliver, params)
45
-
46
- result = (response.present? && response != true) ? ActiveSupport::JSON.decode(response.body) : []
47
- result = (result.is_a?(Hash) && result['error_msg']) ? [] : result # if returned failure
31
+ code, response = self.deliver(params)
32
+ result = response.present? ? JSON.load(response) : { 'error_msg' => 'Empty response' }
33
+ result = result.is_a?(Hash) && result['error_msg'] ? [] : result
34
+
35
+ return result unless block_given?
36
+ yield(result) if block_given?
37
+ end
38
+
39
+ def get_friends(uid, secret = nil)
40
+ params = { "method" => 'friends.get', :uid => uid, :session_secret_key => secret }
41
+ response = self.send(:deliver, params)
42
+
43
+ result = (response.present? && response != true) ? ActiveSupport::JSON.decode(response.body) : []
44
+ result = (result.is_a?(Hash) && result['error_msg']) ? [] : result # if returned failure
48
45
 
49
- return result unless block_given?
50
- yield(result) if block_given?
51
- end
46
+ return result unless block_given?
47
+ yield(result) if block_given?
48
+ end
52
49
 
53
- def get_friends_profiles(uid, secret = nil, options = nil)
54
- friend_uids = get_friends_uids(uid, secret)
55
- friend_profiles = friend_uids.map { |uid| get_info(uid, secret, options) }.flatten.compact
56
-
57
- return friend_profiles unless block_given?
58
- yield(friend_profiles) if block_given?
59
- end
50
+ def get_friends_profiles(uid, secret = nil, options = nil)
51
+ friend_uids = get_friends_uids(uid, secret)
52
+ friend_profiles = friend_uids.map { |uid| get_info(uid, secret, options) }.flatten.compact
53
+
54
+ return friend_profiles unless block_given?
55
+ yield(friend_profiles) if block_given?
56
+ end
60
57
 
61
- def charge_off_balance(uid, balance)
62
- return [] unless block_given?
63
- yield([]) if block_given?
64
- end
58
+ def charge_off_balance(uid, balance)
59
+ return [] unless block_given?
60
+ yield([]) if block_given?
61
+ end
65
62
 
66
- def balance(uid)
67
- return nil unless block_given?
68
- yield(nil) if block_given?
69
- end
70
-
71
- alias :get_friends_uids :get_friends
72
- alias :get_friends_info :get_friends_profiles
73
-
74
- end
75
- end
63
+ def balance(uid)
64
+ return nil unless block_given?
65
+ yield(nil) if block_given?
76
66
  end
67
+
68
+ alias :get_friends_uids :get_friends
69
+ alias :get_friends_info :get_friends_profiles
70
+
77
71
  end
78
72
  end