File: client.rb

package info (click to toggle)
ruby-postmark 1.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 5,413; makefile: 4
file content (94 lines) | stat: -rw-r--r-- 2,506 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'enumerator'

module Postmark
  class Client
    attr_reader :http_client, :max_retries

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

    def api_token=(api_token)
      http_client.api_token = api_token
    end
    alias_method :api_key=, :api_token=

    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 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

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

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

    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

    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)
      format_batch_response(response, name)
    end

    def format_batch_response(response, name)
      [response['TotalCount'], format_response(response[name])]
    end

  end
end