Sha256: 842b52368d6e5613f64f0cdab2ca42d1ad405c154b3912984312815d03ad1f44

Contents?: true

Size: 1.98 KB

Versions: 16

Compression:

Stored size: 1.98 KB

Contents

require 'cgi'

module Postmark
  module HttpClient
    class << self
      def post(path, data = '')
        handle_response(http.post(url_path(path), data, headers))
      end

      def put(path, data = '')
        handle_response(http.put(url_path(path), data, headers))
      end

      def get(path, query = {})
        handle_response(http.get(url_path(path + to_query_string(query)), headers))
      end

      protected

      def to_query_string(hash)
        return "" if hash.empty?
        "?" + hash.map { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join("&")
      end

      def protocol
        Postmark.secure ? "https" : "http"
      end

      def url
        URI.parse("#{protocol}://#{Postmark.host}:#{Postmark.port}/")
      end

      def handle_response(response)
        case response.code.to_i
        when 200
          return Postmark::Json.decode(response.body)
        when 401
          raise InvalidApiKeyError, error_message(response.body)
        when 422
          raise InvalidMessageError, error_message(response.body)
        when 500
          raise InternalServerError, response.body
        else
          raise UnknownError, response
        end
      end

      def headers
        @headers ||= HEADERS.merge({ "X-Postmark-Server-Token" => Postmark.api_key.to_s })
      end

      def url_path(path)
        Postmark.path_prefix + path
      end

      def http
        @http ||= build_http
      end

      def build_http
        http = Net::HTTP::Proxy(Postmark.proxy_host,
                                Postmark.proxy_port,
                                Postmark.proxy_user,
                                Postmark.proxy_pass).new(url.host, url.port)

        http.read_timeout = Postmark.http_read_timeout
        http.open_timeout = Postmark.http_open_timeout
        http.use_ssl = !!Postmark.secure
        http
      end

      def error_message(response_body)
        Postmark::Json.decode(response_body)["Message"]
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
postmark-0.9.10 lib/postmark/http_client.rb
postmark-0.9.9 lib/postmark/http_client.rb
postmark-0.9.8 lib/postmark/http_client.rb
postmark-0.9.6 lib/postmark/http_client.rb
postmark-0.9.5 lib/postmark/http_client.rb
postmark-0.9.4 lib/postmark/http_client.rb
postmark-0.9.3 lib/postmark/http_client.rb
postmark-0.9.2 lib/postmark/http_client.rb
postmark-0.9.1 lib/postmark/http_client.rb
postmark-0.9.0 lib/postmark/http_client.rb
postmark-0.8.0 lib/postmark/http_client.rb
challengepost-postmark-0.7.3 lib/postmark/http_client.rb
challengepost-postmark-0.7.2 lib/postmark/http_client.rb
challengepost-postmark-0.7.1 lib/postmark/http_client.rb
postmark-0.7.1 lib/postmark/http_client.rb
postmark-0.7.0 lib/postmark/http_client.rb