Sha256: 005e8197dc39c3f4113985a1f4a53b5eefc0634b6651184b0dae4e04c4b7ae9b

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

# -*- encoding: utf-8 -*-
$LOAD_PATH.unshift File.dirname(__FILE__)

require 'sendgrid4r/rest/request'

module SendGrid4r
  module REST
    #
    # SendGrid Web API v3 ApiKeys
    #
    module ApiKeys
      include SendGrid4r::REST::Request

      ApiKeys = Struct.new(:result)
      ApiKey = Struct.new(:name, :api_key_id, :api_key, :scope_set_id)

      def self.url(api_key = nil)
        url = "#{BASE_URL}/api_keys"
        url = "#{url}/#{api_key}" unless api_key.nil?
        url
      end

      def self.create_api_keys(resp)
        return resp if resp.nil?
        api_keys = []
        resp['result'].each do |api_key|
          api_keys.push(SendGrid4r::REST::ApiKeys.create_api_key(api_key))
        end
        ApiKeys.new(api_keys)
      end

      def self.create_api_key(resp)
        return resp if resp.nil?
        ApiKey.new(
          resp['name'],
          resp['api_key_id'],
          resp['api_key'],
          resp['scope_set_id']
        )
      end

      def get_api_keys(&block)
        resp = get(@auth, SendGrid4r::REST::ApiKeys.url, &block)
        SendGrid4r::REST::ApiKeys.create_api_keys(resp)
      end

      def post_api_key(name:, &block)
        params = {}
        params['name'] = name
        resp = post(@auth, SendGrid4r::REST::ApiKeys.url, params, &block)
        SendGrid4r::REST::ApiKeys.create_api_key(resp)
      end

      def delete_api_key(api_key_id:, &block)
        delete(@auth, SendGrid4r::REST::ApiKeys.url(api_key_id), &block)
      end

      def patch_api_key(api_key_id:, name:, &block)
        params = {}
        params['name'] = name
        endpoint = SendGrid4r::REST::ApiKeys.url(api_key_id)
        resp = patch(@auth, endpoint, params, &block)
        SendGrid4r::REST::ApiKeys.create_api_key(resp)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sendgrid4r-1.4.0 lib/sendgrid4r/rest/api_keys/api_keys.rb
sendgrid4r-1.3.0 lib/sendgrid4r/rest/api_keys/api_keys.rb
sendgrid4r-1.2.1 lib/sendgrid4r/rest/api_keys/api_keys.rb
sendgrid4r-1.2.0 lib/sendgrid4r/rest/api_keys/api_keys.rb
sendgrid4r-1.1.0 lib/sendgrid4r/rest/api_keys/api_keys.rb
sendgrid4r-1.0.0 lib/sendgrid4r/rest/api_keys/api_keys.rb