Sha256: ef14a584ef39e7380662a299e7991e304db4339e9511c60e42ad7004fea24a2d

Contents?: true

Size: 1.92 KB

Versions: 18

Compression:

Stored size: 1.92 KB

Contents

require 'intercom/generic_handlers/base_handler'

module Intercom
  module GenericHandlers
    module Count
      module ClassMethods
        def generic_count(method_sym, *arguments, &block)

          handler_class = Class.new(GenericHandlers::BaseHandler) do
            def handle
              match = method_string.match(GenericHandlers::Count.count_breakdown_matcher)
              if match && match[1] && match[2] && match[3].nil?
                do_broken_down_count(match[1], match[2])
              elsif method_string.end_with? '_count'
                return do_count
              else
                raise_no_method_missing_handler
              end
            end

            private

            def do_count
              entity.fetch_for_app.send(appwide_entity_to_count)['count']
            rescue Intercom::AttributeNotSetError
              # Indicates this this kind of counting is not supported
              raise_no_method_missing_handler
            end

            def do_broken_down_count(entity_to_count, count_context)
              result = entity.fetch_broken_down_count(entity_to_count, count_context)
              result.send(entity_to_count)[count_context]
            rescue Intercom::BadRequestError => ex
              # Indicates this this kind of counting is not supported
              ex.application_error_code == 'parameter_invalid' ? raise_no_method_missing_handler : raise
            end

            def appwide_entity_to_count; method_string.gsub(/_count$/, ''); end
          end

          handler_class.new(method_sym, arguments, self).handle
        end

      end

      def self.count_breakdown_matcher
        /([[:alnum:]]+)_counts_for_each_([[:alnum:]]+)/
      end

      def self.handles_method?(method_sym)
        method_sym.to_s.end_with? '_count' or method_sym.to_s.match(count_breakdown_matcher)
      end

      def self.included(base)
        base.extend(ClassMethods)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
cogniteev-intercom-2.5.4 lib/intercom/generic_handlers/count.rb
intercom-2.5.4 lib/intercom/generic_handlers/count.rb
intercom-2.4.4 lib/intercom/generic_handlers/count.rb
intercom-2.4.3 lib/intercom/generic_handlers/count.rb
intercom-2.4.2 lib/intercom/generic_handlers/count.rb
intercom-2.4.1 lib/intercom/generic_handlers/count.rb
intercom-2.4.0 lib/intercom/generic_handlers/count.rb
intercom-2.3.0 lib/intercom/generic_handlers/count.rb
intercom-2.2.4 lib/intercom/generic_handlers/count.rb
intercom-2.2.3 lib/intercom/generic_handlers/count.rb
intercom-2.2.2 lib/intercom/generic_handlers/count.rb
intercom-2.2.1 lib/intercom/generic_handlers/count.rb
intercom-2.2.0 lib/intercom/generic_handlers/count.rb
intercom-2.1.6 lib/intercom/generic_handlers/count.rb
intercom-2.1.5 lib/intercom/generic_handlers/count.rb
intercom-2.1.4 lib/intercom/generic_handlers/count.rb
intercom-2.1.3 lib/intercom/generic_handlers/count.rb
intercom-2.1.2 lib/intercom/generic_handlers/count.rb