class Postmark::Client

Attributes

http_client[R]
max_retries[R]

Public Class Methods

new(api_token, options = {}) click to toggle source
# File lib/postmark/client.rb, line 7
def initialize(api_token, options = {})
  options = options.dup
  @max_retries = options.delete(:max_retries) || 0
  @http_client = HttpClient.new(api_token, options)
end

Public Instance Methods

api_key=(api_token)
Alias for: api_token=
api_token=(api_token) click to toggle source
# File lib/postmark/client.rb, line 13
def api_token=(api_token)
  http_client.api_token = api_token
end
Also aliased as: api_key=
find_each(path, name, options = {}) { |e| ... } click to toggle source
# File lib/postmark/client.rb, line 18
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 Instance Methods

format_batch_response(response, name) click to toggle source
# File lib/postmark/client.rb, line 89
def format_batch_response(response, name)
  [response['TotalCount'], format_response(response[name])]
end
format_response(response, options = {}) click to toggle source
# File lib/postmark/client.rb, line 63
def format_response(response, options = {})
  return {} unless response

  compatible = options.fetch(:compatible, false)
  deep = options.fetch(:deep, false)

  if response.kind_of? Array
    response.map { |entry| Postmark::HashHelper.to_ruby(entry, :compatible => compatible, :deep => deep) }
  else
    Postmark::HashHelper.to_ruby(response, :compatible => compatible, :deep => deep)
  end
end
get_resource_count(path, options = {}) click to toggle source
# File lib/postmark/client.rb, line 76
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
load_batch(path, name, options) click to toggle source
# File lib/postmark/client.rb, line 82
def load_batch(path, name, options)
  options[:offset] ||= 0
  options[:count] ||= 30
  response = http_client.get(path, options)
  format_batch_response(response, name)
end
serialize(data) click to toggle source
# File lib/postmark/client.rb, line 53
def serialize(data)
  Postmark::Json.encode(data)
end
take_response_of() { || ... } click to toggle source
# File lib/postmark/client.rb, line 57
def take_response_of
  [yield, nil]
rescue HttpServerError => e
  [e.full_response || {}, e]
end
with_retries() { || ... } click to toggle source
# File lib/postmark/client.rb, line 38
def with_retries
  yield
rescue HttpServerError, HttpClientError, TimeoutError, Errno::EINVAL,
       Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError,
       Net::ProtocolError, SocketError => e
  retries = retries ? retries + 1 : 1
  retriable = !e.respond_to?(:retry?) || e.retry?

  if retriable && retries < self.max_retries
    retry
  else
    raise e
  end
end