Module: NodexPay::Validator
- Defined in:
- lib/nodex_pay/validator.rb
Constant Summary collapse
- AMOUNT_TYPES =
%w[USD JPY EUR AED SGD HKD CAD IDR PHP INR KRW].freeze
- COLOR_THEMES =
%w[dark light].freeze
- UI_MODES =
%w[switchable].freeze
- TRANSACTION_CODE_PATTERN =
/\A[A-Za-z0-9]{4}(?:-[A-Za-z0-9]{4}){2}\z/- VERIFY_TOKEN_PATTERN =
/\A[0-9a-f]{64}\z/
Class Method Summary collapse
- .amount_type(value) ⇒ Object
- .color_theme(value) ⇒ Object
- .credential(value, name) ⇒ Object
- .deadline(value) ⇒ Object
- .order_code(value) ⇒ Object
- .text(value, name) ⇒ Object
- .timeout(value, name) ⇒ Object
- .transaction_code(payload) ⇒ Object
- .ui_mode(value) ⇒ Object
- .verify_token(payload) ⇒ Object
- .webhook_result(payload) ⇒ Object
- .webhook_string(payload, field) ⇒ Object
Class Method Details
.amount_type(value) ⇒ Object
35 36 37 |
# File 'lib/nodex_pay/validator.rb', line 35 def amount_type(value) enum(value, "amount_type", AMOUNT_TYPES, :upcase) end |
.color_theme(value) ⇒ Object
39 40 41 |
# File 'lib/nodex_pay/validator.rb', line 39 def color_theme(value) enum(value, "color_theme", COLOR_THEMES, :downcase) end |
.credential(value, name) ⇒ Object
13 14 15 16 17 |
# File 'lib/nodex_pay/validator.rb', line 13 def credential(value, name) return value.dup.freeze if value.is_a?(String) && !value.empty? raise ConfigurationError, "#{name} must be a non-empty String" end |
.deadline(value) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/nodex_pay/validator.rb', line 56 def deadline(value) return nil if value.nil? seconds = value.is_a?(Time) ? value.to_i : value return seconds if seconds.is_a?(Integer) && !seconds.negative? raise ValidationError, "deadline must be a Time or non-negative Integer" end |
.order_code(value) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/nodex_pay/validator.rb', line 27 def order_code(value) unless value.is_a?(String) && !value.empty? && value.ascii_only? && value.bytesize <= 255 raise ValidationError, "order_code must be 1..255 ASCII bytes" end value.dup.freeze end |
.text(value, name) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/nodex_pay/validator.rb', line 47 def text(value, name) return nil if value.nil? unless value.is_a?(String) && value.encoding == Encoding::UTF_8 && value.valid_encoding? && value.length <= 100 raise ValidationError, "#{name} must be a valid UTF-8 String of at most 100 characters" end value.dup.freeze end |
.timeout(value, name) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/nodex_pay/validator.rb', line 19 def timeout(value, name) valid = value.is_a?(Numeric) && value.positive? valid &&= !value.respond_to?(:finite?) || value.finite? return value if valid raise ConfigurationError, "#{name} must be a positive finite number" end |
.transaction_code(payload) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/nodex_pay/validator.rb', line 72 def transaction_code(payload) value = webhook_string(payload, "transaction_code") return value if TRANSACTION_CODE_PATTERN.match?(value) raise InvalidResponseError, "Payment Result transaction_code has an invalid format" end |
.ui_mode(value) ⇒ Object
43 44 45 |
# File 'lib/nodex_pay/validator.rb', line 43 def ui_mode(value) enum(value, "ui_mode", UI_MODES, :downcase) end |
.verify_token(payload) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/nodex_pay/validator.rb', line 79 def verify_token(payload) value = webhook_string(payload, "verify_token") return value if VERIFY_TOKEN_PATTERN.match?(value) raise InvalidResponseError, "Payment Result verify_token has an invalid format" end |
.webhook_result(payload) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/nodex_pay/validator.rb', line 86 def webhook_result(payload) value = payload["result"] return value if value.equal?(true) || value.equal?(false) raise InvalidResponseError, "Payment Result field \"result\" must be a Boolean" end |
.webhook_string(payload, field) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/nodex_pay/validator.rb', line 65 def webhook_string(payload, field) value = payload[field] return value.dup.freeze if value.is_a?(String) && !value.empty? raise InvalidResponseError, "Payment Result field #{field.inspect} must be a non-empty String" end |