Advertisement
andreymal

vktest.rb [2]

Feb 27th, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.81 KB | None | 0 0
  1. require 'uri'
  2. require 'json'
  3. require 'net/http'
  4.  
  5. class VK
  6.   def initialize(access_token=nil, v='5.45')
  7.     @access_token = access_token
  8.     @v = v
  9.   end
  10.  
  11.   def method_missing(name, options)
  12.     qname = name.to_s.sub('_', '.')
  13.  
  14.     norm_options = {access_token: @access_token, v: @v}
  15.     options.each do |k, v|
  16.       if v.is_a? Array
  17.         norm_options[k] = v.join(',')
  18.       else
  19.         norm_options[k] = v
  20.       end
  21.     end
  22.  
  23.     qstring = URI.encode_www_form(norm_options)
  24.  
  25.     url = "https://api.vk.com/method/#{qname}.json"
  26.     http = Net::HTTP.new('api.vk.com', 443)
  27.     http.use_ssl = true
  28.  
  29.     request = Net::HTTP::Post.new(url)
  30.     request.body = qstring
  31.  
  32.     response = http.request(request)
  33.     JSON.parse(response.body)
  34.   end
  35. end
  36.  
  37. vk = VK.new
  38. puts vk.users_get(user_ids: ['durov', 6])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement