Module: VkLongpollBot::Request

Defined in:
lib/vk_longpoll_bot/request.rb

Overview

Some functions to send HTTP requests.

Class Method Summary collapse

Class Method Details

.api(method_name, parameters, access_token, v = VK_API_CURRENT_VERSION) ⇒ Hash

GET request to VK API.

Parameters:

  • method_name (String, Symbol)
  • parameters (Hash)
  • access_token (String)
  • v (Gem::Version, String) (defaults to: VK_API_CURRENT_VERSION)

    VK API version.

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
# File 'lib/vk_longpoll_bot/request.rb', line 30

def self.api(method_name, parameters, access_token, v = VK_API_CURRENT_VERSION)
  response = JSON.parse self.to("#{VK_API_URL_BASE}/method/#{method_name}?access_token=#{access_token}&v=#{v.to_s}&#{URI.encode_www_form(parameters)}")
  if response["response"]
    response["response"]
  elsif response["error"]
    raise Exceptions::APIError.new(response)
  else
    raise Exceptions::ResponseError.new(response)
  end
end

.longpoll(server, key, ts, wait = LONGPOLL_STANDART_WAIT) ⇒ Hash

GET request to longpoll server.

Parameters:

  • server (String)

    server address.

  • key (String)

    secret session key.

  • ts (Integer)

    index of last event.

  • wait (Integer) (defaults to: LONGPOLL_STANDART_WAIT)

    size of request timeout in seconds.

Returns:

  • (Hash)

    hash with timestamp of last event and array of updates.



50
51
52
# File 'lib/vk_longpoll_bot/request.rb', line 50

def self.longpoll(server, key, ts, wait = LONGPOLL_STANDART_WAIT)
  JSON.parse self.to("#{server}?act=a_check&key=#{key}&ts=#{ts}&wait=#{wait}")
end

.to(url) ⇒ String

Regular GET HTTP request to given URI.

Parameters:

  • url (String)

Returns:

  • (String)


16
17
18
19
# File 'lib/vk_longpoll_bot/request.rb', line 16

def self.to(url)
  uri = URI(url.to_s)
  Net::HTTP.get(uri)
end