Sha256: 1f7adeca697501d2f50cd92c6eac860f4e60af7c8aed01226c6a49f36872027d

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'net/http'
require 'json'

module Tggl
  class Client
    def initialize(api_key = nil, options = {})
      @api_key = api_key
      @url = options[:url] || 'https://api.tggl.io/flags'
      @reporter = api_key.nil? || options[:reporting] == false ? nil :  Reporting.new(
        api_key,
        app_prefix: "ruby-client:#{VERSION}/Client",
        url: options[:reporting] == true ? nil : options[:reporting]&.[](:url),
        app: options[:reporting] == true ? nil : options[:reporting]&.[](:app)
      )
    end

    def eval_context(context)
      response = eval_contexts([context]).first
      raise StandardError.new "Unexpected empty response from Tggl" if response.nil?

      response
    end

    def eval_contexts(contexts)
      uri = URI(@url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      request = Net::HTTP::Post.new(uri.path, {
        'X-Tggl-Api-Key' => @api_key,
        'Content-Type' => 'application/json'
      })
      request.body = contexts.to_json

      response = http.request(request)
      result = JSON.parse(response.body, symbolize_names: true)

      if response.code.to_i > 200
        if result.nil?
          raise StandardError.new "Invalid response from Tggl: #{response.code}"
        end
        raise StandardError.new result['error']
      end

      result.map { |flags| Response.new(flags) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tggl-0.2.0 lib/tggl/client.rb