Last active
April 21, 2020 03:07
-
-
Save theterminalguy/799cee333e02777424d642e8a5434608 to your computer and use it in GitHub Desktop.
Creating a DSL with Ruby https://theterminalguy.sh/creating-a-dsl-with-ruby/
This file contains hidden or 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
require 'ostruct' | |
def send_mail(to:, from:, subject:, body:) | |
puts "Sending mail to: #{to}" | |
puts "From: #{from}" | |
puts "Subject: #{subject}" | |
puts "Body: #{body}" | |
end | |
module FormBot | |
extend self | |
def visit(url, &block) | |
puts "Visiting #{url}" | |
instance_eval(&block) | |
# TODO: add logic for visiting url | |
end | |
def in_form(form_name) | |
puts "Found form #{form_name}" | |
# TODO: add logic for finding form | |
end | |
def fill_in(field_name, with:) | |
puts "Filling in #{field_name} with #{with}" | |
# TODO: add logic for filling in form field | |
end | |
def select_from(field_name, value:) | |
puts "Selecting #{value} from #{field_name}" | |
# TODO: add logic for selecting form field | |
end | |
def submit | |
# Here we are faking a failed response | |
# after the form has been submitted | |
response = OpenStruct.new(failure: true, errors: ['error1']) | |
yield response if block_given? | |
# TODO: add logic for submitting form | |
end | |
end | |
FormBot.visit 'https://www.example.com/register' do | |
in_form 'Registration' | |
fill_in 'First Name', with: 'Jolly' | |
fill_in 'Last Name', with: 'Roger' | |
select_from 'City', value: 'Kansas' | |
select_from 'Gender', value: 'Male' | |
select_from 'Colors', value: ['Red', 'Yellow'] | |
submit do |response| | |
if response.failure | |
send_mail to: '[email protected]', | |
from: '[email protected]', | |
subject: 'Failed to submit form', | |
body: response.errors | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment