Last active
February 23, 2019 16:22
-
-
Save MarioRuiz/9fcce64b7e5af68f75bdffa062f824e9 to your computer and use it in GitHub Desktop.
Create your own API so other tools like Postman or other computers in your network can get what they need for the tests or for whatever you need. Run in command line ruby api.rb to fire up this API. Access from any computer inside your network, for example to get 30 random emails, write in your browser: mycomputer:4567/gen?pattern=20-50:@&num=30
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
# libraries we will use in our api | |
# documentation: https://github.com/MarioRuiz/string_pattern | |
# documentation: https://github.com/MarioRuiz/nice_hash | |
require "sinatra" | |
require "string_pattern" | |
require "nice_hash" | |
set :protection, :except => :frame_options | |
set :bind, "0.0.0.0" | |
get "/" do | |
"Hello from #{request.host}" | |
end | |
# examples: | |
# mycomputername:4567/gen/10-20:LN | |
# mycomputername:4567/gen/30:@ | |
get "/gen/*" do | |
params[:splat].gen | |
end | |
# examples: | |
# mycomputername:4567/gen?pattern=20:LN | |
# mycomputername:4567/gen?pattern=20:@&num=20 | |
get "/gen" do | |
if params.key?(:pattern) | |
if params[:num].to_i > 10000 | |
num = 10000 | |
elsif params[:num].to_i == 0 | |
num = 1 | |
else | |
num = params[:num].to_i | |
end | |
resp = Array.new | |
num.times do | |
resp << params[:pattern].gen | |
end | |
resp.join("\n") | |
end | |
end | |
# examples to generate a random JSON data following the patterns specified: | |
# send a POST to mycomputername:4567/gen with the JSON data: | |
# { | |
# "loginame": "5-10:/xn/", | |
# "password": "5-10:L/n/", | |
# "name": "10-20:T_/x/", | |
# "zip": '5:N', | |
# "address": "21 Doom Av", | |
# "city": "London|Rome", | |
# "mobilePhone": ['(', '3:N', ')', '6-8:N'], | |
# "sex": "male|female|other", | |
# "customer": "true" | |
# } | |
post "/gen" do | |
data = request.body.read | |
if data.json.key?(:pattern) and data.json.size == 1 | |
if data.json(:pattern)[0] == "(" and data.json(:pattern)[-1] == ")" #regular expression | |
res = (Regexp.new data.json(:pattern)).gen | |
else | |
res = data.json(:pattern).gen | |
end | |
else | |
res = data.json.gen.to_json | |
end | |
res | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment