Fix API examples

We were getting many errors when trying to run them, from uninitialized
constant `HTTP` to undefined method `headers`.

We might move these examples to the documentation repository in the
future, but we need to look for possible side-effects first.
This commit is contained in:
Javi Martín
2023-03-01 17:05:19 +01:00
parent e4992ffb8d
commit 56e42f209d
2 changed files with 19 additions and 12 deletions

View File

@@ -1,13 +1,16 @@
require "http" require "net/http"
API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze
def make_request(query_string) def make_request(query_string)
HTTP.headers("User-Agent" => "Mozilla/5.0", accept: "application/json") uri = URI(API_ENDPOINT)
.get( uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" "))
API_ENDPOINT, request = Net::HTTP::Get.new(uri)
params: { query: query_string.delete("\n").delete(" ") } request[:accept] = "application/json"
)
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
https.request(request)
end
end end
query = <<-GRAPHQL query = <<-GRAPHQL

View File

@@ -1,13 +1,17 @@
require "http" require "net/http"
require "json"
API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze
def make_request(query_string) def make_request(query_string)
HTTP.headers("User-Agent" => "Mozilla/5.0", accept: "application/json") uri = URI(API_ENDPOINT)
.get( uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" "))
API_ENDPOINT, request = Net::HTTP::Get.new(uri)
params: { query: query_string.delete("\n").delete(" ") } request[:accept] = "application/json"
)
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
https.request(request)
end
end end
def build_query(options = {}) def build_query(options = {})