Module: Bayarcash::Util
- Defined in:
- lib/bayarcash/util.rb
Overview
Small helpers that reproduce PHP string/array semantics the gateway relies on.
Class Method Summary collapse
-
.build_query(params, prefix = nil) ⇒ String
Build a URL-encoded query string, reproducing PHP's
http_build_query(RFC1738 encoding, bracket notation for nested arrays and hashes). -
.php_falsy?(value) ⇒ Boolean
Whether a decoded JSON value is "falsy" in the PHP sense.
-
.php_string(value) ⇒ String
Cast a value to a string exactly the way PHP's
implode/(string)cast does. -
.safe_json(body) ⇒ Object?
Parse a JSON string, returning nil on failure instead of raising.
-
.url_encode(value) ⇒ String
URL-encode a component the way PHP's
urlencodedoes (spaces become "+").
Class Method Details
.build_query(params, prefix = nil) ⇒ String
Build a URL-encoded query string, reproducing PHP's http_build_query
(RFC1738 encoding, bracket notation for nested arrays and hashes).
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bayarcash/util.rb', line 55 def build_query(params, prefix = nil) parts = [] params.each do |key, value| composed_key = prefix ? "#{prefix}[#{key}]" : key.to_s case value when Hash nested = build_query(value, composed_key) parts << nested unless nested.empty? when Array value.each_with_index do |item, index| if item.is_a?(Hash) || item.is_a?(Array) nested = build_query({ index => item }, composed_key) parts << nested unless nested.empty? else parts << "#{url_encode("#{composed_key}[#{index}]")}=#{url_encode(item)}" end end else parts << "#{url_encode(composed_key)}=#{url_encode(value)}" end end parts.join("&") end |
.php_falsy?(value) ⇒ Boolean
Whether a decoded JSON value is "falsy" in the PHP sense.
Mirrors PHP's json_decode($body, true) ?: $body short-circuit so that empty
or falsy payloads fall back to the raw response body.
31 32 33 34 35 36 37 38 39 |
# File 'lib/bayarcash/util.rb', line 31 def php_falsy?(value) case value when nil, false then true when 0, 0.0 then true when "", "0" then true when Array, Hash then value.empty? else false end end |
.php_string(value) ⇒ String
Cast a value to a string exactly the way PHP's implode/(string) cast does.
This matters for byte-compatible checksum generation: nil becomes an empty
string, booleans become "1"/"" and everything else uses its string form.
15 16 17 18 19 20 21 22 |
# File 'lib/bayarcash/util.rb', line 15 def php_string(value) case value when nil then "" when true then "1" when false then "" else value.to_s end end |
.safe_json(body) ⇒ Object?
Parse a JSON string, returning nil on failure instead of raising.
86 87 88 89 90 |
# File 'lib/bayarcash/util.rb', line 86 def safe_json(body) JSON.parse(body) rescue JSON::ParserError, TypeError nil end |
.url_encode(value) ⇒ String
URL-encode a component the way PHP's urlencode does (spaces become "+").
45 46 47 |
# File 'lib/bayarcash/util.rb', line 45 def url_encode(value) URI.encode_www_form_component(php_string(value)) end |