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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5cf6cc3967a10795bac86e867d2f9572af40035
4
+ data.tar.gz: bad1ca740033624204e839e8b3519116349b7726
5
+ SHA512:
6
+ metadata.gz: 48dd1970eeb9062d32c9824b102d15d4f259e9457b91468223ba36f17df6c81135dc713b59172f0e9faa4d4140cacfee08f95583bf70ce4346664106377715a1
7
+ data.tar.gz: 3da26495cbd61650bc26041b8a50b7409ab70fc43bd2a52a75c04744a654525859ecaa211b738b634108202b8580a5fc6f3256050d6fced2e0aa812e4b59cea2
data/.gitignore CHANGED
@@ -3,3 +3,7 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  doc
6
+ .ruby-version
7
+ .env
8
+ .env.*
9
+ /backups
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.0
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in vk.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'omniauth-vkontakte'
8
+ end
@@ -2,55 +2,68 @@
2
2
 
3
3
  Vk is a wrapper for vk.com API
4
4
 
5
+ ## Project links
6
+
5
7
  * [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)
8
+ * [Source](https://github.com/alsemyonov/vk)
9
+ * [Issues](https://github.com/alsemyonov/vk/issues)
10
+ * [Wiki](https://github.com/alsemyonov/vk/wiki)
11
+ * [![Code Climate](https://codeclimate.com/github/alsemyonov/vk.png)](https://codeclimate.com/github/alsemyonov/vk)
12
+ * [![Build Status](https://travis-ci.org/alsemyonov/vk.png?branch=master)](https://travis-ci.org/alsemyonov/vk)
10
13
 
11
14
  ## Installation
12
15
 
13
- $ gem install vk
16
+ ```bash
17
+ gem install vk
18
+ ```
14
19
 
15
20
  ## Usage
16
21
 
17
22
  ### Initialization
18
23
 
19
- Vk.app_id = 12345
20
- Vk.app_secret = 'secret'
24
+ ```ruby
25
+ Vk.app_id = 12345
26
+ Vk.app_secret = 'secret'
27
+ ```
21
28
 
22
29
  ### Basic usage
23
30
 
24
31
  Simple request. Docs ad {Vk::Request}
25
32
 
26
- vk = Vk.request
27
- profiles = vk.request 'getProfiles', uids: 12345
28
- puts profiles # [{uid: 12345, first_name: 'Ivan', last_name: 'Ivanov'}]
33
+ ```ruby
34
+ vk = Vk.request
35
+ profiles = vk.request 'getProfiles', uids: 12345
36
+ puts profiles # [{uid: 12345, first_name: 'Ivan', last_name: 'Ivanov'}]
37
+ ```
29
38
 
30
39
  ### Usage with DSL
31
40
 
32
41
  View methods list at {Vk::DSL}.
33
42
 
34
- Vk.dsl!
35
- vk = Vk.request
36
- profiles = vk.get_profiles([123, 456, 789], fields: %w('has_mobile'))
37
- puts profiles # [{uid: 1234, ..., has_mobile: 1}]
43
+ ```ruby
44
+ Vk.dsl!
45
+ vk = Vk.request
46
+ profiles = vk.get_profiles([123, 456, 789], fields: %w('has_mobile'))
47
+ puts profiles # [{uid: 1234, ..., has_mobile: 1}]
48
+ ```
38
49
 
39
50
  ### Usage with object oriented DSL
40
51
 
41
52
  Currently implemented classes: {Vk::User}, {Vk::City}, {Vk::Country}, {Vk::Post}.
42
53
 
43
- user = Vk::User.find 12345
44
- user # #<Vk::User:12345 @attributes={first_name: 'Ivan', last_name: 'Ivanov', uid: 12345}>
45
- user.first_name # 'Ivan'
46
- user.last_name # 'Ivanov'
47
- user.name # 'Ivan Ivanov'
48
- user.city # #<Vk::City:1 @attributes={name: 'Moscow', cid: 1}>
49
- user.country # #<Vk::Country:1 @attributes={name: 'Russia', cid: 1}>
50
- user.friends # [#<Vk::User:1 @attributes={first_name: "Pavel", last_name: "Durov", uid: 1}>, ...]
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'}>
54
+ ```ruby
55
+ user = Vk::User.find 12345
56
+ user # => #<Vk::User:12345 @attributes={first_name: 'Ivan', last_name: 'Ivanov', uid: 12345}>
57
+ user.first_name # => 'Ivan'
58
+ user.last_name # => 'Ivanov'
59
+ user.name # => 'Ivan Ivanov'
60
+ user.city # => #<Vk::City:1 @attributes={name: 'Moscow', cid: 1}>
61
+ user.country # => #<Vk::Country:1 @attributes={name: 'Russia', cid: 1}>
62
+ user.friends # => [#<Vk::User:1 @attributes={first_name: "Pavel", last_name: "Durov", uid: 1}>, ...]
63
+ user.friends.first.city # => #<Vk::City:1 @attributes={name: 'Moscow', cid: 1}>
64
+ user.posts.first # => #<Vk::Post:1_123 @attributes={text: 'Preved!11'}>
65
+ ```
53
66
 
54
67
  ## License
55
68
 
56
- MIT License. © [Alexander Semyonov](http://al.semyonov.us/), <al@semyonov.us>, 2011
69
+ MIT License. © [Alex Semyonov](http://al.semyonov.us/), <alex@semyonov.us>, 2011-2016
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require 'bundler'
1
+ require 'bundler/setup'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
4
  begin
@@ -26,3 +26,5 @@ rescue LoadError
26
26
  abort 'install yard to generate documentation ($ bundle install)'
27
27
  end
28
28
  end
29
+
30
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ require 'bundler/setup'
4
+ begin
5
+ require 'dotenv'
6
+ Dotenv.load('.env')
7
+ rescue LoadError
8
+ end
9
+ require 'vk'
10
+ require 'vk/dsl'
11
+
12
+ require 'pry'
13
+ begin
14
+ require 'pry-byebug'
15
+ rescue LoadError
16
+ STDERR.puts 'Byebug is enabled only in Ruby 2'
17
+ end
18
+ require 'pp'
19
+
20
+ SPEC_ROOT = File.expand_path('..', __FILE__)
21
+
22
+ Dir[File.join(SPEC_ROOT, 'spec/support/**/*.rb')].each { |f| require f }
23
+
24
+ Vk.app_id = ENV['VK_APP_ID']
25
+ Vk.app_secret = ENV['VK_APP_SECRET']
26
+ Vk.dsl!
27
+ Vk.log!
28
+
29
+ client = Vk.client
30
+ binding.pry
31
+ client
data/lib/vk.rb CHANGED
@@ -1,37 +1,61 @@
1
1
  require 'active_support/core_ext/object/try'
2
+ require 'vk/exceptions'
3
+ require 'vk/version'
2
4
 
3
5
  module Vk
4
- extend self
5
-
6
- autoload :DSL, 'vk/dsl'
7
- autoload :Request, 'vk/request'
8
-
9
- autoload :Base, 'vk/base'
10
- autoload :User, 'vk/user'
11
- autoload :City, 'vk/city'
12
- autoload :Country, 'vk/country'
13
- autoload :Post, 'vk/post'
6
+ autoload :Client, 'vk/client'
7
+ autoload :DSL, 'vk/dsl'
8
+ autoload :Error, 'vk/error'
9
+ autoload :Result, 'vk/result'
10
+
11
+ autoload :Base, 'vk/base'
12
+ autoload :User, 'vk/user'
13
+ autoload :City, 'vk/city'
14
+ autoload :Country, 'vk/country'
15
+ autoload :Post, 'vk/post'
16
+ autoload :Stats, 'vk/stats'
17
+ autoload :Group, 'vk/group'
18
+ autoload :Album, 'vk/album'
14
19
 
15
20
  class << self
16
- attr_accessor :app_id, :app_secret, :logger
21
+ # @return [String]
22
+ attr_accessor :app_id
23
+ # @return [String]
24
+ attr_accessor :app_secret
17
25
  end
18
26
 
19
- # Request to vk.com API
20
- # @return [Vk::Request] Request object
21
- def request
22
- @request ||= Request.new
27
+ # @return [Logger]
28
+ def self.logger
29
+ @logger ||=
30
+ begin
31
+ require 'logger'
32
+ Logger.new(STDOUT)
33
+ end
34
+ end
35
+
36
+ # @param [Logger] logger
37
+ # @return [Logger]
38
+ def self.logger=(logger)
39
+ @logger = logger
23
40
  end
24
41
 
25
- def log(text, severity = :debug)
26
- Vk.logger.try(severity, text)
42
+ module_function
43
+
44
+ # Request to vk.com API
45
+ # @return [Vk::Client] Request object
46
+ def client(access_token = ENV['VK_ACCESS_TOKEN'])
47
+ @client ||= Client.new(access_token)
27
48
  end
28
49
 
50
+ # @return [Vk::Client]
29
51
  def dsl!
30
- Request.dsl!
52
+ Client.dsl!
31
53
  end
32
54
 
33
55
  def log!
34
56
  require 'logger'
35
57
  self.logger = Logger.new STDOUT
36
58
  end
59
+
60
+ dsl!
37
61
  end
@@ -0,0 +1,28 @@
1
+ require 'vk'
2
+
3
+ module Vk
4
+ module Access
5
+ SCOPES = {
6
+ :notify => 1, # Пользователь разрешил отправлять ему уведомления (для flash/iframe-приложений).
7
+ :friends => 2, # Доступ к друзьям.
8
+ :photos => 4, # Доступ к фотографиям.
9
+ :audio => 8, # Доступ к аудиозаписям.
10
+ :video => 16, # Доступ к видеозаписям.
11
+ :docs => 131072, # Доступ к документам.
12
+ :notes => 2048, # Доступ к заметкам пользователя.
13
+ :pages => 128, # Доступ к wiki-страницам.
14
+ # +256 Добавление ссылки на приложение в меню слева.:status => 1024, # Доступ к статусу пользователя.
15
+ :offers => 32, # Доступ к предложениям (устаревшие методы).
16
+ :questions => 64, # Доступ к вопросам (устаревшие методы).
17
+ :wall => 8192, # Доступ к обычным и расширенным методам работы со стеной. Данное право доступа по умолчанию недоступно для сайтов при использовании OAuth-авторизации (игнорируется при попытке авторизации). : groups => 262144, # Доступ к группам пользователя.
18
+ :messages => 4096, # Доступ к расширенным методам работы с сообщениями (только для Standalone-приложений).
19
+ :email => 4194304, # Доступ к email пользователя.
20
+ :notifications => 524288, # Доступ к оповещениям об ответах пользователю.
21
+ :stats => 1048576, # Доступ к статистике групп и приложений пользователя, администратором которых он является.
22
+ :ads => 32768, # Доступ к расширенным методам работы с рекламным API.
23
+ :market => 134217728, # Доступ к товарам.
24
+ :offline => 65536, # Доступ к API в любое время (при использовании этой опции параметр expires_in, возвращаемый вместе с access_token, содержит 0 — токен бессрочный).
25
+ :nohttps => 0 # Возможность осуществлять запросы к API без HTTPS.
26
+ }
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Album < Base
5
+ self.key_field = :id
6
+ self.fields = %i[
7
+ id
8
+ thumb_id
9
+ owner_id
10
+ title description
11
+ created updated
12
+ size can_upload privacy_view privacy_comment
13
+ thumb_src
14
+ ]
15
+
16
+ def to_s
17
+ title.to_s
18
+ end
19
+
20
+ def photos(options = {})
21
+ loader.get_photos(owner_id, id, options)
22
+ end
23
+
24
+ protected
25
+
26
+ def load_data(options = {})
27
+ @attributes = @attributes.merge(loader.get_group_by_id(id, options)) unless @attributes.size > 1
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,73 @@
1
+ require 'vk/base'
2
+
3
+ module Vk
4
+ class Audio < Base
5
+ GENRES = {
6
+ 1 => 'Rock',
7
+ 2 => 'Pop',
8
+ 3 => 'Rap & Hip-Hop',
9
+ 4 => 'Easy Listening',
10
+ 5 => 'House & Dance',
11
+ 6 => 'Instrumental',
12
+ 7 => 'Metal',
13
+ 21 => 'Alternative',
14
+ 8 => 'Dubstep',
15
+ 1001 => 'Jazz & Blues',
16
+ 10 => 'Drum & Bass',
17
+ 11 => 'Trance',
18
+ 12 => 'Chanson',
19
+ 13 => 'Ethnic',
20
+ 14 => 'Acoustic & Vocal',
21
+ 15 => 'Reggae',
22
+ 16 => 'Classical',
23
+ 17 => 'Indie Pop',
24
+ 19 => 'Speech',
25
+ 22 => 'Electropop & Disco',
26
+ 18 => 'Other',
27
+ }
28
+
29
+ self.fields = %i(
30
+ id
31
+ owner_id
32
+ album_id
33
+ genre_id
34
+ lyrics_id
35
+ artist
36
+ title
37
+ duration
38
+ url
39
+ date
40
+ no_search
41
+ )
42
+
43
+ # @return [Vk::Audio::Album]
44
+ def album
45
+ album_id = read_attribute(:album_id).to_s
46
+ @album ||= owner.audio_albums.detect { |album| album.id.to_s == album_id }
47
+ end
48
+
49
+ # @return [Vk::User]
50
+ def owner
51
+ @owner ||= Vk::User.find(owner_id)
52
+ end
53
+
54
+ # @return [String]
55
+ def genre
56
+ GENRES[genre_id]
57
+ end
58
+
59
+ # @return [String]
60
+ def lyrics
61
+ @lyrics ||= loader.get_lyrics(lyrics_id).text if lyrics_id
62
+ end
63
+
64
+ # @param [String] genre
65
+ # @return [Integer]
66
+ def genre=(genre)
67
+ self.genre_id = GENRES.key(genre)
68
+ end
69
+ end
70
+ end
71
+
72
+ require 'vk/audio/album'
73
+ require 'vk/audio/lyrics'
@@ -0,0 +1,9 @@
1
+ require 'vk/audio'
2
+
3
+ module Vk
4
+ class Audio < Base
5
+ class Album < Base
6
+ self.fields = %i[id owner_id title]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'vk/audio'
2
+
3
+ module Vk
4
+ class Audio < Base
5
+ class Lyrics < Base
6
+ self.key_field = :lyrics_id
7
+ self.fields = %i[lyrics_id text]
8
+ end
9
+ end
10
+ end
@@ -2,87 +2,120 @@ require 'vk'
2
2
 
3
3
  require 'active_support/core_ext/class/attribute'
4
4
  require 'active_support/core_ext/array/extract_options'
5
+ require 'active_support/core_ext/hash/indifferent_access'
5
6
  require 'active_support/hash_with_indifferent_access'
6
- require 'active_support/memoizable'
7
7
 
8
8
  module Vk
9
9
  dsl! # require DSL methods in Vk::Request
10
10
 
11
11
  class Base
12
- extend ActiveSupport::Memoizable
13
-
14
- class_attribute :identity_map, :loader, :key_field, :fields
15
- self.loader = Vk.request
16
-
17
- class << self
18
- # Find object in identity map or initialize object
19
- def find(*ids)
20
- options = ids.extract_options!
21
- if ids.count == 1
22
- id = ids.first.to_i
23
- identity_map[id] ||= new(id, options)
24
- elsif respond_to?(:find_all)
25
- find_all(ids, options)
26
- else
27
- ids.map do |id|
28
- find(id, options)
29
- end
12
+ # @return [{Integer => self}]
13
+ class_attribute :identity_map
14
+
15
+ # @return [Vk::Client]
16
+ class_attribute :loader
17
+
18
+ # @return [Symbol]
19
+ class_attribute :key_field
20
+
21
+ # @return [<Symbol>]
22
+ class_attribute :fields
23
+
24
+ # Find object in identity map or initialize object
25
+ # @param [self, <self>] ids
26
+ def self.find(*ids)
27
+ options = ids.extract_options!
28
+ if ids.count == 1
29
+ id = ids.first.to_i
30
+ identity_map[id] ||= new(id, options)
31
+ elsif respond_to?(:find_all)
32
+ find_all(ids, options)
33
+ else
34
+ ids.map do |id|
35
+ find(id, options)
30
36
  end
31
37
  end
32
- alias [] find
38
+ end
33
39
 
34
- def method_missing(method, *args)
35
- if identity_map.respond_to?(method)
36
- identity_map.send(method, *args)
37
- else
38
- super
39
- end
40
- end
40
+ instance_eval { alias [] find }
41
41
 
42
- def inherited(subclass)
43
- subclass.identity_map = {}
44
- subclass.fields = []
42
+ def self.method_missing(method, *args)
43
+ if identity_map.respond_to?(method)
44
+ identity_map.send(method, *args)
45
+ else
46
+ super
45
47
  end
46
48
  end
47
49
 
50
+ def self.inherited(subclass)
51
+ super(subclass)
52
+ subclass.identity_map = {}
53
+ subclass.fields = []
54
+ subclass.key_field = :id
55
+ subclass.fields = []
56
+ subclass.loader = Vk.client
57
+ end
58
+
48
59
  def initialize(id, options = {})
49
60
  @attributes = ActiveSupport::HashWithIndifferentAccess.new
50
61
 
62
+ if id.is_a?(Hash)
63
+ options[:data] = id
64
+ id = options[:data][key_field.to_s]
65
+ end
66
+
51
67
  self.id = id
52
68
  self.class.identity_map[id] = self
53
69
 
54
70
  if options.key? :data
55
- @attributes.merge!(options[:data])
71
+ @attributes.merge!(options[:data].with_indifferent_access)
56
72
  else
57
73
  load_data(options)
58
74
  end
59
75
  end
60
76
 
77
+ attr_reader :attributes
78
+
61
79
  def id=(id)
62
80
  @attributes[key_field] = id
63
81
  end
64
82
 
83
+ # @return [String]
65
84
  def id
66
- @attributes[key_field].to_i
85
+ @attributes[key_field].to_s
67
86
  end
68
87
 
69
88
  def read_attribute(name)
70
- if !@attributes.key?(name) && fields.include?(name.to_sym)
89
+ if !@attributes.key?(name) && self.class.fields.include?(name.to_sym)
71
90
  load_data(fields: name)
72
91
  end
73
92
  @attributes[name]
74
93
  end
75
94
 
76
95
  def method_missing(method, *args)
77
- if @attributes.key?(method)
96
+ if method =~ /\?\Z/
97
+ respond_to_missing?(method) && @attributes[method]
98
+ elsif @attributes.key?(method)
78
99
  @attributes[method]
79
- elsif self.class.fields.include?(name.to_sym)
80
- read_attribute(name)
100
+ elsif self.class.fields.include?(method.to_sym)
101
+ read_attribute(method.to_s)
81
102
  else
82
103
  super
83
104
  end
84
105
  end
85
106
 
107
+ def respond_to_missing?(method, include_all = false)
108
+ @attributes.key?(method) || self.class.fields.include?(method.to_sym) || super(method, include_all)
109
+ end
110
+
111
+ def logger
112
+ Vk.logger
113
+ end
114
+
115
+ def to_hash
116
+ attributes.to_hash
117
+ end
118
+
86
119
  def inspect
87
120
  "#<#{self.class.name}:#{id} @attributes=#{@attributes.inspect}>"
88
121
  end
@@ -90,7 +123,7 @@ module Vk
90
123
  protected
91
124
 
92
125
  def load_data(options = {})
93
- raise 'Not implemented. Use subclasses'
126
+ # raise 'Not implemented. Use subclasses'
94
127
  end
95
128
  end
96
129
  end