Class: Ya::API::Direct::UrlHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ya/api/direct/url_helper.rb

Overview

Static class with helping functions for url formatting.

Author:

  • Rikki Mongoose

Class Method Summary collapse

Class Method Details

.direct_api_url(mode = :sandbox, version = :v5, service = "") ⇒ String

Generate Yandex Direct API url for a call

Parameters:

  • mode (Symbol) (defaults to: :sandbox)

    Direct API mode, ‘:sandbox` or `:production`

  • mode (Symbol) (defaults to: :sandbox)

    Direct API version, ‘:v5` or `:v4` or `:v4live`

  • mode (Symbol) (defaults to: :sandbox)

    Direct API service to use

Returns:

  • (String)

    Generated Yandex Direct API URL



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ya/api/direct/url_helper.rb', line 18

def self.direct_api_url(mode = :sandbox, version = :v5, service = "")
     format = :json
		protocol = "https"
		api_prefixes = {
			sandbox: "api-sandbox",
			production: "api"
		}
     api_prefix = api_prefixes[mode || :sandbox]
     site = "%{api}.direct.yandex.ru" % {api: api_prefix}
		api_urls = {
			v4: {
				json: '%{protocol}://%{site}/v4/json',
				soap: '%{protocol}://%{site}/v4/soap',
				wsdl: '%{protocol}://%{site}/v4/wsdl',
				},
			v4live: {
				json: '%{protocol}://%{site}/live/v4/json',
				soap: '%{protocol}://%{site}/live/v4/soap',
				wsdl: '%{protocol}://%{site}/live/v4/wsdl',
				},
			v5: {
				json: '%{protocol}://%{site}/json/v5/%{service}',
				soap: '%{protocol}://%{site}/v5/%{service}',
				wsdl: '%{protocol}://%{site}/v5/%{service}?wsdl',
			    }
			}
     api_urls[version][format] % {
       protocol: protocol,
       site: site,
       service: service
     }

end

.extract_response_units(response_header) ⇒ Hash

Extract Yandex Direct API units values from responce HTTP header

Parameters:

  • responce_header (Hash)

    Yandex Direct API response header

Returns:

  • (Hash)

    Units data, extracted from header



56
57
58
59
60
61
62
63
64
# File 'lib/ya/api/direct/url_helper.rb', line 56

def self.extract_response_units(response_header)
  matched = RegExUnits.match response_header["Units"]
  matched.nil? ? {} :
  {
    just_used: matched[1].to_i,
    units_left: matched[2].to_i,
    units_limit: matched[3].to_i
  }
end