module Routemaster::Client::Connection

Public Class Methods

delete(path, &block) click to toggle source
# File routemaster/client/connection.rb, line 20
def delete(path, &block)
  http(:delete, path, &block)
end
get(path, &block) click to toggle source
# File routemaster/client/connection.rb, line 16
def get(path, &block)
  http(:get, path, &block)
end
http(method, path, &block) click to toggle source
# File routemaster/client/connection.rb, line 24
def http(method, path, &block)
  _conn.send(method, path, &block)
end
post(path, &block) click to toggle source
# File routemaster/client/connection.rb, line 12
def post(path, &block)
  http(:post, path, &block)
end
reset_connection() click to toggle source
# File routemaster/client/connection.rb, line 52
def reset_connection
  @_conn = nil
end
send_event(event, topic, callback, t: nil, data: nil) click to toggle source
# File routemaster/client/connection.rb, line 28
def send_event(event, topic, callback, t: nil, data: nil)
  payload = { 'type' => event, 'url' => callback, 'timestamp' => t }
  payload['data'] = data unless data.nil?

  response = post("/topics/#{topic}") do |r|
    r.headers['Content-Type'] = 'application/json'
    r.body = Oj.dump(payload, mode: :strict)
  end

  raise ConnectionError, "event rejected (status: #{response.status})" unless response.success?

  # Any issues would have caused an exception to be thrown
  true
end
subscribe(options) click to toggle source
# File routemaster/client/connection.rb, line 43
def subscribe(options)
  response = post('/subscription') do |r|
    r.headers['Content-Type'] = 'application/json'
    r.body = Oj.dump(_stringify_keys options)
  end

  raise ConnectionError, "subscribe rejected (status: #{response.status})" unless response.success?
end

Private Class Methods

_conn() click to toggle source
# File routemaster/client/connection.rb, line 68
def _conn
  @_conn ||= Faraday.new(url, ssl: { verify: verify_ssl }) do |f|
    f.request :retry, max: 2, interval: 100e-3, backoff_factor: 2
    f.request :basic_auth, uuid, 'x'
    f.adapter :typhoeus

    f.options.timeout      = timeout
    f.options.open_timeout = timeout
  end
end
_stringify_keys(hash) click to toggle source
# File routemaster/client/connection.rb, line 60
def _stringify_keys(hash)
  hash.dup.tap do |h|
    h.keys.each do |k|
      h[k.to_s] = h.delete(k)
    end
  end
end