Sha256: 2c27db8d8a3aba20db12889c58b71b0eb05dd6f95e1f56f06993f70c1f23c6ff

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'logger'
require 'faraday'
require 'faraday_middleware'

module Safelylaunch
  class HttpConnection
    attr_reader :api_token, :logger, :host, :connection, :cache_time

    def initialize(api_token:, logger: Logger.new(STDOUT), cache_time: nil, host: 'http://localhost:2300')
      @api_token = api_token
      @logger = logger
      @host = host
      @cache_time = cache_time

      @cache = HttpCache.new

      @connection = Faraday.new(url: host, request: { timeout: 10 }) do |conn|
        conn.response :json, content_type: %r{application/json}, parser_options: { symbolize_names: true }
        conn.response :logger, logger

        conn.adapter  Faraday.default_adapter
      end
    end

    def get(key)
      cache_time ? cached_check_toggle(key) : check_toggle(key)
    end

  private

    def cached_check_toggle(key)
      @cache.get(key) || @cache.put(key, check_toggle(key), cache_time)
    end

    def check_toggle(key)
      response = connection.get('/api/v1/check', token: api_token, key: key)
      response.body
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safelylaunch-0.1.0 lib/safelylaunch/http_connection.rb