class Github::GraphQL

This class is used to make queries to the Github GraphQL API

Public Class Methods

new(token, query, vars = nil) click to toggle source

Expects a valid Github OAuth token & the GraphQL query string, and optionally a hash array of any variables to be passed with the query.

With raise an ArgumentError if either token or query are nil.

# File lib/github/graphql.rb, line 20
def initialize(token, query, vars = nil)
  @payload = {}

  uri = URI.parse('https://api.github.com/graphql')
  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true

  @request = Net::HTTP::Post.new(uri)
  @request['Content-type'] = 'application/json'

  token(token)
  payload(query, vars)
end

Public Instance Methods

payload(query, vars = nil) click to toggle source

Set the query string and optionally the variables to be passed with the query.

Will raise an ArgumentError if query is nil

# File lib/github/graphql.rb, line 51
def payload(query, vars = nil)
  raise ArgumentError, 'Cannot have nil query!', caller if query.nil?
  @payload['query'] = query
  @payload['variables'] = vars
  @request.body = @payload.to_json
end
query() click to toggle source

Execute the query.

Returns a ruby hash array of the response from Github.

# File lib/github/graphql.rb, line 63
def query
  response = @http.request(@request)
  JSON.parse(response.body)
end
token(token) click to toggle source

Set the OAuth token.

Will raise an ArgumentError if token is nil

# File lib/github/graphql.rb, line 39
def token(token)
  raise ArgumentError, 'Invalid Token', caller unless
    token =~ /\A[A-Za-z0-9]{40,40}\z/
  @request['Authorization'] = "bearer #{token}"
end