vk 0.0.4 → 0.6.5.53

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,17 +2,27 @@ require 'vk/base'
2
2
 
3
3
  module Vk
4
4
  class City < Base
5
- self.key_field = :cid
6
- self.fields = [:cid, :name]
5
+ self.fields = [:id, :title, :important, :country_id, :region_id, :region, :area]
7
6
 
7
+ # @return [Vk::Country, nil]
8
+ def country
9
+ @country ||= Vk::Country.find(country_id) if country_id
10
+ end
11
+
12
+ # @return [Vk::Region, nil]
13
+ def region
14
+ @region ||= Vk::Region.find(region_id) if region_id
15
+ end
16
+
17
+ # @return [String]
8
18
  def to_s
9
- name
19
+ title.to_s
10
20
  end
11
21
 
12
22
  protected
13
23
 
14
24
  def load_data(options = {})
15
- @attributes = @attributes.merge(loader.get_city(id))
25
+ @attributes = @attributes.merge(loader.get_city_by_id(id)) if id
16
26
  end
17
27
  end
18
28
  end
@@ -0,0 +1,104 @@
1
+ # coding: utf-8
2
+ require 'vk'
3
+ require 'vk/prompt'
4
+
5
+ require 'digest/md5'
6
+ require 'uri'
7
+ require 'net/http'
8
+
9
+ require 'active_support/core_ext/object/to_query'
10
+ require 'active_support/core_ext/object/blank'
11
+ require 'json'
12
+
13
+ module Vk
14
+ # Class for requesting vk.com api data
15
+ # @author Alexander Semyonov
16
+ class Client
17
+ include Vk::PromptExtension
18
+
19
+ SCHEME = 'https'
20
+ HOST = 'api.vk.com'
21
+ PATH = '/method/'
22
+ PORT = 443
23
+
24
+ # Generates auth_key for viewer
25
+ # @param [Fixnum, String] viewer_id viewer’s identifier
26
+ def self.auth_key(viewer_id)
27
+ Digest::MD5.hexdigest("#{Vk.app_id}_#{viewer_id}_#{Vk.app_secret}")
28
+ end
29
+
30
+ def self.authenticated?(viewer_id, auth_key)
31
+ auth_key == self.auth_key(viewer_id)
32
+ end
33
+
34
+ def self.dsl!
35
+ require 'vk/dsl'
36
+ include Vk::DSL
37
+ end
38
+
39
+ # @return [Vk::Client]
40
+ def self.authenticated!
41
+ require 'vk/access'
42
+ return new(ENV['VK_ACCESS_TOKEN']) if ENV['VK_ACCESS_TOKEN']
43
+ require 'oauth2'
44
+ token = oauth_client.get_access_token # => OAuth2::AccessToken
45
+ fail 'No token discovered' unless token.try(:token)
46
+ prompt.say 'Please run following command now to prevent asking for codes again:'
47
+ prompt.say
48
+ prompt.say " export VK_ACCESS_TOKEN=#{token.token}"
49
+ prompt.say
50
+ Vk.client.access_token ||= token.token
51
+ ENV['VK_ACCESS_TOKEN'] ||= token.token
52
+ Vk.client
53
+ end
54
+
55
+ # @return [OAuth2::Client]
56
+ def self.oauth_client
57
+ require 'vk/client/oauth2'
58
+ @oauth_client ||= Vk::Client::OAuth2.new
59
+ end
60
+
61
+ # @param [#to_s] access_token
62
+ def initialize(access_token = ENV['VK_ACCESS_TOKEN'])
63
+ @access_token = access_token.to_s
64
+ end
65
+
66
+ def dsl!
67
+ self.class.dsl!
68
+ self
69
+ end
70
+
71
+ # @return [String]
72
+ attr_accessor :access_token
73
+
74
+ def request(method_name, data = {})
75
+ data = data.merge(app_id: Vk.app_id, v: Vk::VK_API)
76
+ data = data.merge(access_token: access_token) if access_token.present?
77
+ Vk.logger.info("vk.#{method_name}(#{data.inspect})")
78
+ http_response = Net::HTTP.post_form(url_for_method(method_name), data).body
79
+ return unless http_response.present?
80
+ json_response = JSON.parse(http_response)
81
+ if json_response['error']
82
+ Vk.logger.error(json_response['error']['error_msg'])
83
+ Vk.logger.debug(json_response)
84
+ raise Vk::Error.new(json_response)
85
+ end
86
+ Vk.logger.debug(json_response)
87
+ json_response['response']
88
+ end
89
+
90
+ # @param [URL::HTTP] method_name
91
+ def url_for_method(method_name)
92
+ URI.parse("#{SCHEME}://#{HOST}:#{PORT}#{PATH}#{method_name}")
93
+ end
94
+
95
+ private
96
+
97
+ def signature(data)
98
+ signature = data.keys.sort.inject('') do |result, key|
99
+ result << "#{key}=#{data[key]}"
100
+ end << Vk.app_secret
101
+ Digest::MD5.hexdigest(signature)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,43 @@
1
+ require 'vk'
2
+ require 'vk/prompt'
3
+ require 'oauth2'
4
+
5
+ module Vk
6
+ class Client
7
+ class OAuth2 < ::OAuth2::Client
8
+ DEFAULT_OAUTH_OPTIONS = {
9
+ site: 'https://api.vk.com',
10
+ authorize_url: 'https://oauth.vk.com/authorize',
11
+ token_url: 'https://oauth.vk.com/access_token'
12
+ }
13
+
14
+ include Vk::PromptExtension
15
+
16
+ def initialize(app_id = Vk.app_id, app_secret = Vk.app_secret, options = DEFAULT_OAUTH_OPTIONS)
17
+ super(app_id, app_secret, options)
18
+ end
19
+
20
+ # @return [String]
21
+ def get_auth_code
22
+ url = auth_code.authorize_url(
23
+ redirect_uri: 'https://oauth.vk.com/blank.html',
24
+ display: 'page',
25
+ scope: Vk::Access::SCOPES.values.inject(0, :+)
26
+ )
27
+ prompt.say 'Open a link and allow access:'
28
+ prompt.say
29
+ prompt.say " #{url}"
30
+ prompt.say
31
+ prompt.ask('Enter the code from the address of the opened page:')
32
+ end
33
+
34
+ # @return [OAuth2::AccessToken]
35
+ def get_access_token
36
+ auth_code.get_token(
37
+ get_auth_code,
38
+ redirect_uri: 'https://oauth.vk.com/blank.html'
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
@@ -2,17 +2,22 @@ require 'vk/base'
2
2
 
3
3
  module Vk
4
4
  class Country < Base
5
- self.key_field = :cid
6
- self.fields = [:cid, :name]
5
+ self.fields = [:id, :title]
7
6
 
7
+ # @return [<Vk::Region>]
8
+ def regions
9
+ @regions ||= loader.get_regions(id).all
10
+ end
11
+
12
+ # @return [String]
8
13
  def to_s
9
- name
14
+ title.to_s
10
15
  end
11
16
 
12
17
  protected
13
18
 
14
19
  def load_data(options = {})
15
- @attributes = @attributes.merge(loader.get_country(id))
20
+ @attributes = @attributes.merge(loader.get_country_by_id(id))
16
21
  end
17
22
  end
18
23
  end
@@ -3,93 +3,25 @@ require 'vk'
3
3
 
4
4
  module Vk
5
5
  module DSL
6
- # Have user installed app?
7
- # @param [String] uid user’s identifier
8
- # @return [Boolean] does user installed app
9
- def app_user?(uid)
10
- request('isAppUser', uid: uid) == '1'
11
- end
12
-
13
- # Profile information for provided uids or domain names
14
- # @param [Array<String>] uids array of users’ identifiers in numbers or domain names
15
- # @param [Hash] options the options to request profile information
16
- # @option options [:uid, :first_name, :last_name, :nickname, :domain, :sex, :bdate, :birthdate, :city, :country, :timezone, :photo, :photo_medium, :photo_big, :has_mobile, :rate, :contacts, :education, :online] :fields ([:uid, :first_name, :last_name]) profile fields to requests
17
- # @option options [:nom, :gen, :dat, :acc, :ins, :abl] :name_case (:nom) case of returned names
18
- # @return [Array<Hash>] array of user profile data
19
- def get_profiles(uids, options = {})
20
- uids = Array(uids)
21
- if uids.first.to_i == 0
22
- options[:domains] = uids.join ','
23
- else
24
- options[:uids] = uids.join ','
25
- end
26
- options[:fields] = Array(options[:fields]).join(',') if options.key?(:fields)
27
- request('getProfiles', options)
28
- end
29
-
30
- def get_profile(uid, options = {})
31
- get_profiles(uid, options)[0]
32
- end
33
-
34
- ## Identifiers of groups in which user participates
35
- ## @param [Integer] uid user’s identifier
36
- ## @return [Array] array of group identifiers
37
- #def get_groups(uid)
38
- #request('getGroups', uid: uid)
39
- #end
40
-
41
- # Cities’ names
42
- # @param [Array<Fixnum>, Fixnum] cids cities identifiers
43
- # @return [Array<Hash>] hash with city identifier and it’s name
44
- def get_cities(cids)
45
- cids = Array(cids).join(',')
46
- request('getCities', cids: cids)
47
- end
48
-
49
- def get_city(cid)
50
- get_cities(cid)[0]
51
- end
52
-
53
- # Countries’ names
54
- # @param [Array<Fixnum>, Fixnum] cids cities identifiers
55
- # @return [Array<Hash>] hash with city identifier and it’s name
56
- def get_countries(cids)
57
- cids = Array(cids).join(',')
58
- request('getCountries', cids: cids)
59
- end
60
-
61
- def get_country(cid)
62
- get_countries(cid)[0]
63
- end
64
-
65
- # Friends information
66
- # @param [Fixnum] uid user identifier
67
- # @param [Hash] options
68
- # @option options [Array<String>] :fields ([:uid, :first_name, :last_name]) what fields to request
69
- # @option options [Fixnum] :count how many friends to request
70
- # @option options [Fixnum] :offset offset of friends to request
71
- def get_friends(uid, options = {})
72
- request('friends.get', options.merge(uid: uid))
73
- end
74
-
75
- # Statuses from user’s wall
76
- # @param [Fixnum] uid user identifier
77
- # @param [Hash] options
78
- # @option options [Fixnum] :count how many statuses to request
79
- # @option options [Fixnum] :offset offset of statuses to request
80
- # @option options [:owner, :others, :all] :filter (:all) what kind of statuses to request
81
- # @return [Array<Fixnum, *Hash>] count of statuses and each status in hash
82
- def get_wall(uid, options = {})
83
- options[:filter] ||= :all
84
- request('wall.get', options.merge(owner_id: uid))
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
94
6
  end
95
7
  end
8
+
9
+ require 'vk/dsl/audio'
10
+ require 'vk/dsl/database'
11
+ require 'vk/dsl/friends'
12
+ require 'vk/dsl/groups'
13
+ require 'vk/dsl/wall'
14
+ require 'vk/dsl/photos'
15
+ require 'vk/dsl/newsfeed'
16
+ require 'vk/dsl/users'
17
+
18
+ module Vk::DSL
19
+ include Audio
20
+ include Database
21
+ include Friends
22
+ include Groups
23
+ include Wall
24
+ include Photos
25
+ include Newsfeed
26
+ include Users
27
+ end
@@ -0,0 +1,51 @@
1
+ require 'vk/exceptions'
2
+ require 'vk/audio'
3
+
4
+ module Vk
5
+ module DSL
6
+ module Audio
7
+ AUDIO_SORT = {
8
+ popularity: 2,
9
+ duration: 1,
10
+ date: 0
11
+ }
12
+
13
+ # @param [Integer] owner_id user’s identifier
14
+ # @param [Hash] options
15
+ # @option options [Fixnum] :offset
16
+ # @option options [Fixnum] :count
17
+ def get_audios(owner_id, options = {})
18
+ fail NotImplementedError, ':need_user is not supported for audio.get method' if options.key?(:need_user)
19
+ Vk::Result.new('audio.get', Vk::Audio, options.merge(owner_id: owner_id))
20
+ end
21
+
22
+ # @param [Integer] owner_id user’s identifier
23
+ # @param [Hash] options
24
+ # @option options [Fixnum] :offset
25
+ # @option options [Fixnum] :count
26
+ def get_audio_albums(owner_id, options = {})
27
+ Vk::Result.new('audio.getAlbums', Vk::Audio::Album, options.merge(owner_id: owner_id))
28
+ end
29
+
30
+ # @param [Fixnum] lyrics_id
31
+ # @return [Vk::Audio::Lyrics]
32
+ def get_lyrics(lyrics_id)
33
+ Vk::Audio::Lyrics.new(request('audio.getLyrics', {lyrics_id: lyrics_id}))
34
+ end
35
+
36
+ # @param [String] q
37
+ # @param [Hash] options
38
+ # @option options [Fixnum] :offset
39
+ # @option options [Fixnum] :count
40
+ # @option options [Fixnum] :auto_complete
41
+ def search_lyrics(q, options = {})
42
+ options[:auto_complete] = !!options[:auto_complete] ? 1 : 0 if options.key?(:auto_complete)
43
+ options[:lyrics] = !!options[:lyrics] ? 1 : 0 if options.key?(:lyrics)
44
+ options[:performer_only] = !!options[:performer_only] ? 1 : 0 if options.key?(:performer_only)
45
+ options[:sort] = AUDIO_SORT[options[:sort]] if options[:sort].is_a?(Symbol)
46
+ options[:search_own] = !!options[:search_own] ? 1 : 0 if options.key?(:search_own)
47
+ Vk::Result.new('audio.search', Vk::Audio, options.merge(q: q))
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,108 @@
1
+ require 'vk/exceptions'
2
+
3
+ module Vk
4
+ module DSL
5
+ module Database
6
+ # @param [Hash] options
7
+ # @option options :need_all [Boolean, 1] load all countries
8
+ # @option options :code [<String>] country codes to load
9
+ # @option options :offset [Fixnum] offset for loaded countries
10
+ # @option options :count [Fixnum] (100) amount of countries to load
11
+ def get_countries(options = {})
12
+ options[:need_all] = 1 if options[:need_all]
13
+ options[:code] = options[:code].join(',') if options[:code]
14
+ Vk::Result.new('database.getCountries', Vk::Country, options)
15
+ end
16
+
17
+ # Countries’ names
18
+ # @param [Array<Fixnum>, Fixnum] country_ids cities identifiers
19
+ # @return [Array<Vk::Country>] array of Vk::Country
20
+ def get_countries_by_id(country_ids)
21
+ require 'vk/country'
22
+ country_ids = Array(country_ids)
23
+ raise Vk::TooMuchArguments.new('database.getCountriesById', 'country_ids', 1000) if country_ids > 1000
24
+ request('database.getCountriesById', country_ids: country_ids.join(',')).map do |country|
25
+ Vk::Country.new(country)
26
+ end
27
+ end
28
+
29
+ # @param [String] country_id
30
+ # @return [Vk::Country]
31
+ def get_country_by_id(country_id)
32
+ get_countries_by_id(country_id).first
33
+ end
34
+
35
+ alias get_country get_country_by_id
36
+
37
+ # @param [Hash] options
38
+ # @option options :country_id [<String>] required attribute. Country identifier
39
+ # @option options :q [String] search query
40
+ # @option options :offset [Fixnum] offset for loaded countries
41
+ # @option options :count [Fixnum] (100) amount of countries to load
42
+ # @return [Vk::Result<Vk::Region>]
43
+ def get_regions(country_id, options = {})
44
+ if country_id.is_a?(:hash)
45
+ country_id = options.delete(:country_id) do
46
+ raise 'No country_id attribute given for database.getRegions'
47
+ end
48
+ end
49
+ require 'vk/region'
50
+ options[:code] = options[:code].join(',') if options[:code]
51
+ Vk::Result.new('database.getRegions', Vk::Region, options, country_id: country_id)
52
+ end
53
+
54
+ # Countries’ names
55
+ # @param [Array<Fixnum>, Fixnum] street_ids cities identifiers
56
+ # @return [Array<Vk::Country>] array of Vk::Country
57
+ def get_streets_by_id(street_ids)
58
+ require 'vk/street'
59
+ street_ids = Array(street_ids)
60
+ raise Vk::TooMuchArguments.new('database.getCountriesById', 'street_ids', 1000) if street_ids > 1000
61
+ request('database.getCountriesById', street_ids: street_ids.join(',')).map do |street|
62
+ Vk::Street.new(street)
63
+ end
64
+ end
65
+
66
+ # @param [Fixnum] street_id
67
+ def get_street_by_id(street_id)
68
+ get_streets_by_id(street_id).first
69
+ end
70
+
71
+ alias get_street get_street_by_id
72
+
73
+ # Cities’ names
74
+ # @param [Array<Fixnum>, Fixnum] city_ids cities identifiers
75
+ # @return [Array<Hash>] hash with city identifier and it’s name
76
+ def get_cities_by_id(city_ids)
77
+ require 'vk/city'
78
+ city_ids = Array(city_ids)
79
+ raise Vk::TooMuchArguments.new('database.getCitiesById', 'city_ids', 1000) if city_ids > 1000
80
+ request('database.getCitiesById', city_ids: city_ids.join(',')).map do |city|
81
+ Vk::City.new(city)
82
+ end
83
+ end
84
+
85
+ # @return [Vk::City]
86
+ def get_city_by_id(cid)
87
+ get_cities_by_id(cid).first
88
+ end
89
+
90
+ alias get_city get_city_by_id
91
+
92
+ # @param [Hash] options
93
+ # @option options :country_id [<String>] required attribute. Country identifier
94
+ # @option options :region_id [<String>] optional attribute. Region identifier
95
+ # @option options :q [String] search query
96
+ # @option options :offset [Fixnum] offset for loaded countries
97
+ # @option options :count [Fixnum] (100) amount of countries to load
98
+ def get_cities(options = {})
99
+ require 'vk/city'
100
+ raise 'No country_id attribute given for database.getRegions' unless options[:country_id]
101
+ options[:code] = options[:code].join(',') if options[:code]
102
+ Vk::Result.new('database.getCities', Vk::City, options,
103
+ country_id: options[:country_id],
104
+ region_id: options[:region_id])
105
+ end
106
+ end
107
+ end
108
+ end