class Forward::API::Tunnel

Public Class Methods

create(options, &block) click to toggle source
# File lib/forward/api/tunnel.rb, line 5
def self.create(options, &block)
  resource = new
  options  = {
    path:          "#{API.base_path}/tunnels/",
    authenticated: true,
    params: {
       hostport:  options[:port],
       vhost:     options[:host],
       subdomain: options[:subdomain_prefix],
       cname:     options[:cname],
       username:  options[:username],
       password:  options[:password],
       no_auth:   options[:no_auth],
       client:    Forward.client_string,
    }
  }

  resource.post(options) do |response, status|
    if status == 200
      block.call(response)
    else
      handle_error(response, status)
    end
  end
end
destroy(id, &block) click to toggle source
# File lib/forward/api/tunnel.rb, line 31
def self.destroy(id, &block)
  resource = new
  options  = {
    path:          "#{API.base_path}/tunnels/#{id}",
    authenticated: true,
  }

  resource.delete(options) do |response, status|
    block.call(response)
  end
end

Private Class Methods

handle_error(response, status) click to toggle source
# File lib/forward/api/tunnel.rb, line 45
def self.handle_error(response, status)
  exit_with_error "Unable to authenticate your account. Try logging out with `forward account logout' or contact #{SUPPORT_EMAIL}" if status == 401
  exit_with_error DEFAULT_ERROR_MESSAGE if response.is_a?(String) || !response.has_key?(:type)

  case response[:type]
  when 'invalid_request_error'
    message = 'Invalid tunnel parameters'

    if response.has_key?(:errors)
      message << ":\n"

      response[:errors].each { |field, errors| message << " - #{field} #{errors.join(', and ')}\n" }
    else
      message << ", run `forward --help' for usage help or contact us at #{SUPPORT_EMAIL}"
    end

    exit_with_error(message)
  when 'account_error'
    exit_with_error response[:errors][:account].first
  else
    exit_with_error DEFAULT_ERROR_MESSAGE
  end
end