Module: StripeMock::RequestHandlers::Products

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

Constant Summary collapse

SEARCH_FIELDS =
["active", "description", "name", "shippable", "url"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.add_handler 'post /v1/products',               :create_product
  base.add_handler 'get /v1/products/((?!search).*)', :retrieve_product
  base.add_handler 'get /v1/products/search',         :search_products
  base.add_handler 'post /v1/products/(.*)',          :update_product
  base.add_handler 'get /v1/products',                :list_products
  base.add_handler 'delete /v1/products/(.*)',        :destroy_product
end

Instance Method Details

#create_product(_route, _method_url, params, _headers) ⇒ Object



13
14
15
16
17
# File 'lib/stripe_mock/request_handlers/products.rb', line 13

def create_product(_route, _method_url, params, _headers)
  params[:id] ||= new_id('prod')
  validate_create_product_params(params)
  products[params[:id]] = Data.mock_product(params)
end

#destroy_product(route, method_url, _params, _headers) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/stripe_mock/request_handlers/products.rb', line 44

def destroy_product(route, method_url, _params, _headers)
  id = method_url.match(route).captures.first
  assert_existence :product, id, products[id]

  products.delete(id)
  { id: id, object: 'product', deleted: true }
end

#list_products(_route, _method_url, params, _headers) ⇒ Object



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

def list_products(_route, _method_url, params, _headers)
  limit = params[:limit] || 10
  Data.mock_list_object(products.values.take(limit), params)
end

#retrieve_product(route, method_url, _params, _headers) ⇒ Object



19
20
21
22
# File 'lib/stripe_mock/request_handlers/products.rb', line 19

def retrieve_product(route, method_url, _params, _headers)
  id = method_url.match(route).captures.first
  assert_existence :product, id, products[id]
end

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



37
38
39
40
41
42
# File 'lib/stripe_mock/request_handlers/products.rb', line 37

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

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

#update_product(route, method_url, params, _headers) ⇒ Object



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

def update_product(route, method_url, params, _headers)
  id = method_url.match(route).captures.first
  product = assert_existence :product, id, products[id]

  product.merge!(params)
end