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

.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