Class: Ksef::Client

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

Overview

Main entry point for the KSeF API.

Examples:

client = Ksef::Client.new(
  nip: "1234567890",
  credentials: Ksef::Credentials::Token.new(ENV.fetch("KSEF_TOKEN"))
)

client.sessions.with_interactive do |session|
  headers = client.invoices.query(
    subject_type: :recipient,
    date_from: Time.now.utc - (7 * 24 * 3600),
    date_to:   Time.now.utc
  )
  xml = client.invoices.fetch_xml(headers.first.ksef_reference_number)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nip:, credentials:, configuration: nil) ⇒ Client

Returns a new instance of Client.

Raises:



24
25
26
27
28
29
30
31
# File 'lib/ksef/client.rb', line 24

def initialize(nip:, credentials:, configuration: nil)
  raise ConfigurationError, "nip cannot be blank" if nip.nil? || nip.to_s.empty?

  @nip             = nip.to_s
  @credentials     = credentials
  @configuration   = configuration || Ksef.configuration.dup
  @current_session = nil
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

#current_sessionObject

Returns the value of attribute current_session.



22
23
24
# File 'lib/ksef/client.rb', line 22

def current_session
  @current_session
end

#nipObject (readonly)

Returns the value of attribute nip.



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

def nip
  @nip
end

Instance Method Details

#connectionObject

Lazily-built HTTP connection. Public so the resource classes can share it; not part of the supported public surface — treat as internal.



35
36
37
# File 'lib/ksef/client.rb', line 35

def connection
  @connection ||= Internal::Connection.new(@configuration)
end

#invoicesObject



43
44
45
# File 'lib/ksef/client.rb', line 43

def invoices
  @invoices ||= Invoices.new(self)
end

#sessionsObject



39
40
41
# File 'lib/ksef/client.rb', line 39

def sessions
  @sessions ||= Sessions.new(self)
end