Module: Net::HTTP::RestClientModule

Included in:
Gitter::API::Client
Defined in:
lib/gitter/api/util/net_http_client.rb

Overview

Net::HTTP::RestClientModule

A wrapper library for NetHTTP to create a simplistic “Rest Client” class

Simply ‘include Net::HTTP::RestClientModule` into a class, and make sure on initialization a `uri` method/accessor with a `URI` object is created.

class MyRestClient
  include Net::HTTP::RestClientModule

  attr_accessor :uri

  def initialize
    @uri = URI("http://example.com")
  end

  # override this if needed, otherewise true by default
  def ssl_verify
    !!ENV["SHOULD_SSL_VERIFY"]
  end
end

# calling
MyRestClient.new.get  "/foo/bar"
MyRestClient.new.post "/foo/bar"
MyRestClient.new.put  "/foo/bar"

TODO: Make query_params a little more generic TODO: Create a gem for this simple lib

Instance Method Summary collapse

Instance Method Details

#connectionObject

Net::HTTP Connection object for the client object

Will conigure it based off of the uri method/accessor of current instance if one has not been instanciated yet.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitter/api/util/net_http_client.rb', line 44

def connection
  return @connection if defined? @connection

  verify_ssl = respond_to?(:ssl_verify) ? ssl_verify : true

  @connection             = Net::HTTP.new(uri.host, uri.port)
  @connection.use_ssl     = true                      if uri.scheme == "https"
  @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE unless verify_ssl

  @connection
end

#get(path, query_params = {}, request_headers = {}) ⇒ Object

Send a GET request to the configured connection

Parameters

path (String)

URL path for the request

query_params (Hash)

Query params to add to the path

request_headers (Hash)

Request specific headers



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gitter/api/util/net_http_client.rb', line 64

def get path, query_params = {}, request_headers = {}
  if query_params.delete_if { |_,v| v.nil? }.empty?
    get_uri = path
  else
    uri_class = uri.scheme == "https" ? URI::HTTPS : URI::HTTP
    get_uri   = uri_class.build :host  => uri.host,
                                :path  => path,
                                :query => URI.encode_www_form(query_params)
  end

  get_headers = headers request_headers
  get_request = Net::HTTP::Get.new get_uri, get_headers

  connection_do get_request
end

#post(path, payload, request_headers = {}) ⇒ Object

Send a POST request to the configured connection

Parameters

path (String)

URL path for the request

payload (Hash)

Request body

request_headers (Hash)

Request specific headers



88
89
90
91
92
93
94
# File 'lib/gitter/api/util/net_http_client.rb', line 88

def post path, payload, request_headers = {}
  post_headers      = headers request_headers
  post_request      = Net::HTTP::Post.new path, post_headers
  post_request.body = payload

  connection_do post_request
end

#put(path, payload, request_headers = {}) ⇒ Object

Send a PUT request to the configured connection

Parameters

path (String)

URL path for the request

payload (Hash)

Request body

request_headers (Hash)

Request specific headers



104
105
106
107
108
109
110
# File 'lib/gitter/api/util/net_http_client.rb', line 104

def put path, payload, request_headers = {}
  put_headers      = headers request_headers
  put_request      = Net::HTTP::Put.new path, put_headers
  put_request.body = payload

  connection_do put_request
end