Module: StripeMock::RequestHandlers::Prices

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/prices.rb

Constant Summary collapse

SEARCH_FIELDS =
["active", "currency", "lookup_key", "product", "type"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/stripe_mock/request_handlers/prices.rb', line 5

def Prices.included(klass)
  klass.add_handler 'post /v1/prices',               :new_price
  klass.add_handler 'post /v1/prices/(.*)',          :update_price
  klass.add_handler 'get /v1/prices/((?!search).*)', :get_price
  klass.add_handler 'get /v1/prices/search',         :search_prices
  klass.add_handler 'get /v1/prices',                :list_prices
end

Instance Method Details

#get_price(route, method_url, params, headers) ⇒ Object



31
32
33
34
# File 'lib/stripe_mock/request_handlers/prices.rb', line 31

def get_price(route, method_url, params, headers)
  route =~ method_url
  assert_existence :price, $1, prices[$1]
end

#list_prices(route, method_url, params, headers) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stripe_mock/request_handlers/prices.rb', line 36

def list_prices(route, method_url, params, headers)
  limit = params[:limit] ? params[:limit] : 10
  price_data = prices.values
  validate_list_prices_params(params)

  if params.key?(:lookup_keys)
    price_data.select! do |price|
      params[:lookup_keys].include?(price[:lookup_key])
    end
  end

  if params.key?(:currency)
    price_data.select! do |price|
      params[:currency] == price[:currency]
    end
  end

  if params.key?(:product)
    price_data.select! do |price|
      params[:product] == price[:product]
    end
  end

  Data.mock_list_object(price_data.first(limit), params.merge!(limit: limit))
end

#new_price(route, method_url, params, headers) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stripe_mock/request_handlers/prices.rb', line 13

def new_price(route, method_url, params, headers)
  params[:id] ||= new_id('price')

  if params[:product_data]
    params[:product] = create_product(nil, nil, params[:product_data], nil)[:id] unless params[:product]
    params.delete(:product_data)
  end

  validate_create_price_params(params)
  prices[ params[:id] ] = Data.mock_price(params)
end

#search_prices(route, method_url, params, headers) ⇒ Object



63
64
65
66
67
68
# File 'lib/stripe_mock/request_handlers/prices.rb', line 63

def search_prices(route, method_url, params, headers)
  require_param(:query) unless params[:query]

  results = search_results(prices.values, params[:query], fields: SEARCH_FIELDS, resource_name: "prices")
  Data.mock_list_object(results, params)
end

#update_price(route, method_url, params, headers) ⇒ Object



25
26
27
28
29
# File 'lib/stripe_mock/request_handlers/prices.rb', line 25

def update_price(route, method_url, params, headers)
  route =~ method_url
  assert_existence :price, $1, prices[$1]
  prices[$1].merge!(params)
end