Class: BnzApi::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: BnzApi.config) ⇒ Client

Returns a new instance of Client.



37
38
39
# File 'lib/bnz_api/client.rb', line 37

def initialize(config: BnzApi.config)
  @config = config
end

Class Method Details

.endpoint(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bnz_api/client.rb', line 6

def self.endpoint(name)
  define_method(name) do |**args|
    klass_name = Hanami::Utils::String.classify(name)
    endpoint_klass = Hanami::Utils::Class.load!("BnzApi::Endpoints::#{klass_name}")

    if config.http_persistent
      init_endpoint(klass_name, endpoint_klass, **args)
    else
      @endpoints = nil
      endpoint_klass.(config, **args)
    end
  end
end

Instance Method Details

#init_endpoint(klass_name, endpoint_klass, **args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bnz_api/client.rb', line 20

def init_endpoint(klass_name, endpoint_klass, **args)
  @endpoints ||= {}
  # TODO: Restructure the code so that we can have
  # a cleaner implementation of caching here
  # https://github.com/Kaligo/partner_api/pull/39#discussion_r1758041952
  if @endpoints[klass_name]
    @endpoints[klass_name].override_data(**args)
  else
    @endpoints[klass_name] = endpoint_klass.new(config, **args)
  end

  @endpoints[klass_name].call
end