Sha256: c69b48aabb55ad4697b29486366c08f99c5b23fb009d9a673468cbc2bec69ba8

Contents?: true

Size: 1.93 KB

Versions: 8

Compression:

Stored size: 1.93 KB

Contents

require 'enumerator'

module Postmark
  class Client
    attr_reader :http_client, :max_retries

    def initialize(api_key, options = {})
      options = options.dup
      @max_retries = options.delete(:max_retries) || 3
      @http_client = HttpClient.new(api_key, options)
    end

    def api_key=(api_key)
      http_client.api_key = api_key
    end

    def find_each(path, name, options = {})
      if block_given?
        options = options.dup
        i, total_count = [0, 1]

        while i < total_count
          options[:offset] = i
          total_count, collection = load_batch(path, name, options)
          collection.each { |e| yield e }
          i += collection.size
        end
      else
        enum_for(:find_each, path, name, options) do
          get_resource_count(path, options)
        end
      end
    end

    protected

    def with_retries
      yield
    rescue DeliveryError
      retries = retries ? retries + 1 : 1
      if retries < self.max_retries
        retry
      else
        raise
      end
    end

    def serialize(data)
      Postmark::Json.encode(data)
    end

    def take_response_of
      [yield, nil]
    rescue DeliveryError => e
      [e.full_response || {}, e]
    end

    def format_response(response, compatible = false)
      return {} unless response

      if response.kind_of? Array
        response.map { |entry| Postmark::HashHelper.to_ruby(entry, compatible) }
      else
        Postmark::HashHelper.to_ruby(response, compatible)
      end
    end

    def get_resource_count(path, options = {})
      # At this point Postmark API returns 0 as total if you request 0 documents
      total_count, _ = load_batch(path, nil, options.merge(:count => 1))
      total_count
    end

    def load_batch(path, name, options)
      options[:offset] ||= 0
      options[:count] ||= 30
      response = http_client.get(path, options)
      [response['TotalCount'], format_response(response[name])]
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
postmark-1.4.3 lib/postmark/client.rb
postmark-1.4.2 lib/postmark/client.rb
postmark-1.4.1 lib/postmark/client.rb
postmark-1.4.0 lib/postmark/client.rb
postmark-1.3.1 lib/postmark/client.rb
postmark-1.3.0 lib/postmark/client.rb
postmark-1.2.1 lib/postmark/client.rb
postmark-1.2.0 lib/postmark/client.rb