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.
@@ -0,0 +1,24 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Friends
6
+ # Friends information
7
+ # @param [Fixnum] user_id user identifier
8
+ # @param [Hash] options
9
+ # @option options [Array<String>] :fields ([:user_id, :first_name, :last_name]) what fields to request
10
+ # @option options [Fixnum] :count how many friends to request
11
+ # @option options [Fixnum] :offset offset of friends to request
12
+ # @return [<Fixnum>, <Vk::User>]
13
+ def get_friends(user_id, options = {})
14
+ options[:user_id] = user_id
15
+
16
+ if options[:fields]
17
+ Vk::Result.new('friends.get', Vk::User, options).all
18
+ else
19
+ request('friends.get', options)['items']
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Groups
6
+ # Identifiers of groups in which user participates
7
+ # @param [Integer] user_id user’s identifier
8
+ # @param [Hash] options
9
+ # @option options [Boolean] :extended (0)
10
+ # @option options [<:admin, :editor, :moder, :groups, :publics, :events>] :filter
11
+ # @option options [<:city, :country, :place, :description, :wiki_page, :members_count, :counters, :start_date, :end_date, :can_post, :can_see_all_posts, :activity, :status, :contacts, :links, :fixed_post, :verified, :site, :can_create_topic>] :fields
12
+ # @option options [Fixnum] :offset
13
+ # @option options [Fixnum] :count
14
+ # @return [Array] array of group identifiers
15
+ def get_groups(user_id, options = {})
16
+ options[:user_id] = user_id
17
+ options[:extended] = !!options[:extended] ? 1 : 0 if options[:extended]
18
+ options[:filter] = options[:filter].map(&:to_s).join(',') if options[:filter]
19
+ options[:fields] = options[:fields].map(&:to_s).join(',') if options[:fields]
20
+ Vk::Result.new('groups.get', Vk::Group, options)
21
+ end
22
+
23
+ # @param [String] group_ids
24
+ # @param [Hash] options
25
+ # @option options [<:city, :country, :place, :description, :wiki_page, :members_count, :counters, :start_date, :end_date, :can_post, :can_see_all_posts, :activity, :status, :contacts, :links, :fixed_post, :verified, :site, :can_create_topic>] :fields
26
+ def get_groups_by_id(group_ids, options = {})
27
+ options[:group_ids] = Array(group_ids).join(',')
28
+ (request('groups.getById', options) || []).map { |group| Vk::Group.new(group) }
29
+ end
30
+
31
+ # @param [String] group_id
32
+ # @param [Hash] options
33
+ # @option options [<:city, :country, :place, :description, :wiki_page, :members_count, :counters, :start_date, :end_date, :can_post, :can_see_all_posts, :activity, :status, :contacts, :links, :fixed_post, :verified, :site, :can_create_topic>] :fields
34
+ def get_group_by_id(group_id, options = {})
35
+ get_groups_by_id(group_id, options).first
36
+ end
37
+ alias get_group get_group_by_id
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,137 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Newsfeed
6
+ FIELDS = %i(
7
+ sex bdate city country photo_50 photo_100 photo_200_orig photo_200 photo_400_orig photo_max photo_max_orig
8
+ online online_mobile domain has_mobile contacts connections site education universities schools
9
+ can_post can_see_all_posts can_see_audio can_write_private_message status last_seen common_count
10
+ relation relatives screen_name maiden_name timezone occupation activities interests music movies tv books games
11
+ about quotes
12
+ )
13
+
14
+ class SearchItem < Vk::Base
15
+ self.fields = ([
16
+ :id, :owner_id, :from_id, :signer_id, :date, :text, :comments, :likes, :attachments, :geo
17
+ ] + FIELDS).uniq.compact
18
+
19
+ # @return [SearchResult]
20
+ attr_accessor :search_result
21
+
22
+ def as_search_result_of(search_result)
23
+ self.search_result = search_result
24
+ self
25
+ end
26
+
27
+ # @return [Vk::User, Vk::Group]
28
+ def owner
29
+ find_group_or_user(owner_id)
30
+ end
31
+
32
+ # @return [Vk::User, Vk::Group]
33
+ def from
34
+ find_group_or_user(from_id)
35
+ end
36
+
37
+ # @return [Vk::User]
38
+ def signer
39
+ find_group_or_user(signer_id)
40
+ end
41
+
42
+ def time
43
+ Time.at(date)
44
+ end
45
+
46
+ private
47
+
48
+ def find_group_or_user(user_or_group_id)
49
+ user_or_group_id = user_or_group_id.to_i
50
+ return if user_or_group_id == 0
51
+ if search_result
52
+ if user_or_group_id < 0
53
+ search_result.groups.detect { |group| group.id == -user_or_group_id }
54
+ else
55
+ search_result.profiles.detect { |user| user.id == user_or_group_id }
56
+ end
57
+ else
58
+ if user_or_group_id < 0
59
+ Vk::Group.new(-user_or_group_id)
60
+ else
61
+ Vk::User.new(user_or_group_id)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ class SearchResult
68
+ # @return [Hash]
69
+ attr_accessor :options
70
+
71
+ # @param [Vk::Client] client
72
+ # @param [Hash] options
73
+ def initialize(client, options = {})
74
+ @client = client
75
+ @options = options
76
+ end
77
+
78
+ def result
79
+ @result ||= @client.request('newsfeed.search', @options)
80
+ end
81
+
82
+ def fetch!
83
+ result
84
+ self
85
+ end
86
+
87
+ # @return [Array<SearchItem>]
88
+ def items
89
+ @items ||= result['items'].map do |item|
90
+ SearchItem.new(item).as_search_result_of(self)
91
+ end
92
+ end
93
+
94
+ # @return [Array<Vk::User>]
95
+ def profiles
96
+ @profiles ||= result['profiles'].map { |user| Vk::User.new(user) }
97
+ end
98
+
99
+ # @return [Array<Vk::Group>]
100
+ def groups
101
+ @groups ||= result['groups'].map { |group| Vk::Group.new(group) }
102
+ end
103
+
104
+ # @return [String]
105
+ def next_from
106
+ @next_from ||= result['next_from']
107
+ end
108
+
109
+ # @return [SearchResult]
110
+ def next_page
111
+ self.class.new(@client, options.merge(start_from: next_from))
112
+ end
113
+ end
114
+
115
+
116
+ # Friends information
117
+ # @param [Hash] options
118
+ # @option options [String] :q search query string
119
+ # @option options [Boolean] :extended search query string
120
+ # @option options [Fixnum] :count (1000) how many posts to request
121
+ # @option options [Numeric] :latitude -90 to 90
122
+ # @option options [Numeric] :longitude -180 to 180
123
+ # @option options [DateTime] :start_time
124
+ # @option options [DateTime] :end_time
125
+ # @option options [String] :start_from
126
+ # @option options [Array<Symbol>] :fields see {FIELDS}
127
+ # @return [SearchResult]
128
+ def search(options = {})
129
+ options[:extended] = !!options[:extended] ? 1 : 0 if options[:extended]
130
+ options[:start_time] = options[:start_time].to_i if options[:start_time]
131
+ options[:end_time] = options[:end_time].to_i if options[:end_time]
132
+ options[:fields] = Array.wrap(options[:fields]).compact.join(',') if options[:fields]
133
+ SearchResult.new(self, options).fetch!
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,36 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Photos
6
+ # @param [String] owner_id
7
+ # @param [Hash] options
8
+ # @option options :offset [Fixnum] offset for loaded countries
9
+ # @option options :count [Fixnum] (100) amount of countries to load
10
+ # @return [Vk::Result<Vk::Album>]
11
+ def get_albums(owner_id, options = {})
12
+ options[:owner_id] = owner_id
13
+ options[:need_system] = !!options[:need_system] ? 1 : 0 if options.key?(:need_system)
14
+ options[:photo_sizes] = !!options[:photo_sizes] ? 1 : 0 if options.key?(:photo_sizes)
15
+ options[:need_covers] = !!options[:need_covers] ? 1 : 0 if options.key?(:need_covers)
16
+ options[:album_ids] = Array(options[:album_ids]).map(&:to_s).join(',') if options.key?(:album_ids)
17
+ require 'vk/album'
18
+ Vk::Result.new('photos.getAlbums', Vk::Album, options)
19
+ end
20
+
21
+ # @return [Vk::Album]
22
+ def get_album(owner_id, album_id, options = {})
23
+ options[:album_ids] = album_id
24
+ get_albums(owner_id, options).all.first
25
+ end
26
+
27
+ # @return [Vk::Result<Vk::Photo>]
28
+ def get_photos(owner_id, album_id, options = {})
29
+ options[:owner_id] = owner_id
30
+ options[:album_id] = album_id
31
+ require 'vk/photo'
32
+ Vk::Result.new('photos.get', Vk::Photo, options)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require 'vk/exceptions'
2
+ require 'active_support/core_ext/array/wrap'
3
+
4
+ module Vk
5
+ module DSL
6
+ module Users
7
+ # Have user installed app?
8
+ # @param [String] user_id user’s identifier
9
+ # @return [Boolean] does user installed app
10
+ def app_user?(user_id)
11
+ request('users.isAppUser', user_id: user_id) == '1'
12
+ end
13
+
14
+ def get_users(user_ids, options = {})
15
+ options[:user_ids] = Array.wrap(user_ids).join(',')
16
+ (request('users.get', options) || []).map do |user|
17
+ Vk::User.new(user)
18
+ end
19
+ end
20
+
21
+ # @param [String] user_id
22
+ # @param [Hash] options
23
+ # @return [Vk::User]
24
+ def get_user(user_id, options = {})
25
+ get_users(user_id, options).first
26
+ end
27
+
28
+ # @return [Vk::User]
29
+ def get_me
30
+ @me ||= Vk::User.new(request('users.get').first)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Wall
6
+ # Statuses from user’s wall
7
+ # @param [Fixnum] user_id user identifier
8
+ # @param [Hash] options
9
+ # @option options [Fixnum] :count how many statuses to request
10
+ # @option options [Fixnum] :offset offset of statuses to request
11
+ # @option options [:owner, :others, :all] :filter (:all) what kind of statuses to request
12
+ # @return [Array<Fixnum, *Hash>] count of statuses and each status in hash
13
+ def get_wall(user_id, options = {})
14
+ options[:filter] ||= :all
15
+ options[:owner_id] = user_id
16
+ Vk::Result.new('groups.get', Vk::Post, options)
17
+ end
18
+
19
+ def get_wall_statuses(posts)
20
+ request('wall.getById', posts: posts)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+ require 'vk'
3
+
4
+ module Vk
5
+ # Class for requesting vk.com api data
6
+ # @author Alexander Semyonov
7
+ class Error < StandardError
8
+ def initialize(msg, details = {})
9
+ if msg.is_a?(Hash)
10
+ details, msg = msg, msg['error']['error_msg']
11
+ end
12
+ super(msg)
13
+ @details = details
14
+ end
15
+
16
+ attr_reader :details
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module Vk
2
+ class Error < StandardError
3
+ end
4
+
5
+ class TooMuchArguments < Error
6
+ def initialize(method, argument, count)
7
+ super("Argument #{argument.inspect} of method #{method.inspect} cannot contain more than #{count} values")
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Group < Base
5
+ self.fields = [
6
+ :id, :name, :screen_name, :is_closed, :type, :is_admin, :admin_level, :is_member,
7
+ :photo_50, :photo_100, :photo_200,
8
+ :city, :country, :place, :description, :wiki_page, :members_count, :counters,
9
+ :start_date, :finish_date, :can_post, :can_see_all_posts, :activity, :status,
10
+ :contacts, :links, :fixed_post, :verified, :site
11
+ ]
12
+
13
+ def to_s
14
+ name
15
+ end
16
+
17
+ # @return [Time]
18
+ def start_at
19
+ Time.at(start_date) if start_date
20
+ end
21
+
22
+ # @return [Time]
23
+ def finish_at
24
+ Time.at(finish_date) if finish_date
25
+ end
26
+
27
+ # @return [Vk::Album]
28
+ def albums
29
+ @albums ||= loader.get_albums("-#{id}").all
30
+ end
31
+
32
+ # @return [Vk::Audio::Album]
33
+ def audio_albums
34
+ @audio_albums ||= loader.get_audio_albums("-#{id}").all
35
+ end
36
+
37
+ def get_wall(options = {})
38
+ loader.get_wall("-#{id}", options)
39
+ end
40
+
41
+ protected
42
+
43
+ def load_data(options = {fields: self.class.fields})
44
+ @attributes = @attributes.merge(loader.get_group_by_id(id, options)) unless @attributes.size > 1
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Photo < Base
5
+ self.fields = [
6
+ :id, :album_id, :owner_id, :user_id, :photo_75, :photo_130, :photo_604, :photo_807, :photo_1280, :photo_2560,
7
+ :width, :height, :text, :date
8
+ ]
9
+
10
+ def uploaded_at
11
+ @uploaded_at ||= Time.at(read_attribute(:date))
12
+ end
13
+
14
+ def to_s
15
+ text.to_s
16
+ end
17
+
18
+ # @return [75, 130, 604, 807, 1280, 2560] maximum available picture size
19
+ def max_photo_size
20
+ @max_photo_size ||=
21
+ attributes.keys.grep(/photo_/).inject(75) do |maximum, size|
22
+ real_size = size.to_s.gsub(/\Aphoto_/, '').to_i
23
+ real_size > maximum ? real_size : maximum
24
+ end
25
+ end
26
+
27
+ # @return [String] url to photo with maximum available size
28
+ def max_photo
29
+ public_send("photo_#{max_photo_size}")
30
+ end
31
+
32
+ protected
33
+
34
+ def load_data(options = {})
35
+ @attributes = @attributes.merge(loader.get_group_by_id(id, options)) unless @attributes.size > 1
36
+ end
37
+ end
38
+ end
@@ -36,34 +36,28 @@ module Vk
36
36
  end
