class Nfe::Operations::Base

Attributes

errors[R]
result[R]

Public Class Methods

new(certificate, private_key, use_schema: true) click to toggle source
# File lib/nfe/operations/base.rb, line 8
def initialize(certificate, private_key, use_schema: true)
  @errors = ActiveModel::Errors.new(self)
  @certificate = certificate
  @private_key = private_key
  @use_schema = use_schema
  validate_certificate
end

Public Instance Methods

execute() click to toggle source
# File lib/nfe/operations/base.rb, line 16
def execute
  if errors.empty?
    before_execute
    call_webservice if valid?
  end

  errors.empty?
end

Protected Instance Methods

before_execute() click to toggle source
# File lib/nfe/operations/base.rb, line 26
def before_execute
end
valid?() click to toggle source
# File lib/nfe/operations/base.rb, line 29
def valid?
  validate_with_validators
  validate_with_schema
  errors.empty?
end

Private Instance Methods

call_webservice() click to toggle source
# File lib/nfe/operations/base.rb, line 36
def call_webservice
  sender = create_webservice_client
  if sender.execute(@object)
    create_result(sender.result)
  elsif
    sender.errors.full_messages.each do |error|
      errors.add(:base, I18n.t("webservice.comunication", error: error))
    end
  end
end
create_result(response) click to toggle source
# File lib/nfe/operations/base.rb, line 84
def create_result(response)
  @result = handle_result(parsed_response(response))
rescue
  errors.add(:base, I18n.t("xml.unexpected_format",
    xml: response.to_xml))
end
create_webservice_client() click to toggle source
# File lib/nfe/operations/base.rb, line 80
def create_webservice_client
  Webservices::Sender.new(@certificate, @private_key, ws_info)
end
parsed_response(response) click to toggle source
# File lib/nfe/operations/base.rb, line 91
def parsed_response(response)
  response.xpath(xpath, ns: Nfe::Models::NFe.namespace).to_xml
end
schema() click to toggle source
# File lib/nfe/operations/base.rb, line 76
def schema
  File.join(File.expand_path("../../schemas", __FILE__), schema_name)
end
sign(object, id) click to toggle source
# File lib/nfe/operations/base.rb, line 95
def sign(object, id)
  signer = Helpers::Signer.new(@certificate, @private_key)
  signer.execute(object, id)
end
validate_certificate() click to toggle source
# File lib/nfe/operations/base.rb, line 64
def validate_certificate
  if @certificate.nil? || !(@certificate.instance_of? OpenSSL::X509::Certificate)
    errors.add(:base, I18n.t("webservice.invalid_certificate"))
  end

  if @private_key.nil? || !(@private_key.instance_of? OpenSSL::PKey::RSA)
    errors.add(:base, I18n.t("webservice.invalid_private_key"))
  end

  errors.empty?
end
validate_with_schema() click to toggle source
# File lib/nfe/operations/base.rb, line 51
def validate_with_schema
  return if errors.any?

  if @use_schema
    validator = Helpers::SchemaValidator.new(schema, @object.to_xml)
    validator.valid?

    validator.errors.full_messages.each do |error|
      errors.add(:base, error)
    end
  end
end
validate_with_validators() click to toggle source
# File lib/nfe/operations/base.rb, line 47
def validate_with_validators
  errors.add(:base, I18n.t("xml.invalid")) unless @object.valid?
end