Permalink
Browse files

+ connection and http verbs

  • Loading branch information...
1 parent 0814216 commit d635a4fc3bb2ed97512fbb4899fab8a1d0a755fb Andrew Zinenko committed Dec 27, 2011
Showing with 202 additions and 175 deletions.
  1. +1 −0 .gitignore
  2. +2 −9 Gemfile
  3. +13 −11 Gemfile.lock
  4. +2 −15 Rakefile
  5. +1 −1 VERSION
  6. +12 −6 lib/vk-ruby.rb
  7. +38 −0 lib/vk-ruby/connection.rb
  8. +28 −39 lib/vk-ruby/core.rb
  9. +20 −0 lib/vk-ruby/core_ext.rb
  10. +1 −1 lib/vk-ruby/serverside.rb
  11. +5 −0 lib/vk-ruby/version.rb
  12. +2 −3 tests/secure_test.rb
  13. +16 −4 tests/serverside_test.rb
  14. +32 −5 tests/standalone_test.rb
  15. +29 −81 vk-ruby.gemspec
View
@@ -14,6 +14,7 @@ doc
# jeweler generated
pkg
.redcar
+*.gem
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
View
11 Gemfile
@@ -1,11 +1,4 @@
source "http://rubygems.org"
-gem 'transformer'
-gem 'yajl-ruby'
-
-group :development do
- gem "bundler"
- gem "jeweler"
- gem 'minitest'
- gem 'webmock'
-end
+# Specify your gem's dependencies in jetspade.gemspec
+gemspec
View
@@ -1,13 +1,16 @@
+PATH
+ remote: .
+ specs:
+ vk-ruby (0.8.0)
+ json (~> 1.6)
+ transformer (~> 0.2.2)
+
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
crack (0.3.1)
- git (1.2.5)
- jeweler (1.6.4)
- bundler (~> 1.0)
- git (>= 1.2.5)
- rake
+ json (1.6.4)
minitest (2.8.1)
rake (0.9.2.2)
transformer (0.2.2)
@@ -20,9 +23,8 @@ PLATFORMS
ruby
DEPENDENCIES
- bundler
- jeweler
- minitest
- transformer
- webmock
- yajl-ruby
+ minitest (~> 2.8)
+ rake (~> 0.9)
+ vk-ruby!
+ webmock (~> 1.0)
+ yajl-ruby (~> 1.0)
View
@@ -2,6 +2,7 @@
require 'rubygems'
require 'bundler'
+
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
@@ -18,18 +19,4 @@ task :default => :test
Rake::TestTask.new do |test|
test.pattern = 'tests/*_test.rb'
test.verbose = true
-end
-
-require 'jeweler'
-Jeweler::Tasks.new do |gem|
- gem.name = "vk-ruby"
- gem.homepage = "http://github.com/zinenko/vk-ruby"
- gem.license = "MIT"
- gem.summary = %Q{ Ruby wrapper for vk.com API }
- gem.description = %Q{ Ruby wrapper for vk.com API }
- gem.email = "zinenkoan@gmail.com"
- gem.authors = ["Andrew Zinenko"]
- gem.files.include 'lib/**/*'
- gem.add_runtime_dependency 'transformer'
-end
-Jeweler::GemcutterTasks.new
+end
View
@@ -1 +1 @@
-0.7.3
+0.7.4
View
@@ -1,11 +1,17 @@
-require 'rubygems'
require 'net/https'
require 'transformer'
require 'yaml'
-require 'yajl/json_gem'
+require 'yajl'
-module VK; end
+module VK
+end
-%w(core secure serverside standalone vk_exception).each do |lib|
- require "vk-ruby/#{lib}"
-end
+VK::JSON = begin
+ require 'yajl'
+ ::Yajl
+rescue LoadError
+ require 'json'
+ ::JSON
+end
+
+%w(core_ext connection core secure serverside standalone vk_exception).each{|lib| require "vk-ruby/#{lib}"}
View
@@ -0,0 +1,38 @@
+class VK::Connection
+ def initialize options = {}
+ options.each{|attr, value| instance_variable_set("@#{attr}", value) }
+ @http = create_connection
+ end
+
+ def request(verbs, path, data, &block)
+ requester = Proc.new{|params|
+ case verbs
+ when :get
+ @http.get [path, params].join('?')
+ when :post
+ @http.post path, params
+ else
+ raise 'Not suported http verbs'
+ end
+ }.call(data.map{|k,v| "#{k}=#{v}"}.join('&'))
+
+ rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
+ @http = create_connection
+ @attempts == 3 ? raise : (@attempts += 1; retry)
+ end
+
+ private
+
+ def create_connection
+ net = Net::HTTP.new @host, @port
+
+ net.set_debug_output @logger if @logger && @logger.debug?
+
+ if @port == 443
+ net.use_ssl = true
+ net.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ end
+ net
+ end
+
+end
View
@@ -1,55 +1,44 @@
module VK
module Core
- attr_accessor :app_id, :access_token, :user_id, :expires_in, :debug, :logger
+ attr_accessor :app_id, :access_token, :expires_in, :logger, :verbs, :attempts, :headers
+
+ private
def vk_call(method_name,p)
- params = p[0] ||= {}
+ params = p.shift || {}
raise 'undefined access token' unless params[:access_token] ||= @access_token
- begin
- resp = request(:path => "/method/#{method_name}", :params => params)
- result = JSON.parse(resp.body)
- rescue Exception => info
- puts resp.inspect
- puts info.inspect
- raise
- end
- raise VK::VkException.new(method_name,result) if result['error']
-
- result['response']
- end
-
- private
+ response = request( :verbs => params.delete(:verbs),
+ :path => [nil, 'method', method_name].join('/'),
+ :params => params)
- # def json(string)
- # return JSON.parse(string) rescue raise string.inspect
- # end
-
- def request(p={})
- http.get( p[:path] + "?" + (p[:params].map{|k,v| "#{k}=#{v}" }).join('&') )
+ raise VK::VkException.new(method_name, response) if response['error']
+ response['response']
end
- def http(p={})
- prms = ({:host => 'api.vk.com', :port => 443, :ssl => true }).merge p
- net = Net::HTTP.new prms[:host], prms[:port]
- net.set_debug_output @logger if @logger && @debug
- unless prms[:ssl] == false
- net.use_ssl = true
- net.verify_mode = OpenSSL::SSL::VERIFY_NONE
- end
- net
- end
+ def request(options, &block)
+ http_verbs = (options.delete(:verbs) || @verbs || :post).to_sym
+ path = options.delete(:path)
+ body = options.delete(:params)
- def base_api
- @@base_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/api/base.yml" ))
- end
+ params = {:host => 'api.vk.com',
+ :port => 443,
+ :logger => @logger,
+ :attempts => @attempts,
+ :headers => @headers }.merge(options)
- def ext_api
- @@ext_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/api/ext.yml" ))
+ http = VK::Connection.new(params)
+ response = http.request(http_verbs, path, body, &block)
+ VK::JSON.load(response.body)
end
- def secure_api
- @@secure_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/api/secure.yml" ))
+ [:base, :ext, :secure].each do |name|
+ class_eval(<<-EVAL, __FILE__, __LINE__)
+ def #{name}_api
+ @@#{name}_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/api/#{name}.yml" ))
+ end
+ EVAL
end
+
end
end
View
@@ -0,0 +1,20 @@
+# encoding: utf-8
+
+class String
+ if RUBY_VERSION >= '1.9'
+
+ def valid_utf8?
+ dup.force_encoding('UTF-8').valid_encoding?
+ end
+
+ else
+
+ def valid_utf8?
+ scan(Regexp.new('[^\x00-\xa0]', nil, 'u')) { |s| s.unpack('U') }
+ true
+ rescue ArgumentError
+ false
+ end
+
+ end
+end
@@ -19,7 +19,7 @@ def authorize(code = nil, auto_save = true)
raise VK::VkAuthorizeException.new('undefined code') unless code
params = {:client_id => @app_id, :client_secret => @app_secret, :code => code}
- result = JSON.parse(request({:path => "/oauth/access_token", :params => params }).body)
+ result = request({:path => "/oauth/access_token", :params => params })
raise VK::VkAuthorizeException.new(result) if result['error']
result.each{|k,v| instance_variable_set(:"@#{k}", v) } if auto_save
View
@@ -0,0 +1,5 @@
+module VK
+ # Actual VK-RUBY version.
+ #
+ VERSION = '0.7.4'
+end
View
@@ -10,7 +10,6 @@ def test_init_attributes
assert_equal @app.app_id, :test_id
assert_equal @app.app_secret, :test_secret
assert_nil @app.expires_in
- assert_nil @app.debug
assert_nil @app.logger
end
@@ -21,9 +20,9 @@ def test_methods_exists
end
def test_authorization
- stub_request(:get, /https:\/\/api.vk.com\/oauth\/access_token/).to_return(lambda { |request| {:body => URI.parse(request.uri).query.to_params.to_json} })
+ stub_request(:post, /https:\/\/api.vk.com\/oauth\/access_token/).to_return(lambda { |request| {:body => request.body.to_json}})
params = {:client_id => :test_id, :client_secret => :test_secret, :grant_type => :client_credentials }
- assert_equal @app.authorize , params.stringify
+ assert_equal @app.authorize , params
WebMock.reset!
end
View
@@ -13,7 +13,6 @@ def test_init_attributes
assert_equal @app.app_secret, :test_secret
assert_equal @app.access_token, :test_token
assert_nil @app.expires_in
- assert_nil @app.debug
assert_nil @app.logger
end
@@ -35,12 +34,25 @@ def test_groups_methods_prefix
end
end
- def test_request_params
- stub_request(:get, /https:\/\/api.vk.com\/method/).to_return(lambda { |request| {:body => {'response' => URI.parse(request.uri).query.to_params}.to_json } })
+ def test_post_request_params
+ stub_request(:post, /https:\/\/api.vk.com\/method/).to_return(lambda { |request| {:body => {'response' => request.body}.to_json } })
perebor @app, @base_api do |obj, method_name, is_group|
unless is_group
params = random_params.merge!(:access_token => :test_token)
+ assert_equal obj.method(method_name.to_sym).call(params), params unless is_group
+ end
+ end
+
+ WebMock.reset!
+ end
+
+ def test_get_request_params
+ stub_request(:get, /https:\/\/api.vk.com\/method/).to_return(lambda { |request| {:body => {'response' => URI.parse(request.uri).query.to_params}.to_json } })
+
+ perebor @app, @base_api do |obj, method_name, is_group|
+ unless is_group
+ params = random_params.merge!(:access_token => :test_token, :verbs => 'get')
assert_equal obj.method(method_name.to_sym).call(params), params.stringify unless is_group
end
end
@@ -49,7 +61,7 @@ def test_request_params
end
def test_authorization
- stub_request(:get, /https:\/\/api.vk.com\/oauth\/access_token/).to_return(lambda { |request| {:body => URI.parse(request.uri).query.to_params.to_json} })
+ stub_request(:post, /https:\/\/api.vk.com\/oauth\/access_token/).to_return(lambda { |request| {:body => request.body.to_json}})
params = {:code => :test_code, :client_id => :test_id ,:client_secret => :test_secret}
assert_equal @app.authorize(params[:code]) , params.stringify
Oops, something went wrong.

0 comments on commit d635a4f

Please sign in to comment.