Class: Artemis::Adapters::CurbAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/artemis/adapters/curb_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

#connection, #headers

Constructor Details

#initialize(uri, service_name:, timeout:, pool_size:) ⇒ CurbAdapter

Returns a new instance of CurbAdapter.



16
17
18
19
20
21
# File 'lib/artemis/adapters/curb_adapter.rb', line 16

def initialize(uri, service_name: , timeout: , pool_size: )
  super

  @multi = Curl::Multi.new
  @multi.pipeline = Curl::CURLPIPE_MULTIPLEX if defined?(Curl::CURLPIPE_MULTIPLEX)
end

Instance Attribute Details

#multiObject (readonly)

Returns the value of attribute multi.



14
15
16
# File 'lib/artemis/adapters/curb_adapter.rb', line 14

def multi
  @multi
end

Instance Method Details

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/artemis/adapters/curb_adapter.rb', line 23

def execute(document:, operation_name: nil, variables: {}, context: {})
  easy = Curl::Easy.new(uri.to_s)

  body = {}
  body["query"] = document.to_query_string
  body["variables"] = variables if variables.any?
  body["operationName"] = operation_name if operation_name

  easy.timeout     = timeout
  easy.multi       = multi
  easy.headers     = DEFAULT_HEADERS.merge(headers(context))
  easy.post_body   = JSON.generate(body)

  if defined?(Curl::CURLPIPE_MULTIPLEX)
    # This ensures libcurl waits for the connection to reveal if it is
    # possible to pipeline/multiplex on before it continues.
    easy.setopt(Curl::CURLOPT_PIPEWAIT, 1)
    easy.version = Curl::HTTP_2_0
  end

  easy.http_post

  case easy.response_code
  when 200, 400
    JSON.parse(easy.body)
  when 500..599
    raise Artemis::GraphQLServerError, "Received server error status #{easy.response_code}: #{easy.body}"
  else
    { "errors" => [{ "message" => "#{easy.response_code} #{easy.body}" }] }
  end
end