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



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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/plan_my_stuff/client.rb', line 48

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

#resolve_repo(repo = nil) ⇒ String

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

Parameters:

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

    repo key, full string, or nil for default

Returns:

  • (String)

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

Raises:

  • (ArgumentError)

    if repo cannot be resolved



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/plan_my_stuff/client.rb', line 79

def resolve_repo(repo = nil)
  repo ||= PlanMyStuff.configuration.default_repo

  if repo.nil?
    raise(
      PlanMyStuff::ConfigurationError,
      'No repo provided and config.default_repo is not set. ' \
        'Either pass repo: explicitly or set config.default_repo in your initializer.',
    )
  end

  case repo
  when Symbol
    resolved = PlanMyStuff.configuration.repos[repo]
    raise(ArgumentError, "Unknown repo key: #{repo.inspect}") if resolved.nil?

    resolved
  when String
    repo
  else
    raise(ArgumentError, "Cannot resolve repo: #{repo.inspect}")
  end
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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plan_my_stuff/client.rb', line 29

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