Sha256: 8542df6ebccd3c7838aeaa31ff6c1238909b0c6d43e70caedcbb7a6e1961cbea

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

module Pushpad
  class Subscription
    class CountError < RuntimeError
    end

    def self.count(options = {})
      CountQuery.new(options).perform
    end

    class CountQuery
      attr_reader :options

      def initialize(options)
        @options = options
      end

      def perform
        project_id = options[:project_id] || Pushpad.project_id
        raise "You must set project_id" unless project_id

        endpoint = "https://pushpad.xyz/api/v1/projects/#{project_id}/subscriptions"
        response = Request.head(endpoint, query_parameters: query_parameters)

        unless response.code == "200"
          raise CountError, "Response #{response.code} #{response.message}: #{response.body}"
        end

        response["X-Total-Count"].to_i
      end

      private

      def query_parameters
        [uid_query_parameters, tag_query_parameters].flatten(1)
      end

      def uid_query_parameters
        options.fetch(:uids, []).map { |uid| ["uids[]", uid] }
      end

      def tag_query_parameters
        tags = options.fetch(:tags, [])

        if tags.is_a?(String)
          [["tags", tags]]
        else
          tags.map { |tag| ["tags[]", tag] }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pushpad-1.1.0 lib/pushpad/subscription.rb
pushpad-1.0.0 lib/pushpad/subscription.rb
pushpad-0.13.0 lib/pushpad/subscription.rb
pushpad-0.12.0 lib/pushpad/subscription.rb
pushpad-0.11.1 lib/pushpad/subscription.rb