Class: Yadirect::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy.rb

Constant Summary collapse

EP_YANDEX_DIRECT_V4 =
'https://soap.direct.yandex.ru/json-api/v4/'

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Proxy

Returns a new instance of Proxy.



12
13
14
15
# File 'lib/proxy.rb', line 12

def initialize params
  @params = params
  @locale = 'RU' || params[:locale]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



41
42
43
44
# File 'lib/proxy.rb', line 41

def method_missing(name, *args, &block)
  ya_params = to_hash_params(*args)
  invoke(name.to_s.to_camelcase, ya_params)
end

Instance Method Details

#invoke(method, args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/proxy.rb', line 17

def invoke method, args = {}
  json_object = {:method => method, :locale => @locale, :param => args}.to_json 
  puts json_object.to_s
  c = Curl::Easy.http_post(EP_YANDEX_DIRECT_V4, json_object) do |curl|
    curl.cacert = @params[:cacert]
    curl.cert_key = @params[:cert_key]
    curl.certtype = "PEM"
    curl.cert = @params[:cert]
    curl.headers['Accept'] = 'application/json'
    curl.headers['Content-Type'] = 'application/json'
    curl.headers['Api-Version'] = '2.2'
    curl.verbose = true if @params[:debug] == true
  end
  
  hash =  JSON.parse(c.body_str)
   
  if(hash.include?("error_code"))
    raise Yadirect::ApiError, hash
  else
    object_result = Hashit.new(hash)
    object_result.data
  end
end

#to_hash_params(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/proxy.rb', line 46

def to_hash_params *args
  return {} if args.empty?
  first_arg = args.shift
  if first_arg.is_a?(Hash)
    return first_arg.camelize_keys
  else
    return args
  end
end