Module: Millionsend::Util

Defined in:
lib/millionsend/util.rb

Overview

Small shared helpers used across the resource modules.

Constant Summary collapse

LOOPBACK_HOSTS =
%w[localhost 127.0.0.1 ::1].freeze

Class Method Summary collapse

Class Method Details

.encode(value) ⇒ Object

Percent-encode a single URL path segment (contact ids and emails, which can contain "@" and "+"). encode_www_form_component is form encoding, so it maps space to "+" — wrong inside a path segment, where servers decode "+" literally. It already turned real plus signs into %2B, so every remaining "+" is a space and can safely become %20.



15
16
17
# File 'lib/millionsend/util.rb', line 15

def encode(value)
  URI.encode_www_form_component(value.to_s).gsub("+", "%20")
end

.include_query(include) ⇒ Object

?include= for the contact lists: the names comma-joined, or nil (dropped from the query) when none were given.



38
39
40
41
# File 'lib/millionsend/util.rb', line 38

def include_query(include)
  names = Array(include)
  names.empty? ? nil : names.join(",")
end

.insecure_http?(uri) ⇒ Boolean

True for an http:// URI whose host is not loopback.

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/millionsend/util.rb', line 22

def insecure_http?(uri)
  return false unless uri.scheme == "http"

  host = uri.host.to_s.downcase
  !LOOPBACK_HOSTS.include?(host) && !host.start_with?("127.")
end

.list_query(options) ⇒ Object

The keyset pagination params every list endpoint accepts. nil values are dropped when the query string is built.



31
32
33
34
# File 'lib/millionsend/util.rb', line 31

def list_query(options)
  options ||= {}
  { limit: options[:limit], after: options[:after], before: options[:before] }
end

.request_options(options) ⇒ Object

Per-request options for the Request constructor. Both call shapes land in the same trailing positional hash: the original idempotency_key: "k" and resend-ruby's keyword form options: { idempotency_key: "k" }, so the latter is unwrapped here.



47
48
49
50
51
# File 'lib/millionsend/util.rb', line 47

def request_options(options)
  options = options[:options] if options.is_a?(Hash) && options.key?(:options)
  options ||= {}
  { idempotency_key: options[:idempotency_key], batch_validation: options[:batch_validation] }
end