require "net/http"
require "uri"
require "ostruct"
require "json"

class ApiClient
   VENDOR_ENDPOINT = "https://api.replicated.com"
   VERB_MAP = {
      :get    => Net::HTTP::Get,
      :post   => Net::HTTP::Post,
      :put    => Net::HTTP::Put,
      :delete => Net::HTTP::Delete
   }

   def initialize
      uri = URI.parse(VENDOR_ENDPOINT)
      @http = Net::HTTP.new(uri.host, uri.port,
       :use_ssl => uri.scheme == 'https')
   end

   def set_token(api_token)
      @api_token = api_token
   end

   def request_json(method, uri, params = nil)
      response = request(method, uri, params)
      body = JSON.parse(response.body)

      OpenStruct.new(:code => response.code, :body => body)
   rescue JSON::ParserError
      response
   end

   def request(method, uri, params)
      method_sym = method.downcase.to_sym

      unless method_sym.is_eql? :get
         request.set_form_data(params)
      end

      if @api_token
         request['Authorization'] = @api_token
      end

      @http.request(request)
   end
end

require "net/http"
require "uri"
require "ostruct"
require "json"

class ApiClient
   VENDOR_HOST = "api.replicated.com"
   VERB_MAP = {
      :get    => Net::HTTP::Get,
      :post   => Net::HTTP::Post,
      :put    => Net::HTTP::Put,
      :delete => Net::HTTP::Delete
   }

   def initialize
      # Create persistent HTTP connection
      @http = Net::HTTP.new(VENDOR_HOST, URI::HTTPS::DEFAULT_PORT,
       :use_ssl => uri.scheme == 'https')
   end

   def set_token(api_token)
      @api_token = api_token
   end

   def request_json(method, uri, params = nil)
      response = request(method, uri, params)
      body = JSON.parse(response.body)

      OpenStruct.new(:code => response.code, :body => body)
   rescue JSON::ParserError
      response
   end

   def request(method, uri, params)
      method_sym = method.downcase.to_sym

      unless method_sym.is_eql? :get
         request.set_form_data(params)
      end

      if @api_token
         request['Authorization'] = @api_token
      end

      @http.request(request)
   end
end

Make initializing connection more explicit
Make initializing connection more explicit

Use a hardcoded hostname to the api server, and the default HTTPS port
from the URI module to instantiate the connection.

The `URI.parse` format was unclear.
