Skip to content

Instantly share code, notes, and snippets.

@crescentrose
Created June 9, 2020 12:26
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save crescentrose/53acfca6f3f4a49d594f0f8a29583cc7 to your computer and use it in GitHub Desktop.
module HttpClient
class Dsl
# These are the methods that your users are going to call
def user_agent(agent)
@user_agent = agent
end
def accept(accept)
@accept = accept
end
# You can call this to get the data from the DSL object
def params
{ accept: @accept, user_agent: @user_agent }
end
end
def self.get(url, &definition)
# Get a new "container" and pass it into a block
dsl = Dsl.new
dsl.instance_eval(&definition)
# voila, now your properties are in the "dsl" variable!
puts "GET #{url}"
puts "User-Agent: #{dsl.params[:user_agent]}"
puts "Accept: #{dsl.params[:accept]}"
end
end
HttpClient.get('https://example.com') do
user_agent "Ruby"
accept "text/plain"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment