Module: VkontakteApi::API

Defined in:
lib/vkontakte_api/api.rb

Overview

A low-level module which handles the requests to VKontakte API and returns their results as mashes.

It uses Faraday with middleware underneath the hood.

Constant Summary collapse

URL_PREFIX =

URL prefix for calling API methods.

'https://api.vk.com/method'

Class Method Summary collapse

Class Method Details

.call(method_name, args = {}, token = nil) ⇒ Hashie::Mash

API method call.

Parameters:

  • method_name (String)

    A full name of the method.

  • args (Hash) (defaults to: {})

    Method arguments.

  • token (String) (defaults to: nil)

    The access token.

Returns:

  • (Hashie::Mash)

    Mashed server response.



15
16
17
18
19
# File 'lib/vkontakte_api/api.rb', line 15

def call(method_name, args = {}, token = nil)
  flat_arguments = Utils.flatten_arguments(args)
  flat_arguments[:v] ||= VkontakteApi.api_version unless VkontakteApi.api_version.nil?
  connection(url: URL_PREFIX, token: token).send(VkontakteApi.http_verb, method_name, flat_arguments).body
end

.connection(options = {}) ⇒ Faraday::Connection

Faraday connection.

Parameters:

  • options (Hash) (defaults to: {})

    Connection options.

Options Hash (options):

  • :url (String)

    Connection URL (either full or just prefix).

  • :token (String)

    OAuth2 access token (not used if omitted).

Returns:

  • (Faraday::Connection)

    Created connection.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vkontakte_api/api.rb', line 26

def connection(options = {})
  url   = options.delete(:url)
  token = options.delete(:token)
  
  Faraday.new(url, VkontakteApi.faraday_options) do |builder|
    builder.request :oauth2, token, token_type: 'param' unless token.nil?
    builder.request :multipart
    builder.request :url_encoded
    builder.request :retry, VkontakteApi.max_retries
    
    builder.response :vk_logger
    builder.response :mashify
    builder.response :multi_json, preserve_raw: true
    
    builder.adapter VkontakteApi.adapter
  end
end