Class: Simple::HTTP

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
CheckedResponse
Defined in:
lib/simple/http.rb,
lib/simple/http.rb,
lib/simple/http/errors.rb,
lib/simple/http/logger.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, CheckedResponse, Driver, GemHelper, Helpers Classes: ContentTypeParser, Error, Headers, Request, Response, Status4XXError, Status5XXError, StatusError, TooManyRedirections

Constant Summary collapse

VERSION =
GemHelper.version "simple-http"

Constants included from CheckedResponse

CheckedResponse::SELF

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

build_url

Methods included from CheckedResponse

#delete!, #get!, #head!, #options!, #post!, #put!

Constructor Details

#initialize(base_url: nil) ⇒ HTTP

Returns a new instance of HTTP.



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

def initialize(base_url: nil)
  self.default_headers = {}
  self.default_params = nil
  self.base_url = base_url
  self.follows_redirections = true
end

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/simple/http/logger.rb', line 5

def logger
  @logger
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.



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

def base_url
  @base_url
end

#basic_authObject

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



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

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.



106
107
108
# File 'lib/simple/http.rb', line 106

def cache
  @cache
end

#default_headersObject

Always send these headers.



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

def default_headers
  @default_headers
end

#default_paramsObject

When set, appends this to all request URLs



55
56
57
# File 'lib/simple/http.rb', line 55

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.



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

def follows_redirections
  @follows_redirections
end

Instance Method Details

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



94
95
96
# File 'lib/simple/http.rb', line 94

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

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



78
79
80
# File 'lib/simple/http.rb', line 78

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

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



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

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

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



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

def options(url, headers = {})
  perform_request!(:OPTIONS, url, nil, headers)
end

#perform_request!(verb, url, body, headers) ⇒ Object

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/simple/http.rb', line 110

def perform_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

  expect! url => /^http|https/

  if default_headers
    headers = headers.merge(default_headers)
  end

  case default_params
  when Hash then url = ::Simple::HTTP.build_url(url, default_params)
  when String then url = url + (url.include?("?") ? "&" : "?") + default_params
  end

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

  execute_request(request)
end

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



86
87
88
# File 'lib/simple/http.rb', line 86

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

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



90
91
92
# File 'lib/simple/http.rb', line 90

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