Module: StripeMock::RequestHandlers::ParamValidators

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/validators/param_validators.rb

Constant Summary collapse

SUPPORTED_PLAN_INTERVALS =
["month", "year", "week", "day"]
SUPPORTED_CURRENCIES =
[
  "usd", "aed", "afn", "all", "amd", "ang", "aoa", "ars", "aud", "awg", "azn", "bam", "bbd", "bdt", "bgn",
  "bif", "bmd", "bnd", "bob", "brl", "bsd", "bwp", "bzd", "cad", "cdf", "chf", "clp", "cny", "cop", "crc",
  "cve", "czk", "djf", "dkk", "dop", "dzd", "egp", "etb", "eur", "fjd", "fkp", "gbp", "gel", "gip", "gmd",
  "gnf", "gtq", "gyd", "hkd", "hnl", "hrk", "htg", "huf", "idr", "ils", "inr", "isk", "jmd", "jpy", "kes",
  "kgs", "khr", "kmf", "krw", "kyd", "kzt", "lak", "lbp", "lkr", "lrd", "lsl", "mad", "mdl", "mga", "mkd",
  "mmk", "mnt", "mop", "mro", "mur", "mvr", "mwk", "mxn", "myr", "mzn", "nad", "ngn", "nio", "nok", "npr",
  "nzd", "pab", "pen", "pgk", "php", "pkr", "pln", "pyg", "qar", "ron", "rsd", "rub", "rwf", "sar", "sbd",
  "scr", "sek", "sgd", "shp", "sll", "sos", "srd", "std", "szl", "thb", "tjs", "top", "try", "ttd", "twd",
  "tzs", "uah", "ugx", "uyu", "uzs", "vnd", "vuv", "wst", "xaf", "xcd", "xof", "xpf", "yer", "zar", "zmw",
  "eek", "lvl", "svc", "vef", "ltl"
]

Instance Method Summary collapse

Instance Method Details

#already_exists_message(obj_class) ⇒ Object



5
6
7
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 5

def already_exists_message(obj_class)
  "#{obj_class.to_s.split("::").last} already exists."
end

#invalid_currency_message(my_val) ⇒ Object



64
65
66
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 64

def invalid_currency_message(my_val)
  "Invalid currency: #{my_val.downcase}. Stripe currently supports these currencies: #{SUPPORTED_CURRENCIES.join(", ")}"
end

#invalid_integer_message(my_val) ⇒ Object



17
18
19
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 17

def invalid_integer_message(my_val)
  "Invalid integer: #{my_val}"
end

#invalid_plan_interval_messageObject



47
48
49
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 47

def invalid_plan_interval_message
  "Invalid interval: must be one of day, month, week, or year"
end

#missing_param_message(attr_name) ⇒ Object



13
14
15
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 13

def missing_param_message(attr_name)
  "Missing required param: #{attr_name}."
end

#missing_plan_amount_messageObject

PlanValidator



41
42
43
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 41

def missing_plan_amount_message
  "Plans require an `amount` parameter to be set."
end

#not_found_message(obj_class, obj_id) ⇒ Object



9
10
11
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 9

def not_found_message(obj_class, obj_id)
  "No such #{obj_class.to_s.split("::").last.downcase}: #{obj_id}"
end

#require_param(param_name) ⇒ Object

Raises:

  • (Stripe::InvalidRequestError)


141
142
143
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 141

def require_param(param_name)
  raise Stripe::InvalidRequestError.new("Missing required param: #{param_name}.", param_name.to_s, http_status: 400)
end

#validate_create_plan_params(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 68

def validate_create_plan_params(params)
  plan_id = params[:id].to_s
  product_id = params[:product]

  @base_strategy.create_plan_params.keys.each do |attr_name|
    message =
      if attr_name == :amount
        "Plans require an `#{attr_name}` parameter to be set."
      else
        "Missing required param: #{attr_name}."
      end
    raise Stripe::InvalidRequestError.new(message, attr_name) if params[attr_name].nil?
  end

  if plans[plan_id]
    message = already_exists_message(Stripe::Plan)
    raise Stripe::InvalidRequestError.new(message, :id)
  end

  unless products[product_id]
    message = not_found_message(Stripe::Product, product_id)
    raise Stripe::InvalidRequestError.new(message, :product)
  end

  unless SUPPORTED_PLAN_INTERVALS.include?(params[:interval])
    message = invalid_plan_interval_message
    raise Stripe::InvalidRequestError.new(message, :interval)
  end

  unless SUPPORTED_CURRENCIES.include?(params[:currency])
    message = invalid_currency_message(params[:currency])
    raise Stripe::InvalidRequestError.new(message, :currency)
  end

  unless params[:amount].integer?
    message = invalid_integer_message(params[:amount])
    raise Stripe::InvalidRequestError.new(message, :amount)
  end

end

#validate_create_price_params(params) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 109

def validate_create_price_params(params)
  price_id = params[:id].to_s

  require_param(:currency) unless params[:currency]
  unless params[:product] || params[:product_data]
    raise Stripe::InvalidRequestError("Requires product or product_data")
  end

  product_id = params[:product] || create_product(nil, nil, params[:product_data], nil).id

  if prices[price_id]
    message = already_exists_message(Stripe::Price)
    raise Stripe::InvalidRequestError.new(message, :id)
  end

  unless products[product_id]
    message = not_found_message(Stripe::Product, product_id)
    raise Stripe::InvalidRequestError.new(message, :product)
  end

  unless SUPPORTED_CURRENCIES.include?(params[:currency])
    message = invalid_currency_message(params[:currency])
    raise Stripe::InvalidRequestError.new(message, :currency)
  end
end

#validate_create_product_params(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 26

def validate_create_product_params(params)
  params[:id] = params[:id].to_s
  @base_strategy.create_product_params.keys.reject{ |k,_| k == :id }.each do |k|
    raise Stripe::InvalidRequestError.new(missing_param_message(k), k) if params[k].nil?
  end

  if products[ params[:id] ]
    raise Stripe::InvalidRequestError.new(already_exists_message(Stripe::Product), :id)
  end
end

#validate_list_prices_params(params) ⇒ Object



135
136
137
138
139
# File 'lib/stripe_mock/request_handlers/validators/param_validators.rb', line 135

def validate_list_prices_params(params)
  if params[:lookup_keys] && !params[:lookup_keys].is_a?(Array)
    raise Stripe::InvalidRequestError.new('Invalid array', :lookup_keys)
  end
end