Sha256: 400946df53a7084e72a2dc3c89e971072f1d8b9d03010bf4b8a72563a859335a

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

# frozen_string_literal: true

require "json"

module GreyNoise
  class API
    attr_reader :key

    def initialize(key: nil)
      @key = key
      @key ||= load_from_config
      @key ||= load_from_env

      raise ArgumentError if @key.nil?
    end

    def experimental
      @experimental ||= Clients::Experimental.new(key)
    end

    def meta
      @meta ||= Clients::Meta.new(key)
    end

    def noise
      @noise ||= Clients::Noise.new(key)
    end

    private

    def load_from_env
      ENV["GREYNOISE_API_KEY"]
    end

    def load_from_config
      path = File.expand_path("~/.config/greynoise/config")
      return nil unless File.exist?(path)

      begin
        data = File.read(path)
        json = JSON.parse(data)
        json["api_key"]
      rescue TypeError, JSON::ParserError => _e
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
greynoise-0.1.1 lib/greynoise/api.rb