Sha256: c6db20edf99481771a534a9e32b0af5df42f74034e8149218f02e6ea3e9adae0

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# -*- encoding: utf-8 -*-

module SendGrid4r::REST
  module MarketingCampaigns
    module Contacts
      #
      # SendGrid Web API v3 Contacts - Custom Fields
      #
      module CustomFields
        include Request

        Field = Struct.new(:id, :name, :type, :value) do
          def eql?(other)
            id.eql?(other.id)
          end

          def hash
            id.hash
          end
        end

        Fields = Struct.new(:custom_fields)

        def self.url(custom_field_id = nil)
          url = "#{BASE_URL}/contactdb/custom_fields"
          url = "#{url}/#{custom_field_id}" unless custom_field_id.nil?
          url
        end

        def self.create_field(resp)
          return resp if resp.nil?
          Field.new(resp['id'], resp['name'], resp['type'], resp['value'])
        end

        def self.create_fields(resp)
          return resp if resp.nil?
          custom_fields = resp['custom_fields'].map do |field|
            Contacts::CustomFields.create_field(field)
          end
          Fields.new(custom_fields)
        end

        def post_custom_field(name:, type:, &block)
          params = {}
          params['name'] = name
          params['type'] = type
          resp = post(@auth, Contacts::CustomFields.url, params, &block)
          Contacts::CustomFields.create_field(resp)
        end

        def get_custom_fields(&block)
          resp = get(@auth, Contacts::CustomFields.url, &block)
          Contacts::CustomFields.create_fields(resp)
        end

        def get_custom_field(custom_field_id:, &block)
          resp = get(@auth, Contacts::CustomFields.url(custom_field_id), &block)
          Contacts::CustomFields.create_field(resp)
        end

        def delete_custom_field(custom_field_id:, &block)
          delete(@auth, Contacts::CustomFields.url(custom_field_id), &block)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sendgrid4r-1.11.0 lib/sendgrid4r/rest/marketing_campaigns/contacts/custom_fields.rb
sendgrid4r-1.10.0 lib/sendgrid4r/rest/marketing_campaigns/contacts/custom_fields.rb