From 56e42f209dcba21955d4f4dc13c8caf19d14bfcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 1 Mar 2023 17:05:19 +0100 Subject: [PATCH] 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. --- doc/api/examples/ruby/example_1.rb | 15 +++++++++------ doc/api/examples/ruby/example_2.rb | 16 ++++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/doc/api/examples/ruby/example_1.rb b/doc/api/examples/ruby/example_1.rb index 5fc837c17..704ca8fb6 100644 --- a/doc/api/examples/ruby/example_1.rb +++ b/doc/api/examples/ruby/example_1.rb @@ -1,13 +1,16 @@ -require "http" +require "net/http" API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze def make_request(query_string) - HTTP.headers("User-Agent" => "Mozilla/5.0", accept: "application/json") - .get( - API_ENDPOINT, - params: { query: query_string.delete("\n").delete(" ") } - ) + uri = URI(API_ENDPOINT) + uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" ")) + request = Net::HTTP::Get.new(uri) + request[:accept] = "application/json" + + Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| + https.request(request) + end end query = <<-GRAPHQL diff --git a/doc/api/examples/ruby/example_2.rb b/doc/api/examples/ruby/example_2.rb index 859ee991d..e81c7b882 100644 --- a/doc/api/examples/ruby/example_2.rb +++ b/doc/api/examples/ruby/example_2.rb @@ -1,13 +1,17 @@ -require "http" +require "net/http" +require "json" API_ENDPOINT = "https://demo.consulproject.org/graphql".freeze def make_request(query_string) - HTTP.headers("User-Agent" => "Mozilla/5.0", accept: "application/json") - .get( - API_ENDPOINT, - params: { query: query_string.delete("\n").delete(" ") } - ) + uri = URI(API_ENDPOINT) + uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" ")) + request = Net::HTTP::Get.new(uri) + request[:accept] = "application/json" + + Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| + https.request(request) + end end def build_query(options = {})