Class: Github::GraphQL
- Inherits:
-
Object
- Object
- Github::GraphQL
- Defined in:
- lib/github/graphql.rb
Overview
This class is used to make queries to the Github GraphQL API
Instance Method Summary collapse
-
#initialize(token, query, vars = nil) ⇒ GraphQL
constructor
Expects a valid Github OAuth token & the GraphQL query string, and optionally a hash array of any variables to be passed with the query.
-
#payload(query, vars = nil) ⇒ Object
Set the query string and optionally the variables to be passed with the query.
-
#query ⇒ Object
Execute the query.
-
#token(token) ⇒ Object
Set the OAuth token.
Constructor Details
#initialize(token, query, vars = nil) ⇒ GraphQL
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.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Method Details
#payload(query, vars = nil) ⇒ Object
Set the query string and optionally the variables to be passed with the query.
Will raise an ArgumentError if query is nil
50 51 52 53 54 55 |
# File 'lib/github/graphql.rb', line 50 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 ⇒ Object
Execute the query.
Returns a ruby hash array of the response from Github.
62 63 64 65 |
# File 'lib/github/graphql.rb', line 62 def query response = @http.request(@request) JSON.parse(response.body) end |
#token(token) ⇒ Object
Set the OAuth token.
Will raise an ArgumentError if token is nil
39 40 41 42 |
# File 'lib/github/graphql.rb', line 39 def token(token) raise ArgumentError, 'Cannot have nil token!', caller if token.nil? @request['Authorization'] = "bearer #{token}" end |