vk 0.0.4 → 0.6.5.53

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.
@@ -5,8 +5,6 @@ require 'delegate'
5
5
  module Vk
6
6
  class Post
7
7
  class Wall
8
- extend ActiveSupport::Memoizable
9
-
10
8
  attr_accessor :uid, :count, :posts
11
9
 
12
10
  def initialize(uid, count, posts)
@@ -14,16 +12,14 @@ module Vk
14
12
  end
15
13
 
16
14
  def user
17
- Vk::User.find(uid)
15
+ @user ||= Vk::User.find(uid)
18
16
  end
19
- memoize :user
20
17
 
21
18
  def [](index)
22
19
  raise "Post #{index} is not exist" if index > count + 1
23
20
  load_posts_to(index) if index >= loaded_posts
24
- if post = posts[index]
25
- Vk::Post.find(Vk::Post.id_for(post), data: post)
26
- end
21
+ post = posts[index]
22
+ Vk::Post.find(Vk::Post.id_for(post), data: post) if post
27
23
  end
28
24
 
29
25
  def first; self[0]; end
@@ -0,0 +1,43 @@
1
+ require 'vk'
2
+ require 'active_support/concern'
3
+
4
+ module Vk
5
+ begin
6
+ require 'tty-prompt'
7
+ Prompt = TTY::Prompt
8
+ rescue LoadError
9
+ class Prompt
10
+ # @param [String] message
11
+ # @param [Hash] _options
12
+ def say(message = '', _options = {})
13
+ puts(message)
14
+ end
15
+
16
+ def ask(message = '', *_args, &_block)
17
+ print(message << ' ')
18
+ gets.chomp
19
+ end
20
+ end
21
+ end
22
+
23
+ module PromptExtension
24
+ extend ActiveSupport::Concern
25
+
26
+ module ClassMethods
27
+ # @return [TTY::Prompt]
28
+ def prompt
29
+ @prompt ||= Prompt.new
30
+ end
31
+ attr_writer :prompt
32
+ end
33
+
34
+ private
35
+
36
+ # @return [TTY::Prompt]
37
+ def prompt
38
+ @prompt ||= self.class.prompt
39
+ end
40
+
41
+ attr_writer :prompt
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Region < Base
5
+ self.fields = [:id, :title, :country_id]
6
+
7
+ def country
8
+ @country ||= Vk::Country.find(country_id)
9
+ end
10
+
11
+ def to_s
12
+ title
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,85 @@
1
+ module Vk
2
+ class Result
3
+ include Enumerable
4
+
5
+ # @example
6
+ # Vk::Result.new('database.getCountries', Vk::Country, options)
7
+ def initialize(method, items_class, options = {}, merged_attributes = {})
8
+ @items = []
9
+ @amount = 0
10
+ @client = options.delete(:client) { Vk.client }
11
+ @items_class = items_class
12
+ @method = method
13
+ @options = options
14
+ @options[:offset] ||= 0
15
+ @options[:count] ||= 100
16
+ @merged_attributes = merged_attributes
17
+ end
18
+
19
+ # @return [Fixnum]
20
+ def count
21
+ load_items unless @count
22
+ @count
23
+ end
24
+
25
+ # @return [Fixnum]
26
+ def amount
27
+ @items.size
28
+ end
29
+
30
+ # @return [<Vk::Base>]
31
+ attr_reader :items
32
+
33
+ def each(&block)
34
+ @items.each do |item|
35
+ block.call(item)
36
+ end
37
+ while amount && count && amount < count
38
+ increase_offset!
39
+ load_items(&block)
40
+ end
41
+ end
42
+
43
+ # @return [<Vk::Base>]
44
+ def all
45
+ while amount && count && amount < count
46
+ increase_offset!
47
+ load_items
48
+ end
49
+ @items
50
+ end
51
+
52
+ def method_missing(name, *arguments, &block)
53
+ if respond_to_missing?(name, false)
54
+ @items_class.public_send(name, *arguments, &block)
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ # @param [String] name
61
+ # @param [Boolean] include_all
62
+ def respond_to_missing?(name, include_all)
63
+ @items_class.respond_to?(name, include_all)
64
+ end
65
+
66
+ private
67
+
68
+ def increase_offset!
69
+ @options[:offset] = amount
70
+ end
71
+
72
+ def load_items(&block)
73
+ raise Vk::TooMuchArguments.new(@method, 'count', 1000) if @options[:count].try(:>, 1000)
74
+ data = @client.request(@method, @options)
75
+ return unless data
76
+ @count = data['count']
77
+ sleep 0.5
78
+ data['items'].each do |item|
79
+ item = @items_class.new(item.merge(@merged_attributes))
80
+ @items << item
81
+ block.call(item) if block_given?
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,114 @@
1
+ require 'vk'
2
+ require 'vk/base'
3
+ require 'active_support/core_ext/module/attribute_accessors'
4
+
5
+ module Vk
6
+ module Stats
7
+ TIME_FORMAT = '%Y-%m-%d'
8
+
9
+ module_function
10
+
11
+ mattr_accessor :loader
12
+
13
+ self.loader = Vk.client
14
+
15
+ # @param [Hash] options
16
+ # @option options [Fixnum] :group_id
17
+ # @option options [Fixnum] :app_id
18
+ # @option options [#to_date] :date_from
19
+ # @option options [#to_date] :date_to
20
+ def get(options = {})
21
+ options[:date_from] = options[:date_from].to_date.strftime(TIME_FORMAT) if options.key?(:date_from)
22
+ options[:date_to] = options[:date_to].to_date.strftime(TIME_FORMAT) if options.key?(:date_to)
23
+ result = loader.request('stats.get', options)
24
+ result.map { |period| Period.new(period['day'], data: period) } if result
25
+ end
26
+
27
+ class Period < Vk::Base
28
+ self.key_field = :day
29
+ self.fields = [
30
+ :views, :visitors, :reach, :reach_subscribers, :subscribed, :unsubscribed, :sex, :age, :sex_age, :cities, :countries
31
+ ]
32
+
33
+ def date
34
+ @date ||= Date.parse(read_attribute(:day))
35
+ end
36
+
37
+ def sex
38
+ @sex ||= (read_attribute(:sex) || []).map do |sex|
39
+ Vk::Stats::Sex.new(nil, data: sex) if sex
40
+ end.compact
41
+ end
42
+
43
+ def age
44
+ @age ||= (read_attribute(:age) || []).map do |age|
45
+ Vk::Stats::Age.new(nil, data: age) if age
46
+ end.compact
47
+ end
48
+
49
+ def sex_age
50
+ @sex_age ||= (read_attribute(:sex_age) || []).map do |sex_age|
51
+ Vk::Stats::SexAge.new(nil, data: sex_age) if sex_age
52
+ end.compact
53
+ end
54
+
55
+ def cities
56
+ @cities ||= (read_attribute(:cities) || []).map do |city|
57
+ Vk::Stats::City.new(nil, data: city) if city
58
+ end.compact
59
+ end
60
+
61
+ def countries
62
+ @countries ||= (read_attribute(:countries) || []).map do |country|
63
+ Vk::Stats::Country.new(nil, data: country) if country
64
+ end.compact
65
+ end
66
+
67
+ private
68
+
69
+ def load_data(*)
70
+ end
71
+ end
72
+
73
+ class Sex < Vk::Base
74
+ self.key_field = :value
75
+ self.fields = [:visitors]
76
+ end
77
+
78
+ class Age < Vk::Base
79
+ self.key_field = :value
80
+ self.fields = [:visitors]
81
+ end
82
+
83
+ class SexAge < Vk::Base
84
+ self.key_field = :value
85
+ self.fields = [:visitors]
86
+
87
+ def sex
88
+ split_sex_age
89
+ @sex
90
+ end
91
+
92
+ def age
93
+ split_sex_age
94
+ @age
95
+ end
96
+
97
+ private
98
+ def split_sex_age
99
+ return if @sex && @age
100
+ @sex, @age = value.split(';')
101
+ end
102
+ end
103
+
104
+ class City < Vk::Base
105
+ self.key_field = :value
106
+ self.fields = [:name, :visitors]
107
+ end
108
+
109
+ class Country < Vk::Base
110
+ self.key_field = :value
111
+ self.fields = [:name, :visitors, :code]
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,17 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Street < Base
5
+ self.fields = [:id, :title]
6
+
7
+ def to_s
8
+ title
9
+ end
10
+
11
+ protected
12
+
13
+ def load_data(options = {})
14
+ @attributes = @attributes.merge(loader.get_street_by_id(id))
15
+ end
16
+ end
17
+ end
@@ -2,18 +2,27 @@ require 'vk/base'
2
2
 
