vk 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +4 -0
- data/README.markdown +8 -1
- data/lib/vk.rb +15 -1
- data/lib/vk/base.rb +8 -2
- data/lib/vk/dsl.rb +9 -0
- data/lib/vk/post.rb +86 -0
- data/lib/vk/post/wall.rb +67 -0
- data/lib/vk/request.rb +2 -1
- data/lib/vk/user.rb +22 -3
- data/lib/vk/version.rb +1 -1
- metadata +6 -3
data/.yardopts
ADDED
data/README.markdown
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Vk is a wrapper for vk.com API
|
4
4
|
|
5
|
+
* [Documentation](http://rubydoc.info/gems/vk)
|
6
|
+
* [Source](http://github.com/alsemyonov/vk)
|
7
|
+
* [Issues](http://github.com/alsemyonov/vk/issues)
|
8
|
+
* [Wiki](http://github.com/alsemyonov/vk/wiki)
|
9
|
+
* [Documentation for unreleased gem version](http://rubydoc.info/github/alsemyonov/vk)
|
10
|
+
|
5
11
|
## Installation
|
6
12
|
|
7
13
|
$ gem install vk
|
@@ -32,7 +38,7 @@ View methods list at {Vk::DSL}.
|
|
32
38
|
|
33
39
|
### Usage with object oriented DSL
|
34
40
|
|
35
|
-
Currently implemented classes: {Vk::User}, {Vk::City}, {Vk::Country}.
|
41
|
+
Currently implemented classes: {Vk::User}, {Vk::City}, {Vk::Country}, {Vk::Post}.
|
36
42
|
|
37
43
|
user = Vk::User.find 12345
|
38
44
|
user # #<Vk::User:12345 @attributes={first_name: 'Ivan', last_name: 'Ivanov', uid: 12345}>
|
@@ -43,6 +49,7 @@ Currently implemented classes: {Vk::User}, {Vk::City}, {Vk::Country}.
|
|
43
49
|
user.country # #<Vk::Country:1 @attributes={name: 'Russia', cid: 1}>
|
44
50
|
user.friends # [#<Vk::User:1 @attributes={first_name: "Pavel", last_name: "Durov", uid: 1}>, ...]
|
45
51
|
user.friends.first.city # #<Vk::City:1 @attributes={name: 'Moscow', cid: 1}>
|
52
|
+
user.posts.first # #<Vk::Post:1_123 @attributes={text: 'Preved!11'}>
|
46
53
|
|
47
54
|
## License
|
48
55
|
|
data/lib/vk.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/object/try'
|
2
|
+
|
1
3
|
module Vk
|
2
4
|
extend self
|
3
5
|
|
@@ -8,16 +10,28 @@ module Vk
|
|
8
10
|
autoload :User, 'vk/user'
|
9
11
|
autoload :City, 'vk/city'
|
10
12
|
autoload :Country, 'vk/country'
|
13
|
+
autoload :Post, 'vk/post'
|
11
14
|
|
12
15
|
class << self
|
13
|
-
attr_accessor :app_id, :app_secret
|
16
|
+
attr_accessor :app_id, :app_secret, :logger
|
14
17
|
end
|
15
18
|
|
19
|
+
# Request to vk.com API
|
20
|
+
# @return [Vk::Request] Request object
|
16
21
|
def request
|
17
22
|
@request ||= Request.new
|
18
23
|
end
|
19
24
|
|
25
|
+
def log(text, severity = :debug)
|
26
|
+
Vk.logger.try(severity, text)
|
27
|
+
end
|
28
|
+
|
20
29
|
def dsl!
|
21
30
|
Request.dsl!
|
22
31
|
end
|
32
|
+
|
33
|
+
def log!
|
34
|
+
require 'logger'
|
35
|
+
self.logger = Logger.new STDOUT
|
36
|
+
end
|
23
37
|
end
|
data/lib/vk/base.rb
CHANGED
@@ -3,11 +3,14 @@ require 'vk'
|
|
3
3
|
require 'active_support/core_ext/class/attribute'
|
4
4
|
require 'active_support/core_ext/array/extract_options'
|
5
5
|
require 'active_support/hash_with_indifferent_access'
|
6
|
+
require 'active_support/memoizable'
|
6
7
|
|
7
8
|
module Vk
|
8
9
|
dsl! # require DSL methods in Vk::Request
|
9
10
|
|
10
11
|
class Base
|
12
|
+
extend ActiveSupport::Memoizable
|
13
|
+
|
11
14
|
class_attribute :identity_map, :loader, :key_field, :fields
|
12
15
|
self.loader = Vk.request
|
13
16
|
|
@@ -26,6 +29,7 @@ module Vk
|
|
26
29
|
end
|
27
30
|
end
|
28
31
|
end
|
32
|
+
alias [] find
|
29
33
|
|
30
34
|
def method_missing(method, *args)
|
31
35
|
if identity_map.respond_to?(method)
|
@@ -59,7 +63,7 @@ module Vk
|
|
59
63
|
end
|
60
64
|
|
61
65
|
def id
|
62
|
-
@attributes[key_field]
|
66
|
+
@attributes[key_field].to_i
|
63
67
|
end
|
64
68
|
|
65
69
|
def read_attribute(name)
|
@@ -71,7 +75,9 @@ module Vk
|
|
71
75
|
|
72
76
|
def method_missing(method, *args)
|
73
77
|
if @attributes.key?(method)
|
74
|
-
@attributes[method
|
78
|
+
@attributes[method]
|
79
|
+
elsif self.class.fields.include?(name.to_sym)
|
80
|
+
read_attribute(name)
|
75
81
|
else
|
76
82
|
super
|
77
83
|
end
|
data/lib/vk/dsl.rb
CHANGED
@@ -80,7 +80,16 @@ module Vk
|
|
80
80
|
# @option options [:owner, :others, :all] :filter (:all) what kind of statuses to request
|
81
81
|
# @return [Array<Fixnum, *Hash>] count of statuses and each status in hash
|
82
82
|
def get_wall(uid, options = {})
|
83
|
+
options[:filter] ||= :all
|
83
84
|
request('wall.get', options.merge(owner_id: uid))
|
84
85
|
end
|
86
|
+
|
87
|
+
def get_wall_statuses(posts)
|
88
|
+
request('wall.getById', posts: posts)
|
89
|
+
end
|
90
|
+
|
91
|
+
def get_wall_status(id)
|
92
|
+
get_wall_statuses(id)[0]
|
93
|
+
end
|
85
94
|
end
|
86
95
|
end
|
data/lib/vk/post.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'vk/base'
|
2
|
+
|
3
|
+
module Vk
|
4
|
+
class Post < Base
|
5
|
+
autoload :Wall, 'vk/post/wall'
|
6
|
+
|
7
|
+
self.key_field = :post_id
|
8
|
+
self.fields = [:id, :from_id, :to_id, :date, :text, :attachment, :comments, :likes, :copy_owner_id, :copy_post_id]
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def find(*ids)
|
12
|
+
options = ids.extract_options!
|
13
|
+
if ids.count == 1
|
14
|
+
id = ids.first
|
15
|
+
identity_map[id] ||= new(id, options)
|
16
|
+
else
|
17
|
+
loaded_ids = ids & identity_map.keys
|
18
|
+
ids_to_load = ids - loaded_ids
|
19
|
+
identity_map.values_at(*loaded_ids).tap do |result|
|
20
|
+
if ids_to_load.any?
|
21
|
+
result << loader.get_wall_statuses(ids_to_load).map do |post|
|
22
|
+
new(id_for(post), data: post)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def id_for(post)
|
30
|
+
"#{post['to_id']}_#{post['id']}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def id
|
35
|
+
@attributes[key_field]
|
36
|
+
end
|
37
|
+
|
38
|
+
def post_id
|
39
|
+
"#{to_id}_#{id}"
|
40
|
+
end
|
41
|
+
memoize :post_id
|
42
|
+
|
43
|
+
def to
|
44
|
+
Vk::User.find(read_attribute(:to_id))
|
45
|
+
end
|
46
|
+
memoize :to
|
47
|
+
|
48
|
+
def from
|
49
|
+
Vk::User.find(read_attribute(:from_id))
|
50
|
+
end
|
51
|
+
memoize :from
|
52
|
+
|
53
|
+
def date
|
54
|
+
Time.at(read_attribute(:date))
|
55
|
+
end
|
56
|
+
memoize :date
|
57
|
+
|
58
|
+
def copy_owner
|
59
|
+
Vk::User.find(read_attribute(:copy_owner_id))
|
60
|
+
end
|
61
|
+
memoize :copy_owner
|
62
|
+
|
63
|
+
def copy_post
|
64
|
+
Vk::Post.find(read_attribute(:copy_owner_id))
|
65
|
+
end
|
66
|
+
memoize :copy_post
|
67
|
+
|
68
|
+
def comments_count
|
69
|
+
read_attribute(:comments)['count']
|
70
|
+
end
|
71
|
+
|
72
|
+
def likes_count
|
73
|
+
read_attribute(:likes)['count']
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
text
|
78
|
+
end
|
79
|
+
|
80
|
+
protected
|
81
|
+
|
82
|
+
def load_data(options = {})
|
83
|
+
@attributes = @attributes.merge(loader.get_wall_status(id))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/vk/post/wall.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'vk/post'
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
|
5
|
+
module Vk
|
6
|
+
class Post
|
7
|
+
class Wall
|
8
|
+
extend ActiveSupport::Memoizable
|
9
|
+
|
10
|
+
attr_accessor :uid, :count, :posts
|
11
|
+
|
12
|
+
def initialize(uid, count, posts)
|
13
|
+
self.uid, self.count, self.posts = uid, count, posts
|
14
|
+
end
|
15
|
+
|
16
|
+
def user
|
17
|
+
Vk::User.find(uid)
|
18
|
+
end
|
19
|
+
memoize :user
|
20
|
+
|
21
|
+
def [](index)
|
22
|
+
raise "Post #{index} is not exist" if index > count + 1
|
23
|
+
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
|
27
|
+
end
|
28
|
+
|
29
|
+
def first; self[0]; end
|
30
|
+
|
31
|
+
def each(&block)
|
32
|
+
load_all_posts
|
33
|
+
0.upto(count) do |index|
|
34
|
+
block.call(self[index])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(method, *args)
|
39
|
+
if posts.respond_to?(method)
|
40
|
+
posts.send(method, *args)
|
41
|
+
else
|
42
|
+
super
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def load_post(index)
|
49
|
+
count, *new_posts = Vk::Base.loader.get_wall(uid, offset: loaded_posts, count: index - loaded_posts + 1)
|
50
|
+
self.posts += new_posts
|
51
|
+
posts[index]
|
52
|
+
end
|
53
|
+
|
54
|
+
def load_posts_to(count)
|
55
|
+
load_post(loaded_posts + 100) while loaded_posts < count
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_all_posts
|
59
|
+
load_posts_to(count)
|
60
|
+
end
|
61
|
+
|
62
|
+
def loaded_posts
|
63
|
+
posts.size
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/vk/request.rb
CHANGED
@@ -42,10 +42,11 @@ module Vk
|
|
42
42
|
method: method_name,
|
43
43
|
v: VERSION
|
44
44
|
)
|
45
|
+
Vk.log(data)
|
45
46
|
url = URI.parse("#{SCHEME}://#{HOST}:#{PORT}#{PATH}?#{data.to_query}&sig=#{signature(data)}")
|
46
47
|
http_response = Net::HTTP.get_response(url).body
|
47
48
|
json_response = JSON.parse(http_response)
|
48
|
-
|
49
|
+
Vk.log(json_response)
|
49
50
|
json_response['response']
|
50
51
|
end
|
51
52
|
|
data/lib/vk/user.rb
CHANGED
@@ -11,7 +11,7 @@ module Vk
|
|
11
11
|
ids_to_load = ids - loaded_ids
|
12
12
|
identity_map.values_at(*loaded_ids).tap do |results|
|
13
13
|
if ids_to_load.any?
|
14
|
-
results
|
14
|
+
results += loader.get_profiles(ids_to_load, options).map do |profile|
|
15
15
|
new(profile['uid'], data: profile)
|
16
16
|
end
|
17
17
|
end
|
@@ -19,9 +19,12 @@ module Vk
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
attr_accessor :posts_count
|
23
|
+
|
22
24
|
def name
|
23
25
|
"#{first_name} #{last_name}"
|
24
26
|
end
|
27
|
+
memoize :name
|
25
28
|
|
26
29
|
def city_id
|
27
30
|
read_attribute(:city)
|
@@ -30,6 +33,7 @@ module Vk
|
|
30
33
|
def city
|
31
34
|
Vk::City.find(city_id)
|
32
35
|
end
|
36
|
+
memoize :city
|
33
37
|
|
34
38
|
def country_id
|
35
39
|
read_attribute(:country)
|
@@ -38,20 +42,35 @@ module Vk
|
|
38
42
|
def country
|
39
43
|
Vk::Country.find(country_id)
|
40
44
|
end
|
45
|
+
memoize :country
|
41
46
|
|
42
47
|
def friend_ids
|
43
|
-
|
48
|
+
loader.get_friends(uid)
|
44
49
|
end
|
50
|
+
memoize :friend_ids
|
45
51
|
|
46
52
|
def friends(options = {})
|
47
|
-
|
53
|
+
User.find_all(friend_ids, options)
|
48
54
|
end
|
55
|
+
memoize :friends
|
49
56
|
|
50
57
|
def to_s
|
51
58
|
name
|
52
59
|
end
|
53
60
|
|
61
|
+
def wall(options = {})
|
62
|
+
count, *posts = loader.get_wall(id, options)
|
63
|
+
Vk::Post::Wall.new(id, count, posts)
|
64
|
+
end
|
65
|
+
memoize :wall
|
66
|
+
alias posts wall
|
67
|
+
|
68
|
+
def posts_count
|
69
|
+
wall.count
|
70
|
+
end
|
71
|
+
|
54
72
|
protected
|
73
|
+
|
55
74
|
def load_data(options = {})
|
56
75
|
@attributes = @attributes.merge(loader.get_profile(id, options))
|
57
76
|
end
|
data/lib/vk/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Alexander Semyonov
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-05 00:00:00 +06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -87,6 +87,7 @@ extra_rdoc_files: []
|
|
87
87
|
|
88
88
|
files:
|
89
89
|
- .gitignore
|
90
|
+
- .yardopts
|
90
91
|
- Gemfile
|
91
92
|
- README.markdown
|
92
93
|
- Rakefile
|
@@ -95,6 +96,8 @@ files:
|
|
95
96
|
- lib/vk/city.rb
|
96
97
|
- lib/vk/country.rb
|
97
98
|
- lib/vk/dsl.rb
|
99
|
+
- lib/vk/post.rb
|
100
|
+
- lib/vk/post/wall.rb
|
98
101
|
- lib/vk/request.rb
|
99
102
|
- lib/vk/user.rb
|
100
103
|
- lib/vk/version.rb
|