Protocol::HTTP GuidesGetting Started

Getting Started

This guide explains how to use protocol-http for building abstract HTTP interfaces.

Installation

Add the gem to your project:

$ bundle add protocol-http

Core Concepts

protocol-http has several core concepts:

Integration

This gem does not provide any specific client or server implementation, rather it's used by several other gems.

Usage

Headers

class Protocol::HTTP::Headers provides semantically meaningful interpretation of header values implements case-normalising keys.

require 'protocol/http/headers'

headers = Protocol::HTTP::Headers.new

headers['Content-Type'] = "image/jpeg"

headers['content-type']
# => "image/jpeg"

Hypertext References

class Protocol::HTTP::Reference is used to construct "hypertext references" which consist of a path and URL-encoded key/value pairs.

require 'protocol/http/reference'

reference = Protocol::HTTP::Reference.new("/search", q: 'kittens')

reference.to_s
# => "/search?q=kittens"

URL Parsing

module Protocol::HTTP::URL is used to parse incoming URLs to extract the query and other relevant details.

require 'protocol/http/url'

reference = Protocol::HTTP::Reference.parse("/search?q=kittens")

parameters = Protocol::HTTP::URL.decode(reference.query)
# => {"q"=>"kittens"}

This implemenation may be merged with class Protocol::HTTP::Reference or removed in the future.