3
3
  module Vk
4
4
  class User < Base
5
- self.key_field = :uid
6
- self.fields = [:uid, :first_name, :last_name, :nickname, :domain, :sex, :bdate, :birthdate, :city, :country, :timezone, :photo, :photo_medium, :photo_big, :has_mobile, :rate, :contacts, :education, :online]
7
-
8
- class << self
9
- def find_all(ids, options = {})
10
- loaded_ids = ids & identity_map.keys
11
- ids_to_load = ids - loaded_ids
12
- identity_map.values_at(*loaded_ids).tap do |results|
13
- if ids_to_load.any?
14
- results += loader.get_profiles(ids_to_load, options).map do |profile|
15
- new(profile['uid'], data: profile)
16
- end
5
+ RELATION_MAPPING = {
6
+ 0 => nil,
7
+ 1 => :single,
8
+ 2 => :friendship,
9
+ 3 => :plight,
10
+ 4 => :married,
11
+ 5 => :complicated,
12
+ 6 => :looking,
13
+ 7 => :love
14
+ }
15
+
16
+ self.key_field = :id
17
+ self.fields = %i(id first_name last_name deactivated hidden sex bdate city country photo_50 photo_100 photo_200_orig photo_200 photo_400_orig photo_max photo_max_orig photo_id online online_mobile domain has_mobile contacts connections site education universities schools can_post can_see_all_posts can_see_audio can_write_private_message status last_seen common_count relation relatives counters screen_name maiden_name timezone occupation activities interests music movies tv books games about quotes personal)
18
+
19
+ def self.find_all(ids, options = {})
20
+ loaded_ids = ids & identity_map.keys
21
+ ids_to_load = ids - loaded_ids
22
+ identity_map.values_at(*loaded_ids).tap do |results|
23
+ if ids_to_load.any?
24
+ results += loader.get_users(ids_to_load, options).map do |profile|
25
+ new(profile['id'], data: profile)
17
26
  end
