Class: Ask::Linear::Client
- Inherits:
-
Object
- Object
- Ask::Linear::Client
- Defined in:
- lib/ask/linear/client.rb
Overview
A lightweight GraphQL client for the Linear API.
Constant Summary collapse
- BASE_URL =
Base URL for the Linear GraphQL API.
"https://api.linear.app/graphql"
Instance Attribute Summary collapse
-
#connection ⇒ Faraday::Connection
readonly
The underlying Faraday connection.
Instance Method Summary collapse
-
#initialize(api_key) ⇒ Client
constructor
A new instance of Client.
-
#query(gql, variables = {}) ⇒ Hash
Execute a GraphQL query or mutation against the Linear API.
Constructor Details
#initialize(api_key) ⇒ Client
Returns a new instance of Client.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ask/linear/client.rb', line 42 def initialize(api_key) @api_key = api_key @connection = Faraday.new(url: BASE_URL) do |f| f.request :json f.response :json f.response :raise_error f..read_timeout = 30 f..open_timeout = 10 f.adapter Faraday.default_adapter end end |
Instance Attribute Details
#connection ⇒ Faraday::Connection (readonly)
Returns the underlying Faraday connection.
39 40 41 |
# File 'lib/ask/linear/client.rb', line 39 def connection @connection end |
Instance Method Details
#query(gql, variables = {}) ⇒ Hash
Execute a GraphQL query or mutation against the Linear API.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ask/linear/client.rb', line 62 def query(gql, variables = {}) unless gql.is_a?(::String) raise ::ArgumentError, "gql must be a String, got #{gql.class}" end unless variables.is_a?(::Hash) raise ::ArgumentError, "variables must be a Hash, got #{variables.class}" end response = @connection.post do |req| req.headers["Authorization"] = @api_key req.body = { query: gql, variables: variables } end body = response.body if body.is_a?(Hash) && body["errors"] = body["errors"].map { |e| e["message"] }.join("; ") raise ::RuntimeError, "Linear API error: #{}" end body end |