Sha256: 0a746244e7b73fdaf260cb42a3f4d410000a7879130009040f23877ed6399b48

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

module Mautic
  class Proxy

    def initialize(connection, endpoint, options = nil)
      @connection = connection
      klass = "Mautic::#{endpoint.classify}"
      @target = klass.safe_constantize || Mautic.const_set(endpoint.classify, Class.new(Mautic::Model))
      @endpoint = endpoint
      @options = options || {}
    end

    def new(attributes = {})
      @target.new(@connection, attributes)
    end

    def all(options = {}, &block)
      if options[:limit] == 'all'

        options.delete(:limit)

        records = results = where(options)
        total = @last_response['total'].to_i
        while records.any?
          if block_given?
            records.each &block
          end
          break if results.size >= total

          records = where(options.merge(start: records.size))
          results.concat records
        end
      else
        results = where(options)
        results.each{|i| yield i } if block_given?
      end
      results
    end

    def where(params = {})
      q =  params.reverse_merge(@options[:default_params] || {})
      json = @connection.request(:get, "api/#{@endpoint}", {params: q })
      @last_response = json
      json[@endpoint].collect do |id, attributes|
        @target.new(@connection, attributes || id)
      end
    end

    def first
      where(limit: 1).first
    end

    def find(id)
      json = @connection.request(:get, "api/#{@endpoint}/#{id}")
      @last_response = json
      @target.new(@connection, json[@endpoint.singularize])
    end


  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mautic-2.2.2 lib/mautic/proxy.rb
mautic-2.1.1 lib/mautic/proxy.rb
mautic-0.1.8 lib/mautic/proxy.rb
mautic-0.1.7 lib/mautic/proxy.rb
mautic-1.0.5 lib/mautic/proxy.rb
mautic-0.1.6 lib/mautic/proxy.rb
mautic-0.1.4 lib/mautic/proxy.rb
mautic-1.0.3 lib/mautic/proxy.rb
mautic-1.0.1 lib/mautic/proxy.rb
mautic-0.1.3 lib/mautic/proxy.rb
mautic-0.1.2 lib/mautic/proxy.rb