Skip to content

Commit

Permalink
vk.com, not vkontakte
Browse files Browse the repository at this point in the history
  • Loading branch information
komba committed Apr 9, 2012
1 parent 8e00fa9 commit ff829d8
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 127 deletions.
38 changes: 19 additions & 19 deletions lib/vkontakte/api/secure.rb
@@ -1,70 +1,70 @@
module Vkontakte
module Api
module Secure

def self.included(base)
base.class_eval do
define_method :secure do
@secure ||= Standart.new(self)
end
end
end

class Standart < Api::Base

def default_options
{
:timestamp => Time.now.utc.to_i,
{
:timestamp => Time.now.utc.to_i,
:random => Kernel.rand(10000),
:client_secret => Vkontakte.config.app_secret
:client_secret => Vkontakte.config.app_secret
}
end

# Отправляет уведомление пользователю.
# http://vkontakte.ru/developers.php?oid=-1&p=secure.sendNotification
#
def sendNotification(options = {})
options = default_options.merge(options)
call('secure.sendNotification', options)
end
# Возвращает платежный баланс (счет) приложения в сотых долях голоса.

# Возвращает платежный баланс (счет) приложения в сотых долях голоса.
#
def getAppBalance(options = {})
options = default_options.merge(options)
call("secure.getAppBalance", options)
end
# Возвращает баланс пользователя на счету приложения в сотых долях голоса.

# Возвращает баланс пользователя на счету приложения в сотых долях голоса.
#
def getBalance(options = {})
options = default_options.merge(options)
call("secure.getBalance", options)
end

# Списывает голоса со счета пользователя на счет приложения (в сотых долях).
#
def withdrawVotes(options = {})
options = default_options.merge(options)
call("secure.withdrawVotes", options)
end
# Выводит историю транзакций по переводу голосов между пользователями и приложением.

# Выводит историю транзакций по переводу голосов между пользователями и приложением.
#
def getTransactionsHistory(options = {})
options = default_options.merge(options)
call("secure.getTransactionsHistory", options)
end
# Поднимает пользователю рейтинг от имени приложения.

# Поднимает пользователю рейтинг от имени приложения.
#
def addRating(options = {})
options = default_options.merge(options)
call("secure.addRating", options)
end
# Устанавливает счетчик, который выводится пользователю жирным шрифтом в левом меню.
# Это происходит только в том случае, если пользователь добавил приложение в левое меню со страницы приложения,

# Устанавливает счетчик, который выводится пользователю жирным шрифтом в левом меню.
# Это происходит только в том случае, если пользователь добавил приложение в левое меню со страницы приложения,
# списка приложений или настроек.
#
def setCounter(options = {})
Expand Down
48 changes: 25 additions & 23 deletions lib/vkontakte/app/base.rb
Expand Up @@ -4,20 +4,20 @@ module Vkontakte
module App
class Base
include ::HTTParty
base_uri "https://api.vkontakte.ru"

base_uri "https://api.vk.com"
format Vkontakte.config.format
debug_output Vkontakte.config.logger

attr_accessor :auth

def initialize(app_id = nil, app_secret = nil)
@config = {
:app_id => (app_id || Vkontakte.config.app_id),
:app_id => (app_id || Vkontakte.config.app_id),
:app_secret => (app_secret || Vkontakte.config.app_secret)
}
end

# http://vkontakte.ru/developers.php?oid=-1&p=%D0%90%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F
# Site auth:
# https://api.vkontakte.ru/oauth/access_token?
Expand All @@ -33,63 +33,65 @@ def initialize(app_id = nil, app_secret = nil)
# {"access_token":"533bacf01e11f55b536a565b57531ac114461ae8736d6506a3", "expires_in":43200, "user_id":6492}
#
def authorize(code = nil, options = {})
options = {
:client_id => @config[:app_id],
options = {
:client_id => @config[:app_id],
:client_secret => @config[:app_secret],
:code => code
}.merge(options)

# Server auth
if options[:code].blank?
options.delete(:code)
options[:grant_type] = 'client_credentials'
end

@auth = get("/oauth/access_token", options)

self.class.base_uri "https://oauth.vk.com"
@auth = get("/access_token", options)
end

# Check if app is authorized
#
def authorized?
auth && auth['access_token']
end
# Выполнение запросов к API

# Выполнение запросов к API
# https://api.vkontakte.ru/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN
# METHOD_NAME – название метода из списка функций API,
# PARAMETERS – параметры соответствующего метода API,
# ACCESS_TOKEN – ключ доступа, полученный в результате успешной авторизации приложения.
# ACCESS_TOKEN – ключ доступа, полученный в результате успешной авторизации приложения.
# Example:
# https://api.vkontakte.ru/method/getProfiles?uid=66748&access_token=533bacf01e11f55b536a565b57531ac114461ae8736d6506a3
#
# More info: http://vkontakte.ru/developers.php?oid=-1&p=%D0%92%D1%8B%D0%BF%D0%BE%D0%BB%D0%BD%D0%B5%D0%BD%D0%B8%D0%B5_%D0%B7%D0%B0%D0%BF%D1%80%D0%BE%D1%81%D0%BE%D0%B2_%D0%BA_API
#
def call(method_name, params = {})
params[:access_token] ||= @auth['access_token'] if authorized?

unless params[:access_token].blank?
self.class.base_uri "https://api.vk.com"
get("/method/#{method_name}", params)
else
raise VkException.new(method_name, {
:error => 'access_token is blank',
:error => 'access_token is blank',
:error_description => 'You need first authorize app before call API methods.'
})
end
end

protected

def get(method_name, options = {})
response = self.class.get(method_name, :query => options)

if response['error']
raise VkException.new(method_name, response)
else
return response
end
end
end

# Errors
# {"error":"invalid_grant","error_description":"Code is expired."}
# {"error":{"error_code":5,"error_msg":"User authorization failed: invalid application type","request_params":[{"key":"oauth","value":"1"},{"key":"method","value":"getProfiles"},{"key":"uid","value":"66748"},{"key":"access_token","value":"533bacf01e11f55b536a565b57531ac114461ae8736d6506a3"}]}}
Expand All @@ -98,13 +100,13 @@ class VkException < Exception
def initialize(method_name, options)
error_hash = options.symbolize_keys
@message = "Error in #{method_name}: "

if error_hash[:error].is_a?(Hash)
@message += error_hash[:error].inspect
else
@message += [error_hash[:error], error_hash[:error_description]].join('-')
end

super @message
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vkontakte/app/secure.rb
Expand Up @@ -5,7 +5,7 @@ module App
#
class Secure < Base
include Api::Secure

def initialize(app_id = nil, app_secret = nil)
super
authorize
Expand Down
42 changes: 21 additions & 21 deletions spec/api/friends_spec.rb
Expand Up @@ -4,51 +4,51 @@
it "should be valid" do
Vkontakte::Api::Friends.should be_a(Module)
end

context "iframe" do
before(:each) do
@token = '3a3d250e705051b03ed479343c3ec2833783eea3eea29860182716ed1d40319'
@iframe = Vkontakte::App::Iframe.new
@iframe.auth = {'access_token' => @token}
end

it "should get friends list by uid param" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/friends.get?access_token=#{@token}&uid=81202312",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/friends.get?access_token=#{@token}&uid=81202312",
:body => '{"response":[2592709,3859793,4663468]}')

@iframe.friends.get(:uid => 81202312).should == {"response"=>[2592709, 3859793, 4663468]}
end

it "should get friends list by fields" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/friends.get?access_token=#{@token}&fields=81202312",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/friends.get?access_token=#{@token}&fields=81202312",
:body => '{"response":[{"uid":2592709,"first_name":"Павел","last_name":"Галета","online":0},{"uid":3859793,"first_name":"Мария","last_name":"Семёнова","online":0},{"uid":4663468,"first_name":"Ekaterina","last_name":"Koronotova","online":0}]}')

