class SnpVkParser::Group

Class for work with group and user in group.

Attributes

album_ids[RW]
id[RW]
post_ids[RW]
token[RW]
user_id[RW]
vk_client[R]

Public Class Methods

new(params = {}) click to toggle source

Initialize.

# File lib/snp_vk_parser/group.rb, line 10
def initialize(params = {})
  params.each { |key, value| instance_variable_set("@#{key}", value) }
end

Public Instance Methods

albums_user_photo_count() click to toggle source

Count user photos in albums. @return [Integer] Ammount of photos.

# File lib/snp_vk_parser/group.rb, line 58
def albums_user_photo_count
  count = 0
  albums.each do |a|
    vk_client.photos.get(owner_id: a.owner_id, album_id: a.aid).each do |p|
      count += 1 if p.user_id == user_id
    end
  end
  count
end
member?() click to toggle source

Check user is member. @return [Boolean]

# File lib/snp_vk_parser/group.rb, line 70
def member?
  vk_client.groups.is_member(group_id: id.abs, user_id: user_id) == 1
end
members() click to toggle source

Members ids. @return [Array] Array of user ids.

# File lib/snp_vk_parser/group.rb, line 76
def members
  vk_client.groups.get_members(group_id: id.abs).users
end
posts_user_comment_count() click to toggle source

Count user comments in posts. @return [Integer] Ammount of comments.

# File lib/snp_vk_parser/group.rb, line 21
def posts_user_comment_count
  count = 0
  posts.map do |p|
    vk_client.wall.get_comments(owner_id: id, post_id: p.id).each_with_index do |comment, index|
      next if index == 0
      count += 1 if comment.from_id == user_id
    end
  end
  count
end
posts_user_like_count() click to toggle source

Count user likes in posts. @return [Integer] Ammount of likes.

# File lib/snp_vk_parser/group.rb, line 34
def posts_user_like_count
  count = 0
  posts.map do |p|
    vk_client.likes.get_list(owner_id: id, type: 'post', item_id: p.id).users.each do |u|
      count += 1 if u == user_id
    end
  end
  count
end
posts_user_repost_count() click to toggle source

Count user reposts in posts. @return [Integer] Ammount of reposts.

# File lib/snp_vk_parser/group.rb, line 46
def posts_user_repost_count
  count = 0
  posts.map do |p|
    vk_client.wall.get_reposts(owner_id: id, post_id: p.id).profiles.each do |u|
      count += 1 if u.uid == user_id
    end
  end
  count
end

Private Instance Methods

albums() click to toggle source

Get albums. @return [Array] Array of albums.

# File lib/snp_vk_parser/group.rb, line 102
def albums
  if album_ids.respond_to?(:each) && album_ids.any?
    vk_client.photos.get_albums(owner_id: id, album_ids: album_ids.join(','))
  else
    vk_client.photos.get_albums(owner_id: id)
  end
end
post_ids_string() click to toggle source

Convert post ids to string. @return [String] Post ids with owner.

# File lib/snp_vk_parser/group.rb, line 96
def post_ids_string
  post_ids.map { |p| "#{id}_#{p}" }.join ','
end
posts() click to toggle source

Get posts. @return [Array] Array of posts.

# File lib/snp_vk_parser/group.rb, line 84
def posts
  if post_ids.respond_to?(:each) && post_ids.any?
    posts = vk_client.wall.getById(posts: post_ids_string)
  else
    posts = vk_client.wall.get(owner_id: id)
    posts.shift
  end
  posts
end