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: Caching, Driver, GemHelper Classes: BodyBuilder, Error, Headers, Request, Response, TooManyRedirections

Constant Summary collapse

VERSION =
GemHelper.version "simple-http"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTTP

Returns a new instance of HTTP.



52
53
54
55
# File 'lib/simple/http.rb', line 52

def initialize
  self.follows_redirections = true
  self.logger = Logger.new(STDERR, level: :warn)
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.



35
36
37
# File 'lib/simple/http.rb', line 35

def base_url
  @base_url
end

#basic_authObject

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



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

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.



82
83
84
# File 'lib/simple/http.rb', line 82

def cache
  @cache
end

#default_paramsObject

When set, appends this to all request URLs



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

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.



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

def follows_redirections
  @follows_redirections
end

#loggerObject

The logger instance.



30
31
32
# File 'lib/simple/http.rb', line 30

def logger
  @logger
end

Instance Method Details

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



73
74
75
# File 'lib/simple/http.rb', line 73

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

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



61
62
63
# File 'lib/simple/http.rb', line 61

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

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



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

def head(url, headers = {})
  request :HEAD, url, nil, headers
end

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



65
66
67
# File 'lib/simple/http.rb', line 65

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

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



69
70
71
# File 'lib/simple/http.rb', line 69

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

#request(verb, url, body, headers) ⇒ Object

– HTTP request ———————————————————–



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/simple/http.rb', line 86

def request(verb, url, body, headers)
  #
  # normalize url; i.e. prepend base_url if the url itself is incomplete.
  unless url =~ /^(http|https):/
    url = File.join(base_url, url)
  end

  uri = URI.parse(url)
  unless uri.is_a?(URI::HTTP)
    raise ArgumentError, "Invalid URL: #{url}"
  end

  # append default_params, if set
  if default_params
    url.concat(url.include?("?") ? "&" : "?")
    url.concat default_params
  end

  request = Request.new(verb: verb, url: url, body: body, headers: headers)

  execute_request(request)
end