Class: Octarine::SimpleHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/octarine/simple_http.rb

Overview

SimpleHTTP is a bare-bones implementation of a simple http client, designed to be easily replaceable with another implementation.

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ SimpleHTTP

:call-seq: SimpleHTTP.new(url, options={}) -> simple_http SimpleHTTP.new(options) -> simple_http

Create a SimpleHTTP instace with either a url and options or options with a :url key.



17
18
19
20
21
22
23
# File 'lib/octarine/simple_http.rb', line 17

def initialize(url, options={})
  unless url.respond_to?(:to_str)
    options = url
    url = options[:url]
  end
  @host, @port = url.to_s.split(/:/)
end

Instance Method Details

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

:call-seq: simple_http.delete(path, headers={}) -> response

Perform a DELETE request, returns a response that responds to #status, #headers, and #body



66
67
68
# File 'lib/octarine/simple_http.rb', line 66

def delete(path, headers={})
  run_request(:delete, path, nil, headers)
end

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

:call-seq: simple_http.get(path, headers={}) -> response

Perform a GET request, returns a response that responds to #status, #headers, and #body



39
40
41
# File 'lib/octarine/simple_http.rb', line 39

def get(path, headers={})
  run_request(:get, path, nil, headers)
end

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

:call-seq: simple_http.head(path, headers={}) -> response

Perform a HEAD request, returns a response that responds to #status, #headers, and #body



30
31
32
# File 'lib/octarine/simple_http.rb', line 30

def head(path, headers={})
  run_request(:head, path, nil, headers)
end

#options(path, headers) ⇒ Object

:call-seq: simple_http.options(path, headers={}) -> response

Perform an OPTIONS request, returns a response that responds to #status, #headers, and #body



75
76
77
# File 'lib/octarine/simple_http.rb', line 75

def options(path, headers)
  run_request(:options, path, nil, headers)
end

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

:call-seq: simple_http.post(path, body=nil, headers={}) -> response

Perform a POST request, returns a response that responds to #status, #headers, and #body



48
49
50
# File 'lib/octarine/simple_http.rb', line 48

def post(path, body=nil, headers={})
  run_request(:post, path, body, headers)
end

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

:call-seq: simple_http.put(path, body=nil, headers={}) -> response

Perform a PUT request, returns a response that responds to #status, #headers, and #body



57
58
59
# File 'lib/octarine/simple_http.rb', line 57

def put(path, body=nil, headers={})
  run_request(:put, path, body, headers)
end

#run_request(method, path, body, headers) ⇒ Object

:call-seq: simple_http.run_request(method, path, body, headers) -> res

Perform request, returns a response that responds to #status, #headers, and #body



84
85
86
87
88
89
# File 'lib/octarine/simple_http.rb', line 84

def run_request(method, path, body, headers)
  klass = Net::HTTP.const_get(method.to_s.capitalize)
  req.klass.new(path, headers)
  req.body = body if body
  request(req)
end