@iframe.friends.get(:fields => 81202312).should == {"response"=>[{"uid"=>2592709, "last_name"=>"\320\223\320\260\320\273\320\265\321\202\320\260", "online"=>0, "first_name"=>"\320\237\320\260\320\262\320\265\320\273"}, {"uid"=>3859793, "last_name"=>"\320\241\320\265\320\274\321\221\320\275\320\276\320\262\320\260", "online"=>0, "first_name"=>"\320\234\320\260\321\200\320\270\321\217"}, {"uid"=>4663468, "last_name"=>"Koronotova", "online"=>0, "first_name"=>"Ekaterina"}]}
end

it "should get getAppUsers" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/friends.getAppUsers?access_token=#{@token}",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/friends.getAppUsers?access_token=#{@token}",
:body => '{"response":[2592709]}')

@iframe.friends.getAppUsers.should == {"response" => [2592709]}
end

it "should get getOnline" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/friends.getOnline?access_token=#{@token}&uid=2592709",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/friends.getOnline?access_token=#{@token}&uid=2592709",
:body => '{"response":[2450999,2488708,2649518,4440077]}')

@iframe.friends.getOnline(:uid => 2592709).should == {"response"=>[2450999, 2488708, 2649518, 4440077]}
end

it "should get getMutual" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/friends.getMutual?target_uid=2450999&access_token=#{@token}&source_uid=2592709",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/friends.getMutual?target_uid=2450999&access_token=#{@token}&source_uid=2592709",
:body => '{"response":[2301578,2619312,5818827,6391852,6411298,6422462]}')

response = @iframe.friends.getMutual(:target_uid => 2450999, :source_uid => 2592709)
response.should == {"response" => [2301578,2619312,5818827,6391852,6411298,6422462]}
end
Expand Down
14 changes: 7 additions & 7 deletions spec/api/groups_spec.rb
Expand Up @@ -14,15 +14,15 @@

