Permalink
Browse files

~

  • Loading branch information...
1 parent 8b0c0a8 commit 7bd9df5abfae48929530f42f77994a53e7f28a59 Andrew Zinenko committed Jul 17, 2012
Showing with 95 additions and 81 deletions.
  1. +10 −6 Gemfile.lock
  2. +1 −1 Rakefile
  3. +2 −2 lib/vk-ruby.rb
  4. +14 −1 lib/vk-ruby/api/base.yml
  5. +8 −8 lib/vk-ruby/configurable.rb
  6. +37 −52 lib/vk-ruby/core.rb
  7. +0 −10 lib/vk-ruby/vk_exception.rb
  8. +21 −0 tests/configurable.rb
  9. +2 −1 vk-ruby.gemspec
View
@@ -2,21 +2,25 @@ PATH
remote: .
specs:
vk-ruby (0.8.2)
- multi_json (~> 1.2.0)
+ faraday (~> 0.8.0)
+ multi_json (~> 1.3.6)
transformer (~> 0.2.2)
GEM
remote: http://rubygems.org/
specs:
- addressable (2.2.6)
+ addressable (2.2.8)
crack (0.3.1)
- minitest (2.10.0)
- multi_json (1.2.0)
+ faraday (0.8.1)
+ multipart-post (~> 1.1)
+ minitest (2.12.1)
+ multi_json (1.3.6)
+ multipart-post (1.1.5)
oj (1.0.6)
rake (0.9.2.2)
transformer (0.2.2)
- webmock (1.7.8)
- addressable (~> 2.2, > 2.2.5)
+ webmock (1.8.7)
+ addressable (>= 2.2.7)
crack (>= 0.1.7)
PLATFORMS
View
@@ -17,6 +17,6 @@ require 'rake/testtask'
task :default => :test
Rake::TestTask.new do |test|
- test.pattern = 'tests/*_test.rb'
+ test.pattern = 'tests/configurable.rb'
test.verbose = true
end
View
@@ -1,6 +1,6 @@
# encoding: UTF-8
-require 'net/https'
+require 'faraday'
require 'cgi'
require 'transformer'
require 'yaml'
@@ -9,4 +9,4 @@
module VK
end
-%w(connection core secure serverside standalone vk_exception).each{|lib| require "vk-ruby/#{lib}"}
+%w(configurable core secure serverside standalone vk_exception).each{|lib| require "vk-ruby/#{lib}"}
View
@@ -113,4 +113,17 @@
- get
- getById
- isMember
- - getMembers
+ - getMembers
+ board:
+ - getTopicsnew
+ - deleteTopicnew
+ - getCommentsnew
+ - addCommentnew
+ - editCommentnew
+ - deleteCommentnew
+ - restoreCommentnew
+ - addTopicnew
+ - closeTopicnew
+ - fixTopicnew
+ - unfixTopicnew
+ - deleteTopic
@@ -2,14 +2,14 @@
module VK::Configurable
- def attr_configurable attr_name, defaul = nil
- define_method attr_name.to_sym do
- if instance_variable_get("@#{attr_name}")
- instance_variable_get("@#{attr_name}")
- else
- VK.const_defined?(attr_name.upercase) ? VK.const_get(attr_name.upercase) : nil
- end
- end
+ def attr_configurable *arr
+ params = arr.pop if arr.last.is_a?(Hash)
+
+ arr.each do |attr_name|
+ define_method attr_name.to_sym do
+ (var = instance_variable_get("@#{attr_name}")) ? var : (const = VK.const_defined?(attr_name.upcase) ? const : nil)
+ end
+ end
end
end
View
@@ -1,86 +1,71 @@
# encoding: UTF-8
module VK::Core
- include Configurable
+ extend VK::Configurable
+
+ attr_accessor :faraday_middleware
attr_configurable :app_id, :access_token, :ca_path, :ca_file, :verify_mode, :logger, :proxy
- attr_configurable :verbs, :post
- attr_configurable :attempts, 5
- attr_configurable :timeout, 2
- attr_configurable :open_timeout, 3
+ attr_configurable :verbs, default: :post
+ attr_configurable :attempts, default: 5
+ attr_configurable :timeout, default: 2
+ attr_configurable :open_timeout, default: 3
+ attr_configurable :default_adapter, default: Faraday.default_adapter
[:base, :ext, :secure].each do |name|
- class_eval(<<-EVAL, __FILE__, __LINE__)
+ class_eval(<<-EVAL, __FILE__, __LINE__ + 1)
def #{name}_api
@@#{name}_api ||= YAML.load_file( File.expand_path( File.dirname(__FILE__) + "/api/#{name}.yml" ))
end
EVAL
end
- # Call vk.com api method.
- #
- # @param method_name a vk.com api method name
- # @param arr a array of method params
- #
- # @example
- # @application.vk_call('api_method_name', [{:a => 1, :b => "My String"}])
- # => request to "https://api.vk.com/method/api_method_name?a=1&b=My+String"
- #
- # @return the vk.com api response hash
-
def vk_call(method_name, arr)
- params = arr.shift || {}
- params[:access_token] ||= self.access_token
-
- response = request("/method/#{method_name}", params)
+ response = request("/method/#{method_name}", (arr.shift || {}))
- if response['error']
- if response['error']['code'].to_i == 5
- raise VK::RevokeAccessException.new(method_name, response, params[:access_token])
- else
- raise VK::ApiException.new(method_name, response) if response['error']
- end
- end
+ raise VK::ApiException.new(method_name, response) if response['error']
response['response']
end
private
def request(path, options = {})
- attempts = params.delete(:attempts) || self.attempts
- verbs = params.delete(:verbs) || self.verbs
- params = self.params.merge(options)
+ options[:access_token] ||= self.access_token
- case verbs
- when :post then body = self.class.encode_params(options)
- when :get then path << '?' << self.class.http_params(options)
+ attempts = options.delete(:attempts) || self.attempts
+ host = options.delete(:host) || 'https://api.vk.com'
+ verb = (options[:verb] ||= self.verbs)
+
+ case verb.downcase.to_sym
+ when :post then body = encode_params(options)
+ when :get then path << '?' << encode_params(options)
else raise 'Not suported http verbs'
end
- response = connection(params).request(http_verbs, path, params, body, attempts)
+ request_options = {params: (verb == "get" ? params : {})}
+
+ response = Faraday.new(host, request_options, &faraday_middleware)
- raise VK::BadResponseException.new(response, verbs, path, options) if response.code.to_i >= 500
+ raise VK::BadResponseException.new(response, verbs, path, options) if response.code.to_i != 200
- parse response.body
+ response
+ end
+
+ def faraday_middleware
+ @faraday_middleware ||= proc do
+ faraday.request :url_encoded
+ faraday.response :logger
+ faraday.adapter self.default_adapter
+ end
end
- # Encodes a given hash into a query string.
- #
- # @param params_hash a hash of values to CGI-encode and appropriately join
- #
- # @example
- # VK::Core.encode_params({:a => 2, :b => "My String"})
- # => "a=2&b=My+String"
- #
- # @return the appropriately-encoded string
-
- def self.encode_params(param_hash)
- ((param_hash || {}).sort_by{|k, v| k.to_s}.collect do |key_and_value|
- key_and_value[1] = MultiJson.dump(key_and_value[1]) unless key_and_value[1].is_a? String
- "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
- end).join("&")
+ def encode_params(params)
+ params.map do |key, value|
+ value = MultiJson.dump(value) unless value.is_a?(String)
+ "#{key}=#{CGI.escape value}"
+ end.join("&")
end
end
@@ -20,14 +20,4 @@ def initialize(error_hash)
end
end
- class RevokeAccessException < Exception
- attr_reader :vk_method, :error_code, :error_msg, :access_token
-
- def initialize(api_method, error_hash, access_token)
- @access_token = access_token
- @vk_method, @error_code = api_method, error_hash['error']['error_code'].to_i
- @error_msg = "Revoke access for this token #{@access_token}"
- super @error_msg
- end
- end
end
View
@@ -0,0 +1,21 @@
+require File.expand_path('../helpers', __FILE__)
+
+class TestConfigurable < MiniTest::Unit::TestCase
+
+ def setup
+ @application = Object.new
+ @application.include Vk::Configurable
+ @application.class_eval do
+ at
+ end
+ end
+
+ def test_that_kitty_can_eat
+ assert_equal "OHAI!", @meme.i_can_has_cheezburger?
+ end
+
+ def test_that_it_will_not_blend
+ refute_match %r/^no/, @meme.will_it_blend?
+ end
+
+end
View
@@ -17,7 +17,8 @@ Gem::Specification.new do |gem|
gem.extra_rdoc_files = ["LICENSE.txt", "README.md" ]
gem.add_runtime_dependency 'transformer', '~> 0.2.2'
- gem.add_runtime_dependency 'multi_json', '~> 1.2.0'
+ gem.add_runtime_dependency 'multi_json', '~> 1.3.6'
+ gem.add_runtime_dependency 'faraday', '~> 0.8.0'
gem.add_development_dependency 'rake', '~> 0.9'
gem.add_development_dependency 'minitest', '~> 2.8'

0 comments on commit 7bd9df5

Please sign in to comment.