class Postmark::HttpClient
Constants
- DEFAULTS
Attributes
api_key[RW]
api_key=[RW]
api_token[RW]
auth_header_name[R]
host[R]
http[R]
http_open_timeout[R]
http_read_timeout[R]
http_ssl_version[R]
path_prefix[R]
port[R]
proxy_host[R]
proxy_pass[R]
proxy_port[R]
proxy_user[R]
secure[R]
Public Class Methods
new(api_token, options = {})
click to toggle source
# File lib/postmark/http_client.rb, line 24 def initialize(api_token, options = {}) @api_token = api_token @request_mutex = Mutex.new apply_options(options) @http = build_http end
Public Instance Methods
delete(path, query = {})
click to toggle source
# File lib/postmark/http_client.rb, line 47 def delete(path, query = {}) do_request { |client| client.delete(url_path(path + to_query_string(query)), headers) } end
get(path, query = {})
click to toggle source
# File lib/postmark/http_client.rb, line 43 def get(path, query = {}) do_request { |client| client.get(url_path(path + to_query_string(query)), headers) } end
patch(path, data = '')
click to toggle source
# File lib/postmark/http_client.rb, line 39 def patch(path, data = '') do_request { |client| client.patch(url_path(path), data, headers) } end
post(path, data = '')
click to toggle source
# File lib/postmark/http_client.rb, line 31 def post(path, data = '') do_request { |client| client.post(url_path(path), data, headers) } end
protocol()
click to toggle source
# File lib/postmark/http_client.rb, line 51 def protocol self.secure ? 'https' : 'http' end
put(path, data = '')
click to toggle source
# File lib/postmark/http_client.rb, line 35 def put(path, data = '') do_request { |client| client.put(url_path(path), data, headers) } end
Protected Instance Methods
apply_options(options = {})
click to toggle source
# File lib/postmark/http_client.rb, line 57 def apply_options(options = {}) options = Hash[*options.select { |_, v| !v.nil? }.flatten] DEFAULTS.merge(options).each_pair do |name, value| instance_variable_set(:"@#{name}", value) end @port = options[:port] || (@secure ? 443 : 80) end
build_http()
click to toggle source
# File lib/postmark/http_client.rb, line 100 def build_http http = Net::HTTP::Proxy(self.proxy_host, self.proxy_port, self.proxy_user, self.proxy_pass).new(url.host, url.port) http.read_timeout = self.http_read_timeout http.open_timeout = self.http_open_timeout http.use_ssl = !!self.secure http.ssl_version = self.http_ssl_version if self.http_ssl_version && http.respond_to?(:ssl_version=) http end
do_request() { |http| ... }
click to toggle source
# File lib/postmark/http_client.rb, line 90 def do_request @request_mutex.synchronize do handle_response(yield(http)) end rescue Timeout::Error => e raise TimeoutError.new(e) rescue Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError => e raise HttpClientError.new(e.message) end
handle_response(response)
click to toggle source
# File lib/postmark/http_client.rb, line 74 def handle_response(response) if response.code.to_i == 200 Postmark::Json.decode(response.body) else raise HttpServerError.build(response.code, response.body) end end
headers()
click to toggle source
# File lib/postmark/http_client.rb, line 82 def headers HEADERS.merge(self.auth_header_name => self.api_token.to_s) end
to_query_string(hash)
click to toggle source
# File lib/postmark/http_client.rb, line 65 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
url()
click to toggle source
# File lib/postmark/http_client.rb, line 70 def url URI.parse("#{protocol}://#{self.host}:#{self.port}/") end
url_path(path)
click to toggle source
# File lib/postmark/http_client.rb, line 86 def url_path(path) self.path_prefix + path end