Class: Ask::Linear::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    Linear personal API key



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.options.read_timeout = 30
    f.options.open_timeout = 10
    f.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionFaraday::Connection (readonly)

Returns the underlying Faraday connection.

Returns:

  • (Faraday::Connection)

    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.

Parameters:

  • gql (String)

    GraphQL query or mutation string

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

    Variables to interpolate into the query (default: {})

Returns:

  • (Hash)

    Parsed response body from the Linear API

Raises:

  • (ArgumentError)

    if gql is not a String or variables is not a Hash

  • (RuntimeError)

    if the API returns errors in the response body

  • (Faraday::Error)

    if the HTTP request fails



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"]
    messages = body["errors"].map { |e| e["message"] }.join("; ")
    raise ::RuntimeError, "Linear API error: #{messages}"
  end

  body
end