class ProxyAuth::Connection

Connects to proxy and gets service token

Public Instance Methods

service_token() click to toggle source
# File lib/proxy_auth/connection.rb, line 4
def service_token
  register_service
  @response && JSON.parse(@response.body)['token']
end

Private Instance Methods

headers() click to toggle source
# File lib/proxy_auth/connection.rb, line 24
def headers
  {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'Authorization' => registration_token
  }
end
http() click to toggle source
# File lib/proxy_auth/connection.rb, line 32
def http
  @http ||= Net::HTTP.new(uri.host, uri.port)
end
params() click to toggle source
# File lib/proxy_auth/connection.rb, line 36
def params
  {
    service: {
      name: service_name, host: service_url, api_version: service_version
    }
  }.to_json
end
register_service() click to toggle source
# File lib/proxy_auth/connection.rb, line 11
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
registration_token() click to toggle source
# File lib/proxy_auth/connection.rb, line 18
def registration_token
  encoder = ActionController::HttpAuthentication::Token
  token = Rails.application.secrets.proxy['token']
  @registration_token ||= encoder.encode_credentials token
end
service_name() click to toggle source
# File lib/proxy_auth/connection.rb, line 44
def service_name
  @service_name ||= Rails.application.secrets.service['name']
end
service_url() click to toggle source
# File lib/proxy_auth/connection.rb, line 48
def service_url
  @service_url ||= Rails.application.secrets.service['url']
end
service_version() click to toggle source
# File lib/proxy_auth/connection.rb, line 52
def service_version
  @service_version ||= Rails.application.secrets.service['api_version']
end
uri() click to toggle source
# File lib/proxy_auth/connection.rb, line 56
def uri
  @uri ||= URI.parse(Rails.application.secrets.proxy['url'])
end