Project

minver

0.0
No commit activity in last 3 years
No release in over 3 years
Minimal HTTP server with graceful shutdown & value passing
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
 Project Readme

Minver

This gem provides a minimal HTTP server solution with two key features:

  1. Graceful shutdown of the server
  2. The caller can retrieve a value that is generated from the route handler

Installation

Add this line to your application's Gemfile:

gem 'minver'

And then execute:

$ bundle

Or install it yourself as:

$ gem install minver

Usage

This gem allows you to wait for user input via HTTP requests. For example:

require 'minver'

# initialize server. default port is 18167, default bind is '::'
server = Minver::Base.new port: 3000

# Define your routes:

server.post "/name" do |request|
  name = request.params["name"]
  # pass this param to the caller
  pass name: name
  "Thanks, #{name}, your personal information was submitted."
end

server.post "/age" do |request|
  age = request.params["age"].to_i
  if age < 5
    [400, {}, "Hey there, fella. You better ask your parents to use this app instead."]
  else
    # pass this param to the caller
    pass age: age
    "Thank you, your age has been submitted!"
  end
end

# We're ready to go! Instantiate the hash where we store the info:

personal_info = {}

# And listen for requests!

loop do
  $stdout.puts "Provide your personal information over http://localhost:3000/name and /age"
  personal_info.merge!(server.run)
  break if personal_info.key?(:name) && personal_info.key?(:age)
end

# Now go to your terminal and make a request!
# e.g.:
# curl -XPOST -H"Content-Type: application/json" -d'{"name": "Danyel Bayraktar"}' localhost:3000/name
# or:
# curl -XPOST -d'age=3' localhost:3000/age


# Do something with this information!
puts "Hey #{personal_info[:name]}, #{personal_info[:age]} years is the best age to be starring my repo!"

# Don't forget to shut down the server
# (you can also call `stop` instead of `pass` from the route handler while still providing a value):
server.stop

Contributing

  1. Fork it ( https://github.com/muja/minver/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request