class GithubApi::V4::Client

Constants

ClientError
ENDPOINT
ServerError
VERSION

Public Class Methods

new(access_token) click to toggle source

@param [String] access_token

# File lib/github_api/v4/client.rb, line 13
def initialize(access_token)
  @access_token = access_token
end

Public Instance Methods

graphql(query:, variables: {}) click to toggle source

@param [String] query @param [Hash] variables @return [Hash]

# File lib/github_api/v4/client.rb, line 20
def graphql(query:, variables: {})
  resp = post('/graphql', query: query, variables: variables)
  handle_errors(resp)
  JSON.parse(resp.body)
end
query_schema() click to toggle source

@return [Hash]

# File lib/github_api/v4/client.rb, line 68
def query_schema
  type('Query')
end
rate_limit() click to toggle source
# File lib/github_api/v4/client.rb, line 130
  def rate_limit
    graphql(query: <<~GRAPHQL).dig('data', 'rateLimit')
      query {
        rateLimit {
          remaining
          limit
        }
      }
    GRAPHQL
  end
schema() click to toggle source

@return [Hash]

# File lib/github_api/v4/client.rb, line 27
  def schema
    graphql(query: <<~GRAPHQL)
      query {
        __schema {
          types {
            kind
            name
            description
            fields(includeDeprecated: true) {
              name
              description
              args {
                name
                description
                type {
                  name
                }
                defaultValue
              }
              type {
                name
                description
              }
              isDeprecated
              deprecationReason
            }
            inputFields {
              name
              description
              type {
                name
              }
              defaultValue
            }
          }
        }
      }
    GRAPHQL
  end
type(name) click to toggle source

@param [String] name @return [Hash]

# File lib/github_api/v4/client.rb, line 74
  def type(name)
    graphql(query: <<~GRAPHQL)
      query {
        __type(name: #{name.dump}) {
          kind
          name
          description
          fields(includeDeprecated: true) {
            name
            description
            args {
              name
              description
              type {
                name
              }
              defaultValue
            }
            type {
              name
              description
              fields(includeDeprecated: true) {
                name
                description
                args {
                  name
                  description
                  type {
                    name
                  }
                  defaultValue
                }
                type {
                  name
                  description
                }
                isDeprecated
                deprecationReason
              }
            }
            isDeprecated
            deprecationReason
          }
          inputFields {
            name
            description
            type {
              name
            }
            defaultValue
          }
        }
      }
    GRAPHQL
  end

Private Instance Methods

handle_errors(resp) click to toggle source
# File lib/github_api/v4/client.rb, line 143
def handle_errors(resp)
  case resp
  when Net::HTTPClientError
    raise ClientError.new("#{resp.code}: #{resp.body}")
  when Net::HTTPServerError
    raise ServerError.new("#{resp.code}: #{resp.body}")
  end
end
post(path, params = {}) click to toggle source
# File lib/github_api/v4/client.rb, line 152
def post(path, params = {})
  Net::HTTP.start(ENDPOINT.host, ENDPOINT.port, use_ssl: ENDPOINT.scheme == 'https') do |http|
    headers = {
      'Authorization': "bearer #{@access_token}",
      'Content-Type':  'application/json',
    }
    http.post(path, params.to_json, headers)
  end
end