Skip to content

Instantly share code, notes, and snippets.

@purinkle
Created May 28, 2023 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purinkle/0f6fe8d5393c0c64d1fe2e69abe60213 to your computer and use it in GitHub Desktop.
Save purinkle/0f6fe8d5393c0c64d1fe2e69abe60213 to your computer and use it in GitHub Desktop.
When to use keyword arguments?
require "net/http"
class HttpWrapper
def post(url, body, headers)
Net::HTTP.post(URI.parse(url), body, headers)
end
end
url = "https://example.com/path"
body = {TEST_KEY: "TEST_VALUE"}
headers = {"Authorization" => "Bearer TEST_API_KEY"}
@http = HttpWrapper.new
@http.post(url, body.to_json, headers).body
# or…
class HttpWrapper
def post(url:, body:, headers:)
Net::HTTP.post(URI.parse(url), body, headers)
end
end
url = "https://example.com/path"
body = {TEST_KEY: "TEST_VALUE"}
headers = {"Authorization" => "Bearer TEST_API_KEY"}
@http = HttpWrapper.new
@http.post(url:, body: body.to_json, headers:).body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment