Module: YandexApiDirect::YandexObject::YandexObjectCallMethod

Defined in:
lib/yandex-api-direct/yandex_object.rb

Overview

Call method to Yandex API

Instance Method Summary collapse

Instance Method Details

#call_method(method_name, param = nil) ⇒ Object

Call method perform call to yandex api

  • method name (string|symbol) - method name defined by yandex api

  • param (optional) - params defined by yandex api



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/yandex-api-direct/yandex_object.rb', line 64

def call_method method_name, param = nil
  payload = {
    method: method_name.camelize,
    locale: YandexApiDirect.config[:locale],
    login: YandexApiDirect.config[:login],
    application_id: YandexApiDirect.config[:application_id],
    token: YandexApiDirect.config[:access_token],
  }
  payload[:param] = param if param

  # get url
  url = URI.parse(YandexApiDirect.url)

  # set new post request with JSON content type
  request = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json'})

  # set payload to post body
  request.body = payload.to_json

  # create new connection to yandex
  http = Net::HTTP.new(url.host, url.port)

  # set use SSL 
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  # send request 
  response = http.request(request)

  # no problem with connection
  if response.code.to_i == 200
    #parse respose JSON body into hash
    resp = JSON.parse(response.body).symbolize_keys

    # if there is no error_code
    unless resp.has_key?(:error_code)
      return resp
    else # error code
      case resp[:error_code]
      when 53 then raise YandexAuthorizationError, "Got error from Yandex API, error code: #{resp[:error_code]} | #{resp[:error_str]} (#{resp[:error_detail]})"
      when 55 then raise YandexMethodError, "Got error from Yandex API, error code: #{resp[:error_code]} | #{resp[:error_str]} (#{resp[:error_detail]})"
      else raise YandexError, "Got error from Yandex API, error code: #{resp[:error_code]} | #{resp[:error_str]} (#{resp[:error_detail]})"
      end
    end
  else
    # Request responce is not valid (errors: 404, 500, etc.)
    raise YandexConnectionError, "Problem with connection to Yandex API, status code: #{response.code} \n content body: #{response.body}"
  end
end

#camelize_keys(hash) ⇒ Object

Change Camelized keys of hash to camelized keys



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/yandex-api-direct/yandex_object.rb', line 115

def camelize_keys(hash)
  return nil unless hash
  return hash if hash.is_a?(Array)
  hash.inject({}){|result, (key, value)|
    new_key = key.to_s.camelize.gsub(/(Ids|Id|Fio)/) { |r| r.upcase }.to_sym
    new_value = case value
                when Hash then camelize_keys(value)
                else value
                end
    result[new_key] = new_value
    result
  }
end