Class: PlanMyStuff::Client
- Inherits:
-
Object
- Object
- PlanMyStuff::Client
- 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
- #octokit ⇒ Octokit::Client readonly
Instance Method Summary collapse
-
#graphql(query, variables: {}) ⇒ Hash
Executes a GraphQL query against GitHub’s /graphql endpoint.
- #initialize ⇒ Client constructor
-
#last_response ⇒ Faraday::Response?
Returns the Faraday response from the most recent Octokit call.
-
#resolve_repo(repo = nil) ⇒ String
Resolves a repo param to a full “Org/Repo” string.
-
#rest(method, **kwargs) ⇒ Object
Delegates a REST API call to Octokit, normalizing errors.
Constructor Details
#initialize ⇒ Client
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
#octokit ⇒ Octokit::Client (readonly)
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.
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., status: e.respond_to?(:response_status) ? e.response_status : nil)) end |
#last_response ⇒ Faraday::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.
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.
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.
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., status: e.respond_to?(:response_status) ? e.response_status : nil)) end |