Skip to content

Instantly share code, notes, and snippets.

@sos4nt
Last active May 12, 2021 13:32
Show Gist options
  • Save sos4nt/ea681c2bc254b78e531877dd5287c8a5 to your computer and use it in GitHub Desktop.
Save sos4nt/ea681c2bc254b78e531877dd5287c8a5 to your computer and use it in GitHub Desktop.
class Client
def self.post(&block)
data = DslData.new
Dsl.new(data).instance_eval(&block)
# build request with data from dsl calls
# and execute request
puts "Making a request to #{data.url} with headers #{data.headers}"
response = "response" # this would be your http response
if rand > 0.5 # response.success?
data.on_success.call(response) if data.on_success
else
data.on_error.call(response) if data.on_error
end
end
end
class DslData
attr_accessor :url, :headers, :on_success, :on_error
def initialize
@headers = {}
end
end
class Dsl
def initialize(data)
@data = data
end
def url(value)
@data.url = value
end
def header(key, value)
@data.headers[key] = value
end
def on_success(&block)
@data.on_success = block
end
def on_error(&block)
@data.on_error = block
end
end
Client.post do |client|
url "http://somewhere"
header "Kwazy", "Cupcakes"
on_success do |response|
puts "got #{response} which is a success"
end
on_error do |response|
puts "gor #{response} which is an error"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment