Class: Simple::HTTP
- Inherits:
-
Object
- Object
- Simple::HTTP
- 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.5"
Instance Attribute Summary collapse
-
#base_url ⇒ Object
The base URL: when set, all requests that do not start with http: or https: are done relative to this base URL.
-
#basic_auth ⇒ Object
When set, sets Authorization headers to all requests.
-
#cache ⇒ Object
When set, and a response is cacheable (as it returns a valid expires_in value), the cache object is used to cache responses.
-
#default_params ⇒ Object
When set, appends this to all request URLs.
-
#follows_redirections ⇒ Object
When set (default), redirections are followed.
-
#logger ⇒ Object
The logger instance.
Instance Method Summary collapse
- #delete(url, headers = {}) ⇒ Object
-
#expires_in(response) ⇒ Object
when does the response expire? By default, calculates expiration based on response headers.
- #get(url, headers = {}) ⇒ Object
-
#initialize ⇒ HTTP
constructor
A new instance of HTTP.
- #post(url, body = nil, headers = {}) ⇒ Object
- #put(url, body = nil, headers = {}) ⇒ Object
Constructor Details
#initialize ⇒ HTTP
49 50 51 52 53 |
# File 'lib/simple/http.rb', line 49 def initialize self.follows_redirections = true self.logger = Logger.new(STDERR) self.logger.level = Logger::WARN end |
Instance Attribute Details
#base_url ⇒ Object
The base URL: when set, all requests that do not start with http: or https: are done relative to this base URL.
32 33 34 |
# File 'lib/simple/http.rb', line 32 def base_url @base_url end |
#basic_auth ⇒ Object
When set, sets Authorization headers to all requests. Must be an array [ username, password ].
47 48 49 |
# File 'lib/simple/http.rb', line 47 def basic_auth @basic_auth end |
#cache ⇒ Object
When set, and a response is cacheable (as it returns a valid expires_in value), the cache object is used to cache responses.
65 66 67 |
# File 'lib/simple/http.rb', line 65 def cache @cache end |
#default_params ⇒ Object
When set, appends this to all request URLs
42 43 44 |
# File 'lib/simple/http.rb', line 42 def default_params @default_params end |
#follows_redirections ⇒ Object
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.
38 39 40 |
# File 'lib/simple/http.rb', line 38 def follows_redirections @follows_redirections end |
#logger ⇒ Object
The logger instance.
27 28 29 |
# File 'lib/simple/http.rb', line 27 def logger @logger end |
Instance Method Details
#delete(url, headers = {}) ⇒ Object
58 |
# File 'lib/simple/http.rb', line 58 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.
70 71 72 |
# File 'lib/simple/http.rb', line 70 def expires_in(response) response.expires_in end |
#get(url, headers = {}) ⇒ Object
55 |
# File 'lib/simple/http.rb', line 55 def get(url, headers = {}); http :GET, url, nil, headers; end |
#post(url, body = nil, headers = {}) ⇒ Object
56 |
# File 'lib/simple/http.rb', line 56 def post(url, body = nil, headers = {}); http :POST, url, body, headers; end |
#put(url, body = nil, headers = {}) ⇒ Object
57 |
# File 'lib/simple/http.rb', line 57 def put(url, body = nil, headers = {}); http :PUT, url, body, headers; end |