Class: NfseSjc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nfse_sjc/client.rb

Constant Summary collapse

PARAM_KEYS =
[:pretty_print_xml, :log, :wsdl, :ssl_cert_file,
:ssl_cert_key_file, :ssl_cert_key_password].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/nfse_sjc/client.rb', line 6

def initialize(params = {})
  @savon  = Savon.client slice(params, *PARAM_KEYS).merge(NfseSjc.config(at: PARAM_KEYS))
  @parser = Parser.new
  @header = Document.new(Dirs.template('cabecalho_v3.xml.erb'), {}).to_xml.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



36
37
38
# File 'lib/nfse_sjc/client.rb', line 36

def method_missing(name, *args, &block)
  call(name, *args, &block)
end

Instance Method Details

#call(method, params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nfse_sjc/client.rb', line 12

def call(method, params)
  schema = Schemas.get_schema("#{method}.xml.erb")
  schema_path = File.join Dirs.etc, 'schemas', schema
  xsd = Nokogiri::XML::Schema(File.open schema_path)
  document = Document.new(Dirs.template("#{method}.xml.erb"), params).to_signed_xml

  errors = xsd.validate(Nokogiri::XML(document))

  if errors.any?
    message = errors.map do |err|
      "* #{err}".gsub('{http://www.ginfes.com.br/tipos_v03.xsd}','').gsub('Element ', '')
    end.join("\n")

    raise NfseSjc::Errors::ValidationError.new("XML with invalid or incorrect data according to schema #{schema}, found errors:\n #{message}")
  else
    response = @savon.call(method, message: {
      arg0: @header,
      arg1: document
    })

    @parser.parse(response.body[:"#{method}_response"][:return])
  end
end