Sha256: 4207e822c43cb8a995366185dd2572f40f1714532bdfd43e475d5283a3981316

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module ProxyAuth
  # Connects to proxy and gets service token
  class Connection
    def service_token
      register_service
      @response && JSON.parse(@response.body)['token']
    end

    private

    def register_service
      @response = http.post('/services', params, headers)
      Rails.logger.info "Service #{service_name} registered."
    rescue Errno::ECONNREFUSED
      Rails.logger.fatal 'Proxy connection refused.'
    end

    def registration_token
      encoder = ActionController::HttpAuthentication::Token
      token = Rails.application.secrets.proxy['token']
      @registration_token ||= encoder.encode_credentials token
    end

    def headers
      {
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'Authorization' => registration_token
      }
    end

    def http
      @http ||= Net::HTTP.new(uri.host, uri.port)
    end

    def params
      {
        service: {
          name: service_name, host: service_url, version: service_version
        }
      }.to_json
    end

    def service_name
      @service_name ||= Rails.application.secrets.service['name']
    end

    def service_url
      @service_url ||= Rails.application.secrets.service['url']
    end

    def service_version
      @service_version ||= Rails.application.secrets.service['version']
    end

    def uri
      @uri ||= URI.parse(Rails.application.secrets.proxy['url'])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proxy_auth-0.0.5 lib/proxy_auth/connection.rb