-
-
Save crescentrose/53acfca6f3f4a49d594f0f8a29583cc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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