Class: Artemis::Adapters::MultiDomainAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/artemis/adapters/multi_domain_adapter.rb

Constant Summary

Constants inherited from AbstractAdapter

AbstractAdapter::DEFAULT_HEADERS, AbstractAdapter::EMPTY_HEADERS

Instance Attribute Summary collapse

Attributes inherited from AbstractAdapter

#pool_size, #service_name, #timeout

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#headers

Constructor Details

#initialize(_uri, service_name:, timeout:, pool_size:, adapter_options: {}) ⇒ MultiDomainAdapter

Returns a new instance of MultiDomainAdapter.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 10

def initialize(_uri, service_name: , timeout: , pool_size: , adapter_options: {})
  if adapter_options[:adapter] == :multi_domain
    raise ArgumentError, "You can not use the :multi_domain adapter with the :multi_domain adapter."
  end

  @connection_by_url    = {}
  @service_name         = service_name.to_s
  @timeout              = timeout
  @pool_size            = pool_size
  @adapter              = adapter_options[:adapter] || :net_http
  @mutex_for_connection = Mutex.new
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 8

def adapter
  @adapter
end

Instance Method Details

#connectionObject

Raises:

  • (NotImplementedError)


46
47
48
49
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 46

def connection
  raise NotImplementedError, "Calling the #connection method without a URI is not supported. Please use the " \
                             "#connection_for_url(uri) instead."
end

#connection_for_url(url) ⇒ Object



51
52
53
54
55
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 51

def connection_for_url(url)
  @connection_by_url[url.to_s] || @mutex_for_connection.synchronize do
    @connection_by_url[url.to_s] ||= ::Artemis::Adapters.lookup(adapter).new(url, service_name: service_name, timeout: timeout, pool_size: pool_size)
  end
end

#execute(document:, operation_name: nil, variables: {}, context: {}) ⇒ Object

Makes an HTTP request for GraphQL query.



35
36
37
38
39
40
41
42
43
44
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 35

def execute(document:, operation_name: nil, variables: {}, context: {})
  url = context[:url]

  if url.nil?
    raise ArgumentError, 'The MultiDomain adapter requires a url on every request. Please specify a url with a context: ' \
                         'Client.with_context(url: "https://awesomeshop.domain.conm")'
  end

  connection_for_url(url).execute(document: document, operation_name: operation_name, variables: variables, context: context)
end

#multiplex(queries, context: {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/artemis/adapters/multi_domain_adapter.rb', line 23

def multiplex(queries, context: {})
  url = context[:url]

  if url.nil?
    raise ArgumentError, 'The MultiDomain adapter requires a url on every request. Please specify a url with a context: ' \
                         'Client.multiplex(url: "https://awesomeshop.domain.conm") { ... }'
  end

  connection_for_url(url).multiplex(queries, context: context)
end