Class: Anvil::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, config: nil) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/anvil/client.rb', line 14

def initialize(api_key: nil, config: nil)
  @config = config || Anvil.configuration.dup
  @config.api_key = api_key if api_key
  @config.validate!
  @rate_limiter = RateLimiter.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/anvil/client.rb', line 12

def config
  @config
end

#rate_limiterObject (readonly)

Returns the value of attribute rate_limiter.



12
13
14
# File 'lib/anvil/client.rb', line 12

def rate_limiter
  @rate_limiter
end

Instance Method Details

#delete(path, params = {}, options = {}) ⇒ Object



33
34
35
# File 'lib/anvil/client.rb', line 33

def delete(path, params = {}, options = {})
  request(:delete, path, params: params, **options)
end

#get(path, params = {}, options = {}) ⇒ Object



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

def get(path, params = {}, options = {})
  request(:get, path, params: params, **options)
end

#graphql(query_string, variables: {}) ⇒ Hash

Execute a raw GraphQL request

Parameters:

  • query_string (String)

    The GraphQL query or mutation string

  • variables (Hash) (defaults to: {})

    Variables to pass to the query

Returns:

  • (Hash)

    The parsed response data



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/anvil/client.rb', line 42

def graphql(query_string, variables: {})
  response = post(config.graphql_url, {
                    query: query_string,
                    variables: variables
                  })

  body = response.body
  body = response.data if body.is_a?(Anvil::Response)

  if body.is_a?(Hash) && body[:errors] && !body[:errors].empty?
    messages = body[:errors].map { |e| e[:message] || e['message'] }.compact
    raise GraphQLError.new(
      "GraphQL errors: #{messages.join(', ')}",
      response,
      graphql_errors: body[:errors]
    )
  end

  body.is_a?(Hash) ? body[:data] : body
end

#mutation(mutation:, variables: {}) ⇒ Hash

Execute a GraphQL mutation

Parameters:

  • mutation (String)

    The GraphQL mutation string

  • variables (Hash) (defaults to: {})

    Variables to pass to the mutation

Returns:

  • (Hash)

    The mutation result data



77
78
79
# File 'lib/anvil/client.rb', line 77

def mutation(mutation:, variables: {})
  graphql(mutation, variables: variables)
end

#post(path, data = {}, options = {}) ⇒ Object



25
26
27
# File 'lib/anvil/client.rb', line 25

def post(path, data = {}, options = {})
  request(:post, path, body: data, **options)
end

#put(path, data = {}, options = {}) ⇒ Object



29
30
31
# File 'lib/anvil/client.rb', line 29

def put(path, data = {}, options = {})
  request(:put, path, body: data, **options)
end

#query(query:, variables: {}) ⇒ Hash

Execute a GraphQL query

Parameters:

  • query (String)

    The GraphQL query string

  • variables (Hash) (defaults to: {})

    Variables to pass to the query

Returns:

  • (Hash)

    The query result data



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

def query(query:, variables: {})
  graphql(query, variables: variables)
end