Class: Simple::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/http.rb,
lib/simple/http.rb,
lib/simple/http/errors.rb,
lib/simple/http/version.rb

Overview

A very simple, Net::HTTP-based HTTP client.

Has some support for transferring JSON data: all data in PUT and POST requests are jsonized, and all data in responses are parsed as JSON if the Content-Type header is set to “application/json”.

Defined Under Namespace

Modules: Assertions Classes: Error, TooManyRedirections

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTTP

Returns a new instance of HTTP.



44
45
46
# File 'lib/simple/http.rb', line 44

def initialize
  self.follows_redirections = true
end

Instance Attribute Details

#base_urlObject

The base URL: when set, all requests that do not start with http: or https: are done relative to this base URL.



27
28
29
# File 'lib/simple/http.rb', line 27

def base_url
  @base_url
end

#basic_authObject

When set, sets Authorization headers to all requests. Must be an array [ username, password ].



42
43
44
# File 'lib/simple/http.rb', line 42

def basic_auth
  @basic_auth
end

#cacheObject

When set, and a response is cacheable (as it returns a valid expires_in value), the cache object is used to cache responses.



58
59
60
# File 'lib/simple/http.rb', line 58

def cache
  @cache
end

#default_paramsObject

When set, appends this to all request URLs



37
38
39
# File 'lib/simple/http.rb', line 37

def default_params
  @default_params
end

#follows_redirectionsObject

When set (default), redirections are followed. Note: When follows_redirections is not set, a HTTP redirection would raise an error - which is probably only useful when testing an interface.



33
34
35
# File 'lib/simple/http.rb', line 33

def follows_redirections
  @follows_redirections
end

Instance Method Details

#delete(url, headers = {}) ⇒ Object



51
# File 'lib/simple/http.rb', line 51

def delete(url, headers = {});            http :DELETE, url, nil, headers; end

#expires_in(response) ⇒ Object

when does the response expire? By default, calculates expiration based on response headers. Override as needed.



63
64
65
# File 'lib/simple/http.rb', line 63

def expires_in(response)
  response.expires_in
end

#get(url, headers = {}) ⇒ Object



48
# File 'lib/simple/http.rb', line 48

def get(url, headers = {});               http :GET, url, nil, headers; end

#post(url, body = nil, headers = {}) ⇒ Object



49
# File 'lib/simple/http.rb', line 49

def post(url, body = nil, headers = {});  http :POST, url, body, headers; end

#put(url, body = nil, headers = {}) ⇒ Object



50
# File 'lib/simple/http.rb', line 50

def put(url, body = nil, headers = {});   http :PUT, url, body, headers; end