Class: Anvil::Client
- Inherits:
-
Object
- Object
- Anvil::Client
- Defined in:
- lib/anvil/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#rate_limiter ⇒ Object
readonly
Returns the value of attribute rate_limiter.
Instance Method Summary collapse
- #delete(path, params = {}, options = {}) ⇒ Object
- #get(path, params = {}, options = {}) ⇒ Object
-
#graphql(query_string, variables: {}) ⇒ Hash
Execute a raw GraphQL request.
-
#initialize(api_key: nil, config: nil) ⇒ Client
constructor
A new instance of Client.
-
#mutation(mutation:, variables: {}) ⇒ Hash
Execute a GraphQL mutation.
- #post(path, data = {}, options = {}) ⇒ Object
- #put(path, data = {}, options = {}) ⇒ Object
-
#query(query:, variables: {}) ⇒ Hash
Execute a GraphQL query.
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/anvil/client.rb', line 12 def config @config end |
#rate_limiter ⇒ Object (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 = {}, = {}) request(:delete, path, params: params, **) end |
#get(path, params = {}, options = {}) ⇒ Object
21 22 23 |
# File 'lib/anvil/client.rb', line 21 def get(path, params = {}, = {}) request(:get, path, params: params, **) end |
#graphql(query_string, variables: {}) ⇒ Hash
Execute a raw GraphQL request
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? = body[:errors].map { |e| e[:message] || e['message'] }.compact raise GraphQLError.new( "GraphQL errors: #{.join(', ')}", response, graphql_errors: body[:errors] ) end body.is_a?(Hash) ? body[:data] : body end |
#mutation(mutation:, variables: {}) ⇒ Hash
Execute a GraphQL mutation
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 = {}, = {}) request(:post, path, body: data, **) end |
#put(path, data = {}, options = {}) ⇒ Object
29 30 31 |
# File 'lib/anvil/client.rb', line 29 def put(path, data = {}, = {}) request(:put, path, body: data, **) end |
#query(query:, variables: {}) ⇒ Hash
Execute a GraphQL query
68 69 70 |
# File 'lib/anvil/client.rb', line 68 def query(query:, variables: {}) graphql(query, variables: variables) end |