Class: GHX::GraphqlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ghx/graphql_client.rb

Overview

Internal class to interact with the GitHub GraphQL API

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ GraphqlClient

Returns a new instance of GraphqlClient.

Parameters:

  • api_key (String)


5
6
7
# File 'lib/ghx/graphql_client.rb', line 5

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#query(query) ⇒ Net::HTTPResponse

Perform a GraphQL Query and return the result

Parameters:

  • query (String)

    GraphQL Query

Returns:

  • (Net::HTTPResponse)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ghx/graphql_client.rb', line 12

def query(query)
  uri = URI("https://api.github.com/graphql")
  req = Net::HTTP::Post.new(uri)
  req["Authorization"] = "Bearer #{@api_key}"
  req["Content-Type"] = "application/json"
  req.body = {query: query}.to_json

  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end
end