0.0
No commit activity in last 3 years
No release in over 3 years
http_requests is a lib that provides an easy way to create an HTTP/HTTPS connection and issue requests to it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

http_requests

http_requests is a lib that provides easy methods to make http and https requests.

Install

Add to your Gemfile:

gem http_requests

Then execute the command bundle install on terminal when inside your folder with the Gemfile.

Or install yourself:

gem install http_requests

Usage

First, import the gem to you project using

require http_requests

Then, follow the bellow examples to make the most simple http post and get requests possible:

Simplle HTTP request.

conn = HTTPConn.new("http://www.someexample.com")
res = conn.get # Or conn.post

puts res.message
puts res.code
puts res.body

Setting a header and a query string for a request to an specific end point:

conn = HTTPConn.new("http://www.someexample.com")
res = conn.get(end_point: "/someaction"
               header: { "Accept" => "application/json" },
               query_str: { "token" => "abc123" })
# Will generate the uri "http://www.someexample.com/someaction?token=abc123"

Setting up an HTTPS (without certificate validation) connection with a preset header (so every requets will use it):

settings = {
  header: { "Accept" => "application/json" },
  ssl: true
}

conn = HTTPConn.new("https://www.someexample.com", settings)

res = conn.post(end_point: "/someaction",
                body: %q{"{ "token": "abc123" }"})

If you want to use the HTTPS certificate validation, set the certificate path when declaring the settings:

settings = {
  header: { "Accept" => "application/json" },
  ssl: true,
  cert: "certs/certificate.pem"
}

In order to issue multiple requests to the same connection, use conn.start passing a block. It is useful because everytime a request is issued, a connection is opened and automatically closed, then, when a next request is called, another connection is opened.

conn.start do
  conn.get
  conn.get
  conn.post
end

SingleConn

The SingleConn is a module that allows the same connection instance to be used throughout the entire application. It works as a Singleton Class.

File 1:

settings = {
  # ...
}
SingleConn.init("http://www.someexample.com", settings)
res = SingleConn.get

Once initialized, you can just use it in other files:

File 2:

res = SingleConn.post(end_point: "/anyaction/youwant")

As seen, SingleConn works exactly like a normal Connection, but its "instance" can be shared.