Sha256: 4d67dbab706eff6b264bff7cb0efffc156bed6e6d4b1567815115949a7a5c6e4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module NRQL2Nerd
  class Error < StandardError; end

  class Client
    def graphql_nrql_query(query)
      <<~GRAPHQL
          query {
            actor {
              account(id: #{Config.new.account_id}) {
              nrql(query: "#{query}") {
                results
              }
            }
          }
        }
      GRAPHQL
    end

    def graphql_hash(query)
      {
        query: graphql_nrql_query(query)
      }
    end

    def prepare_query(query)
      JSON.pretty_generate(graphql_hash(query))
    end

    def client
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      http
    end

    def uri
      URI("https://api.newrelic.com/graphql")
    end

    def make_query_request(query)
      request = Net::HTTP::Post.new(uri)
      request["Content-Type"] = "application/json"
      request["API-Key"] = Config.new.api_key
      request.body = prepare_query(query)

      client.request(request)
    end

    def run_query(query)
      response = make_query_request(query)

      JSON.parse(response.body).dig("data", "actor", "account", "nrql", "results")
    end

    # module_function :graphql_nrql_query, :graphql_hash, :prepare_query, :run_query, :make_query_request, :uri, :client
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nrql2nerd-0.1.2 lib/nrql2nerd/client.rb
nrql2nerd-0.1.1a lib/nrql2nerd/client.rb
nrql2nerd-0.1.1 lib/nrql2nerd/client.rb