Class: PlanMyStuff::Client

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

Overview

Infrastructure wrapper around Octokit. Handles auth, error normalization, and repo resolution. Domain modules (Issues, Projects, etc.) use this internally via PlanMyStuff.client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient



22
23
24
25
26
# File 'lib/plan_my_stuff/client.rb', line 22

def initialize
  PlanMyStuff.configuration.validate!

  @octokit = Octokit::Client.new(access_token: PlanMyStuff.configuration.access_token)
end

Instance Attribute Details

#octokitOctokit::Client (readonly)

Returns:

  • (Octokit::Client)


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

def octokit
  @octokit
end

Instance Method Details

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

Executes a GraphQL query against GitHub’s /graphql endpoint.

Parameters:

  • query (String)

    GraphQL query string

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

    GraphQL variables

Returns:

  • (Hash)

    parsed response data



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/plan_my_stuff/client.rb', line 55

def graphql(query, variables: {})
  payload = { query: query }
  payload[:variables] = variables unless variables.empty?

  response = octokit.post('/graphql', payload.to_json)
  data =
    if response.is_a?(Hash)
      response
    else
      (response.respond_to?(:to_h) ? response.to_h : response)
    end

  data = data.deep_symbolize_keys if data.respond_to?(:deep_symbolize_keys)

  check_graphql_errors!(data)

  data[:data]
rescue Octokit::TooManyRequests => e
  raise_rate_limit_error(e)
rescue Octokit::ClientError, Octokit::ServerError => e
  raise(APIError.new(e.message, status: e.respond_to?(:response_status) ? e.response_status : nil))
end

#last_responseFaraday::Response?

Returns the Faraday response from the most recent Octokit call. Useful for reading headers (e.g. ‘ETag`, `X-RateLimit-Remaining`) that aren’t included in the parsed response body.

Returns:

  • (Faraday::Response, nil)


19
# File 'lib/plan_my_stuff/client.rb', line 19

delegate :last_response, to: :octokit

#resolve_repo(repo = nil) ⇒ String

Resolves a repo param to a full “Org/Repo” string.

Parameters:

  • repo (Symbol, String, PlanMyStuff::Repo, nil) (defaults to: nil)

    repo key, full string, Repo instance, or nil for default

Returns:

  • (String)

    full repo path (e.g. “BrandsInsurance/Element”)

Raises:

  • (ArgumentError)

    if repo cannot be resolved



86
87
88
# File 'lib/plan_my_stuff/client.rb', line 86

def resolve_repo(repo = nil)
  PlanMyStuff::Repo.resolve(repo).full_name
end

#rest(method, **kwargs) ⇒ Object

Delegates a REST API call to Octokit, normalizing errors.

Parameters:

  • method (Symbol)

    Octokit method name (e.g. :create_issue)

  • args (Array)

    positional arguments

  • kwargs (Hash)

    keyword arguments

Returns:

  • (Object)

    Octokit response



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plan_my_stuff/client.rb', line 36

def rest(method, *, **kwargs, &)
  if kwargs.empty?
    octokit.public_send(method, *, &)
  else
    octokit.public_send(method, *, **kwargs, &)
  end
rescue Octokit::TooManyRequests => e
  raise_rate_limit_error(e)
rescue Octokit::ClientError, Octokit::ServerError => e
  raise(APIError.new(e.message, status: e.respond_to?(:response_status) ? e.response_status : nil))
end