Sha256: dc412da537e18eba2e6af986ef079b2c6913ce7394d6567df7e1b53e32117132

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# Орерации(действия)
module Operation
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    # Метод описания операции
    # @ name    : Название операции
    # @ url     : url = название_сервиса + / + название_операции
    # @ options : Параметры для операции
    #     - defaults : Значения по умолчанию для Body
    def operation(name, url, options = {})
      options[:defaults] ||= {}
      service, operation = url.split('/')
      model_name = self.name.split('::').last.downcase

      Tehportal::Model.operations[model_name] ||= {}
      Tehportal::Model.operations[model_name][name] = {service: service, operation: operation}

      # Добавляем синглтон метод к классу модели
      define_singleton_method(name) do |*args|
        args[0] ||= {}
        client = args[0].delete(:client) || Tehportal.client
        message = options[:defaults].merge(args[0])
        
        unless options[:cache] and result = Rails.cache.fetch([:tehportal, service, operation, message])
          result = client.request(service, operation, message)
          if options[:cache] and !result.include?(:errorMessage)
            Rails.cache.write([:tehportal, service, operation, message], result, expires_in: options[:cache])
          end
        end
        
        if options[:as] == :list
          if result.include?(:errorMessage)
            if result[:errorMessage] =~ /^ERROR \(302\)/
              result = []
            end
          else
            result = Array.wrap(result['getListValues'] || [result])
          end
        end
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tehportal-0.1.3 lib/tehportal/operation.rb