Module: YandexDirect
- Defined in:
- lib/yandex_direct_api.rb
Defined Under Namespace
Classes: RuntimeError
Class Method Summary collapse
- .configuration ⇒ Object
- .get_units ⇒ Object
- .load(file, env = nil) ⇒ Object
- .parse_json(json) ⇒ Object
- .request(service, method, params = {}, units = false) ⇒ Object
- .url ⇒ Object
Class Method Details
.configuration ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/yandex_direct_api.rb', line 15 def self.configuration if defined? @environment raise RuntimeError.new("Configure yandex-direct-api for #{@environment} environment.") unless @configuration else raise RuntimeError.new('Configure yandex-direct-api for current environment.') unless @configuration end @configuration end |
.get_units ⇒ Object
81 82 83 84 |
# File 'lib/yandex_direct_api.rb', line 81 def self.get_units units = request('dictionaries', 'get', {"DictionaryNames": ["AdCategories"]}, true) units.split('/')[1].to_i end |
.load(file, env = nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/yandex_direct_api.rb', line 32 def self.load file, env = nil @environment = env.to_s if env config = YAML.load_file(file) @configuration = defined?(@environment) ? config[@environment] : config end |
.parse_json(json) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/yandex_direct_api.rb', line 24 def self.parse_json json begin return JSON.parse(json) rescue => e raise RuntimeError.new "#{e.} in response." end end |
.request(service, method, params = {}, units = false) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/yandex_direct_api.rb', line 38 def self.request(service, method, params = {}, units = false) uri = URI(url + service) body = { method: method, params: params } puts "\t\033[32mYandexDirect:\033[0m #{service}.#{method}(#{body[:params]})" if configuration['verbose'] && !units http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE # http.set_debug_output($stdout) request = Net::HTTP::Post.new(uri.path) request['Authorization'] = "Bearer #{configuration['token']}" request['Accept-Language'] = configuration['locale'] request['Client-Login'] = configuration['login'].downcase.gsub('.', '-') request.body = JSON.generate(body) response = http.start do |http| http.request(request) end raise YandexDirect::RuntimeError.new("#{response.code} - #{response.}") unless response.code.to_i == 200 return response['Units'] if units json = parse_json(response.body) = [] if json.has_key?('error') .push("#{json['error']['error_code'].to_i} - #{json['error']['error_string']}: #{json['error']['error_detail']}") else value = json['result'].present? ? (json['result'].values.first || json['result']) : json value = value.values if value.kind_of?(Hash) value.each do |item| item['Errors'].each do |error| .push("#{error['Code'].to_i} - #{error['Message']}: #{error['Details']}") end if item.has_key?('Errors') end if value.present? end raise RuntimeError.new(.join("\n")) if .any? return json['result'] end |
.url ⇒ Object
11 12 13 |
# File 'lib/yandex_direct_api.rb', line 11 def self.url configuration['sandbox'] ? 'https://api-sandbox.direct.yandex.com/json/v5/' : 'https://api.direct.yandex.com/json/v5/' end |