Class: Gdexpress::Client

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

Constant Summary collapse

CONFIG_OPTIONS =
[:api_token, :dte_box, :environment]
GD_ENVIRONMENTS =
{
  testing: "T",
  production: "P"
}
API_ENDPOINTS =
{
  fiscal_status: "/api/Core.svc/Core/FiscalStatus/",
  tracking: "/api/Core.svc/Core/Tracking/",
  recover_pdf: "/api/Core.svc/Core/RecoverPdf/",
  recover_xml: "/api/Core.svc/Core/RecoverXml/",
}
GDE_STATUS_CODES =
{
  processed: 0,
  processing_dte: 1,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client



23
24
25
# File 'lib/gdexpress/client.rb', line 23

def initialize(config = {})
  set_config(config)
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



21
22
23
# File 'lib/gdexpress/client.rb', line 21

def api_token
  @api_token
end

#dte_boxObject

Returns the value of attribute dte_box.



21
22
23
# File 'lib/gdexpress/client.rb', line 21

def dte_box
  @dte_box
end

#environmentObject

Returns the value of attribute environment.



21
22
23
# File 'lib/gdexpress/client.rb', line 21

def environment
  @environment
end

Instance Method Details

#accepted?(dte) ⇒ Boolean



47
48
49
50
# File 'lib/gdexpress/client.rb', line 47

def accepted?(dte)
  response = fiscal_status(dte)
  response[:status].to_i == GDE_STATUS_CODES[:processed]
end

#find_dte(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/gdexpress/client.rb', line 27

def find_dte(params = {})      
  raise ArgumentError, "Missing config param" unless config_params_valid?(params, [:rut_emisor, :tipo_dte, :folio])
  Gdexpress::Dte.new params
end

#fiscal_status(dte) ⇒ Object



52
53
54
55
# File 'lib/gdexpress/client.rb', line 52

def fiscal_status(dte)
  response = get "fiscal_status", dte
  response
end

#processed?(dte) ⇒ Boolean



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gdexpress/client.rb', line 32

def processed?(dte)
  begin
    response = fiscal_status dte  
  rescue DTENotFound => e
    # Si no lo encontramos puede que aun no haya llegado a
    # GDexpress Cloud, por eso decimos que no se ha procesado
    return false
  end
  
  # Estamos confiando que GDexpress devuelve Status = 1
  # mientras se espera respuesta de SII
  return false if response[:status].to_i == GDE_STATUS_CODES[:processing_dte]
  true
end

#recover_pdf_base64(dte) ⇒ Object



62
63
64
65
# File 'lib/gdexpress/client.rb', line 62

def recover_pdf_base64(dte)
  response = get "recover_pdf", dte
  response[:data]
end

#recover_xml(dte) ⇒ Object



67
68
69
70
# File 'lib/gdexpress/client.rb', line 67

def recover_xml(dte)
  response = get "recover_xml", dte
  response
end

#tracking(dte) ⇒ Object



57
58
59
60
# File 'lib/gdexpress/client.rb', line 57

def tracking(dte)
  response = get "tracking", dte
  response
end