class MmJsonClient::HttpClient::Client

A helper class to handle lower level HTTP calls for the API.

Attributes

options[R]

Public Class Methods

new(url, endpoint, options = {}) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 9
def initialize(url, endpoint, options = {})
  @url = url
  @endpoint = endpoint
  @options = options
end

Public Instance Methods

post(data) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 15
def post(data)
  new_http(URI(@url)).start do |http|
    req = Net::HTTP::Post.new(@endpoint)
    req.body = data
    configure_content_type(req)

    http.request(req)
  end
end

Private Instance Methods

configure_content_type(req) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 34
def configure_content_type(req)
  req['Accept'] = 'application/json'
  req['Content-Type'] = 'application/json'
end
configure_https(http) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 39
def configure_https(http)
  http.use_ssl = true
  http.verify_mode = if @options[:verify_ssl] == false
                       OpenSSL::SSL::VERIFY_NONE
                     else
                       OpenSSL::SSL::VERIFY_PEER
                     end
end
configure_timeouts(http) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 48
def configure_timeouts(http)
  http.open_timeout = @options[:open_timeout] if @options[:open_timeout]
  http.read_timeout = @options[:timeout] if @options[:timeout]
end
new_http(uri) click to toggle source
# File lib/mm_json_client/http_client/client.rb, line 27
def new_http(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  configure_https(http) if uri.scheme == 'https'
  configure_timeouts(http)
  http
end