Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.