18
27
  end
19
28
  end
@@ -21,58 +30,132 @@ module Vk
21
30
 
22
31
  attr_accessor :posts_count
23
32
 
33
+ # @return [String]
24
34
  def name
25
- "#{first_name} #{last_name}"
35
+ @name ||= "#{first_name} #{last_name}"
36
+ end
37
+
38
+ # @return [Date]
39
+ def born_on
40
+ @born_on ||= Date.strptime(bdate, '%d.%m.%Y') if born_on?
26
41
  end
27
- memoize :name
28
42
 
43
+ def born_on?
44
+ read_attribute(:bdate).to_s.split(/\./).size == 3
45
+ end
46
+
47
+ # @return [:unknown, :female, :male]
48
+ def gender
49
+ {
50
+ 0 => :unknown,
51
+ 1 => :female,
52
+ 2 => :male
53
+ }[sex]
54
+ end
55
+
56
+ # @return [Fixnum]
29
57
  def city_id
30
58
  read_attribute(:city)
31
59
  end
32
60
 
61
+ # @return [Vk::City]
33
62
  def city
34
- Vk::City.find(city_id)
63
+ @city ||= Vk::City.find(city_id)
35
64
  end
36
- memoize :city
37
65
 
66
+ # @return [Fixnum]
38
67
  def country_id
39
68
  read_attribute(:country)
40
69
  end
41
70
 
71
+ # @return [Vk::Country]
42
72
  def country
43
- Vk::Country.find(country_id)
73
+ @country ||= Vk::Country.find(country_id)
44
74
  end
45
- memoize :country
46
75
 
47
- def friend_ids
48
- loader.get_friends(uid)
76
+ # @return [<Fixnum>]
77
+ def friend_ids(options = {})
78
+ @friend_ids ||= loader.get_friends(id, options = {})
49
79
  end
50
- memoize :friend_ids
51
80
 
81
+ # @param [<Vk::User>] options
52
82
  def friends(options = {})
53
- User.find_all(friend_ids, options)
83
+ @friends ||= {}
84
+ @friends[options] ||=
85
+ if options[:fields]
86
+ loader.get_friends(id, options).all
87
+ else
88
+ User.find_all(friend_ids)
89
+ end
90
+ end
91
+
92
+ # @return [<Vk::Photo>]
93
+ def profile_photos
94
+ @profile_photos ||= loader.get_photos(id, 'profile').all
95
+ end
96
+
97
+ # @return [Boolean]
98
+ def deactivated?
99
+ read_attribute(:deactivated)
54
100
  end
55
- memoize :friends
56
101
 
102
+ def relation_type
103
+ RELATION_MAPPING[relation]
104
+ end
105
+
106
+ # @return [Vk::User]
107
+ def relation_partner
108
+ partner = read_attribute(:relation_partner)
109
+ Vk::User.new(partner) if partner
110
+ end
111
+
112
+ # @return [<Vk::Audio::Album>]
113
+ def audio_albums
114
+ @audio_albums ||= loader.get_audio_albums(id).all
115
+ end
116
+
117
+ # @return [<Vk::Audio>]
118
+ def audios
119
+ @audios ||= loader.get_audios(id).all
120
+ end
121
+
122
+ # @return [String]
57
123
  def to_s
58
124
  name
59
125
  end
60
126
 
127
+ # @param [Vk::Wall] options
61
128
  def wall(options = {})
62
- count, *posts = loader.get_wall(id, options)
63
- Vk::Post::Wall.new(id, count, posts)
129
+ @wall ||= {}
130
+ @wall[options] ||=
131
+ begin
132
+ count, *posts = loader.get_wall(id, options)
133
+ Vk::Post::Wall.new(id, count, posts)
134
+ end
64
135
  end
65
- memoize :wall
136
+
66
137
  alias posts wall
67
138
 
139
+ # @return [Fixnum]
68
140
  def posts_count
69
141
  wall.count
70
142
  end
71
143
 
144
+ # @param [Hash] options
145
+ # @see Vk::Request.get_groups
146
+ # @return [<Vk::Group>]
147
+ def groups(options = {})
148
+ @groups ||= {}
149
+ options[:extended] = true
150
+ index = options.hash
151
+ @groups[index] ||= loader.get_groups(id, options)
152
+ @groups[index].all
153
+ end
154
+
72
155
  protected
73
156
 
74
- def load_data(options = {})
75
- @attributes = @attributes.merge(loader.get_profile(id, options))
157
+ def load_data(options = { fields: self.class.fields })
158
+ @attributes = @attributes.merge(loader.get_user(id, options))
76
159
  end
77
160
  end
78
161
  end