class SoapyYandex::Client

Attributes

opts[R]

Public Class Methods

new(opts) click to toggle source
# File lib/soapy_yandex/client.rb, line 5
def initialize(opts)
  @opts = opts
end

Protected Instance Methods

run(request) click to toggle source
# File lib/soapy_yandex/client.rb, line 11
def run(request)
  http_request = HTTParty.post(
    uri_base + request.api_path,
    headers: headers,
    ssl_ca_file: ca_file,
    pem: ssl_cert.to_pem + ssl_key.to_pem,
    body: sign(request.to_s)
  )
  extract_response(http_request.body)
end

Private Instance Methods

ca_file() click to toggle source
# File lib/soapy_yandex/client.rb, line 57
def ca_file
  SoapyYandex.root.join('YMchain.pem').to_s
end
empty_cert_store() click to toggle source
# File lib/soapy_yandex/client.rb, line 37
def empty_cert_store
  @empty_cert_store ||= OpenSSL::X509::Store.new
end
extract_response(body) click to toggle source
# File lib/soapy_yandex/client.rb, line 26
def extract_response(body)
  message = OpenSSL::PKCS7.new(body)
  unless message.verify([remote_cert], empty_cert_store, nil, OpenSSL::PKCS7::NOVERIFY)
    raise Error, 'Response signature verification failed'
  end

  response = Response.new(message.data)
  raise ServerError, response.error_code if response.error?
  response
end
headers() click to toggle source
# File lib/soapy_yandex/client.rb, line 61
def headers
  { 'Content-Type' => 'application/pkcs7-mime' }
end
remote_cert() click to toggle source
# File lib/soapy_yandex/client.rb, line 41
def remote_cert
  @remote_cert ||= OpenSSL::X509::Certificate.new(opts[:remote_cert])
end
sign(payload) click to toggle source
# File lib/soapy_yandex/client.rb, line 53
def sign(payload)
  OpenSSL::PKCS7.sign(ssl_cert, ssl_key, payload, [], OpenSSL::PKCS7::BINARY).to_s
end
ssl_cert() click to toggle source
# File lib/soapy_yandex/client.rb, line 45
def ssl_cert
  @ssl_cert ||= OpenSSL::X509::Certificate.new(opts[:ssl_cert])
end
ssl_key() click to toggle source
# File lib/soapy_yandex/client.rb, line 49
def ssl_key
  @ssl_key ||= OpenSSL::PKey::RSA.new(opts[:ssl_key], opts[:ssl_key_passphrase])
end
uri_base() click to toggle source
# File lib/soapy_yandex/client.rb, line 65
def uri_base
  'https://' + opts[:server]
end