it "should get groups list by uid param" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.get?access_token=#{@token}&uid=81202312",
"https://api.vk.com/method/groups.get?access_token=#{@token}&uid=81202312",
:body => '{"response":[16527885]}')

@iframe.groups.get(:uid => 81202312).should == {"response" => [16527885]}
end

it "should raise permission error on access friend groups" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.get?access_token=#{@token}&uid=2592709",
"https://api.vk.com/method/groups.get?access_token=#{@token}&uid=2592709",
:body => '{"error":{"error_code":7,"error_msg":"Permission to perform this action is denied by user","request_params":[{"key":"oauth","value":"1"},{"key":"method","value":"groups.get"},{"key":"uid","value":"2592709"},{"key":"access_token","value":"74aee6063ec4aea07047ba3cb47079607f870797079ea90fef75c6361570a5f"}]}}')

lambda {
Expand All @@ -37,23 +37,23 @@

it "should return group info" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.getById?access_token=#{@token}&gid=#{@group_id}",
"https://api.vk.com/method/groups.getById?access_token=#{@token}&gid=#{@group_id}",
:body => '{"response":[{"gid":16527885,"name":"Club Music Group of Kiev","screen_name":"club16527885","is_closed":0,"type":"group","photo":"http:\/\/cs884.vkontakte.ru\/g16527885\/c_08b73308.jpg","photo_medium":"http:\/\/cs884.vkontakte.ru\/g16527885\/b_6e68053d.jpg","photo_big":"http:\/\/cs884.vkontakte.ru\/g16527885\/a_ba67625c.jpg"}]}')

@iframe.groups.getById(:gid => @group_id).should == {"response"=>[{"photo"=>"http://cs884.vkontakte.ru/g16527885/c_08b73308.jpg", "name"=>"Club Music Group of Kiev", "gid"=>16527885, "is_closed"=>0, "photo_medium"=>"http://cs884.vkontakte.ru/g16527885/b_6e68053d.jpg", "type"=>"group", "photo_big"=>"http://cs884.vkontakte.ru/g16527885/a_ba67625c.jpg", "screen_name"=>"club16527885"}]}
end

it "should return group member" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.isMember?access_token=#{@token}&gid=#{@group_id}&uid=81202312",
"https://api.vk.com/method/groups.isMember?access_token=#{@token}&gid=#{@group_id}&uid=81202312",
:body => '{"response":1}')

@iframe.groups.isMember(:uid => 81202312, :gid => @group_id).should == {"response" => 1}
end

it "should return all groups members" do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.getMembers?access_token=#{@token}&gid=#{@group_id}&count=5",
"https://api.vk.com/method/groups.getMembers?access_token=#{@token}&gid=#{@group_id}&count=5",
:body => '{"response":{"count":29364,"users":[107765962,66506999,145557591,72256631,28639402]}}')

response = @iframe.groups.getMembers(:gid => @group_id, :count => 5)
Expand All @@ -64,7 +64,7 @@
describe '#join' do
it 'should join to group' do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.join?access_token=#{@token}&gid=1", :body => '{"response":1}')
"https://api.vk.com/method/groups.join?access_token=#{@token}&gid=1", :body => '{"response":1}')

response = @iframe.groups.join(:gid => 1)
response.should == {"response"=> 1}
Expand All @@ -74,7 +74,7 @@
describe '#leave' do
it 'should allow user to leave group' do
FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/groups.leave?access_token=#{@token}&gid=1", :body => '{"response":1}')
"https://api.vk.com/method/groups.leave?access_token=#{@token}&gid=1", :body => '{"response":1}')

response = @iframe.groups.leave(:gid => 1)
response.should == {"response"=> 1}
Expand Down
12 changes: 6 additions & 6 deletions spec/api/photos_spec.rb
Expand Up @@ -4,24 +4,24 @@
it "should be valid" do
Vkontakte::Api::Photos.should be_a(Module)
end

context "params" do
before(:each) do
@token = '3a3d250e705051b03ed479343c3ec2833783eea3eea29860182716ed1d40319'
@iframe = Vkontakte::App::Iframe.new
@iframe.auth = {'access_token' => @token}
end

it "should be call getAlbums method" do
response = '{"response":[{"aid":"17071606","thumb_id":"98054577","owner_id":"6492","title":"",
"description":"","created":"1204576880","updated":"1229532461", "size":"3","privacy":"0"}]}'

FakeWeb.register_uri(:get,
"https://api.vkontakte.ru/method/photos.getAlbums?access_token=#{@token}",
FakeWeb.register_uri(:get,
"https://api.vk.com/method/photos.getAlbums?access_token=#{@token}",
:body => response)

@iframe.photos.getAlbums.should == {"response" => [{"aid" => "17071606","thumb_id" => "98054577","owner_id" => "6492","title" => "", "description" => "","created" => "1204576880","updated" => "1229532461", "size" => "3","privacy" => "0"}]}
end

end
end

0 comments on commit ff829d8

Please sign in to comment.