Sha256: 2bedb3390da76a08b8defb9bdc2451a7c3e8501a9e3256b93b58f961381751f6

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require 'active_support/inflector'
require 'json'

require_relative 'request'

require_relative 'schemas/echo'
require_relative 'schemas/create_beneficiary_ul'

module TochkaCyclopsApi
  module DataProcessor
    def send_request(method, data)
      @method = method
      @data = data

      if valid_params?
        send_data
      else
        return @errors.map{ |k,v| [k, v].join(' ')}.join(', ')
      end
    end

    private

    def valid_params?
      if shape
        result = shape.call(@data)
        @errors = result.errors.to_h
      end

      @errors.empty?
    end

    def send_data
      TochkaCyclopsApi::Request.send(body)
    end

    def shape
      begin
        schema = ('TochkaCyclopsApi::Schemas::' + camel_case_method).constantize
        schema.new
      rescue => e
        @errors = {error: e.message}
        false
      end
    end

    def camel_case_method
      @method.split('_').map(&:capitalize).join
    end

    def body
      {
        "jsonrpc": "2.0",
        "method": @method,
        "params": @data,
        "id": "908ca508-f1f1-4256-9c43-9ba7ad9c45fb"
      }.to_json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tochka_cyclops_api-0.1.0 lib/tochka_cyclops_api/data_processor.rb