37
37
 
38
38
  def post_id
39
- "#{to_id}_#{id}"
39
+ @post_id ||= "#{to_id}_#{id}"
40
40
  end
41
- memoize :post_id
42
41
 
43
42
  def to
44
- Vk::User.find(read_attribute(:to_id))
43
+ @to ||= Vk::User.find(read_attribute(:to_id))
45
44
  end
46
- memoize :to
47
45
 
48
46
  def from
49
- Vk::User.find(read_attribute(:from_id))
47
+ @from ||= Vk::User.find(read_attribute(:from_id))
50
48
  end
51
- memoize :from
52
49
 
53
50
  def date
54
- Time.at(read_attribute(:date))
51
+ @date ||= Time.at(read_attribute(:date))
55
52
  end
56
- memoize :date
57
53
 
58
54
  def copy_owner
59
- Vk::User.find(read_attribute(:copy_owner_id))
55
+ @copy_owner ||= Vk::User.find(read_attribute(:copy_owner_id))
60
56
  end
61
- memoize :copy_owner
62
57
 
63
58
  def copy_post
64
- Vk::Post.find(read_attribute(:copy_owner_id))
59
+ @copy_post ||= Vk::Post.find(read_attribute(:copy_owner_id))
65
60
  end
66
- memoize :copy_post
67
61
 
68
62
  def comments_count
69
63
  read_attribute(:comments)['count']