Class: Postmen::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/postmen/connection.rb

Overview

Connection class is used to perform any HTTP connections, it also does error handling.

Constant Summary collapse

MAX_REQUESTS =

Maximum number of retries

5
MAIN_DOMAIN =

Main domain used during normal usage.

'postmen.com'.freeze
FAILOVER_DOMAIN =

Failover domain used during DNS issues with main domain

'postmen.net'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection



14
15
16
# File 'lib/postmen/connection.rb', line 14

def initialize
  @requests = 0
end

Class Method Details

.endpoint(subdomain, failover = false) ⇒ Object

Returns the endpoint used in SDK, based on the region(subdomain) and a failover switch



73
74
75
76
77
# File 'lib/postmen/connection.rb', line 73

def self.endpoint(subdomain, failover = false)
  URI::HTTPS.build(scheme: 'https',
                   host: hostname(subdomain, failover),
                   path: '/v3').to_s
end

.hostname(subdomain, failover) ⇒ Object

Returns the hostname based on the region(subdomain) and a failover switch



83
84
85
86
# File 'lib/postmen/connection.rb', line 83

def self.hostname(subdomain, failover)
  base = failover ? FAILOVER_DOMAIN : MAIN_DOMAIN
  [subdomain, base].join('.')
end

Instance Method Details

#delete(path) ⇒ Object

Performs a HTTP DELETE request

Examples:

.delete('/shipper-accounts/11111')


62
63
64
65
66
# File 'lib/postmen/connection.rb', line 62

def delete(path)
  HTTP
    .headers(headers)
    .delete(get_full_url(path))
end

#get(path, options = {}) ⇒ Object

Performs a HTTP GET request.

Examples:

.get('/labels')
.get('/labels', { params: { limit: 5 } })


25
26
27
28
29
# File 'lib/postmen/connection.rb', line 25

def get(path, options = {})
  with_error_handling do
    Response.new(raw_get(path, options)).tap(&:parse_response!)
  end
end

#post(path, options = {}) ⇒ Object

Performs a HTTP POST request.

Examples:

.post('/labels')
.post('/labels', { json: { my: { sample: :data } } })


38
39
40
41
42
# File 'lib/postmen/connection.rb', line 38

def post(path, options = {})
  with_error_handling do
    Response.new(raw_post(path, options))
  end
end

#put(path, options = {}) ⇒ Object

Performs a HTTP PUT request.

Examples:

.put('/shipper-accounts/123/info')
..put('/shipper-accounts/123/info', { json: { my: { sample: :data } } })


51
52
53
54
55
# File 'lib/postmen/connection.rb', line 51

def put(path, options = {})
  with_error_handling do
    Response.new(raw_put(path, options)).tap(&:parse_response!)
  end
end