spree_api 1.2.5 → 1.3.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. data/.rspec +1 -0
  2. data/app/controllers/spree/api/addresses_controller.rb +17 -0
  3. data/app/controllers/spree/api/base_controller.rb +111 -0
  4. data/app/controllers/spree/api/countries_controller.rb +14 -0
  5. data/app/controllers/spree/api/images_controller.rb +29 -0
  6. data/app/controllers/spree/api/line_items_controller.rb +38 -0
  7. data/app/controllers/spree/api/orders_controller.rb +81 -0
  8. data/app/controllers/spree/api/payments_controller.rb +75 -0
  9. data/app/controllers/spree/api/product_properties_controller.rb +61 -0
  10. data/app/controllers/spree/api/products_controller.rb +45 -0
  11. data/app/controllers/spree/api/return_authorizations_controller.rb +50 -0
  12. data/app/controllers/spree/api/shipments_controller.rb +41 -0
  13. data/app/controllers/spree/api/taxonomies_controller.rb +46 -0
  14. data/app/controllers/spree/api/taxons_controller.rb +49 -0
  15. data/app/controllers/spree/api/variants_controller.rb +71 -0
  16. data/app/controllers/spree/api/zones_controller.rb +43 -0
  17. data/app/helpers/spree/api/api_helpers.rb +0 -5
  18. data/app/overrides/api_admin_user_edit_form.rb +1 -1
  19. data/app/views/spree/admin/users/_api_fields.html.erb +26 -14
  20. data/app/views/spree/api/{v1/addresses/show.rabl → addresses/show.v1.rabl} +0 -0
  21. data/app/views/spree/api/{v1/countries/index.rabl → countries/index.v1.rabl} +0 -0
  22. data/app/views/spree/api/{v1/countries/show.rabl → countries/show.v1.rabl} +0 -0
  23. data/app/views/spree/api/{v1/errors/gateway_error.rabl → errors/gateway_error.v1.rabl} +0 -0
  24. data/app/views/spree/api/{v1/errors/invalid_api_key.rabl → errors/invalid_api_key.v1.rabl} +0 -0
  25. data/app/views/spree/api/{v1/errors/invalid_resource.rabl → errors/invalid_resource.v1.rabl} +0 -0
  26. data/app/views/spree/api/{v1/errors/must_specify_api_key.rabl → errors/must_specify_api_key.v1.rabl} +0 -0
  27. data/app/views/spree/api/{v1/errors/not_found.rabl → errors/not_found.v1.rabl} +0 -0
  28. data/app/views/spree/api/{v1/errors/unauthorized.rabl → errors/unauthorized.v1.rabl} +0 -0
  29. data/app/views/spree/api/{v1/images/show.rabl → images/show.v1.rabl} +0 -0
  30. data/app/views/spree/api/{v1/line_items/new.rabl → line_items/new.v1.rabl} +0 -0
  31. data/app/views/spree/api/{v1/line_items/show.rabl → line_items/show.v1.rabl} +1 -1
  32. data/app/views/spree/api/{v1/orders/address.rabl → orders/address.v1.rabl} +0 -0
  33. data/app/views/spree/api/{v1/orders/canceled.rabl → orders/canceled.v1.rabl} +0 -0
  34. data/app/views/spree/api/{v1/orders/cart.rabl → orders/cart.v1.rabl} +0 -0
  35. data/app/views/spree/api/{v1/orders/complete.rabl → orders/complete.v1.rabl} +0 -0
  36. data/app/views/spree/api/{v1/orders/could_not_transition.rabl → orders/could_not_transition.v1.rabl} +0 -0
  37. data/app/views/spree/api/{v1/orders/delivery.rabl → orders/delivery.v1.rabl} +0 -0
  38. data/app/views/spree/api/{v1/orders/index.rabl → orders/index.v1.rabl} +0 -0
  39. data/app/views/spree/api/{v1/orders/invalid_shipping_method.rabl → orders/invalid_shipping_method.v1.rabl} +0 -0
  40. data/app/views/spree/api/{v1/orders/payment.rabl → orders/payment.v1.rabl} +0 -0
  41. data/app/views/spree/api/{v1/orders/show.rabl → orders/show.v1.rabl} +6 -6
  42. data/app/views/spree/api/{v1/payments/credit_over_limit.rabl → payments/credit_over_limit.v1.rabl} +0 -0
  43. data/app/views/spree/api/{v1/payments/index.rabl → payments/index.v1.rabl} +0 -0
  44. data/app/views/spree/api/{v1/payments/new.rabl → payments/new.v1.rabl} +0 -0
  45. data/app/views/spree/api/{v1/payments/show.rabl → payments/show.v1.rabl} +0 -0
  46. data/app/views/spree/api/{v1/product_properties/index.rabl → product_properties/index.v1.rabl} +0 -0
  47. data/app/views/spree/api/{v1/product_properties/new.rabl → product_properties/new.v1.rabl} +0 -0
  48. data/app/views/spree/api/{v1/product_properties/show.rabl → product_properties/show.v1.rabl} +0 -0
  49. data/app/views/spree/api/{v1/products/index.rabl → products/index.v1.rabl} +1 -2
  50. data/app/views/spree/api/{v1/products/new.rabl → products/new.v1.rabl} +0 -0
  51. data/app/views/spree/api/{v1/products/product.rabl → products/product.v1.rabl} +0 -0
  52. data/app/views/spree/api/{v1/products/show.rabl → products/show.v1.rabl} +1 -1
  53. data/app/views/spree/api/{v1/return_authorizations/index.rabl → return_authorizations/index.v1.rabl} +0 -0
  54. data/app/views/spree/api/{v1/return_authorizations/new.rabl → return_authorizations/new.v1.rabl} +0 -0
  55. data/app/views/spree/api/{v1/return_authorizations/show.rabl → return_authorizations/show.v1.rabl} +0 -0
  56. data/app/views/spree/api/{v1/shipments/cannot_ready_shipment.rabl → shipments/cannot_ready_shipment.v1.rabl} +0 -0
  57. data/app/views/spree/api/{v1/shipments/show.rabl → shipments/show.v1.rabl} +0 -0
  58. data/app/views/spree/api/{v1/taxonomies/index.rabl → taxonomies/index.v1.rabl} +1 -1
  59. data/app/views/spree/api/{v1/taxonomies/nested.rabl → taxonomies/nested.v1.rabl} +1 -1
  60. data/app/views/spree/api/{v1/taxonomies/new.rabl → taxonomies/new.v1.rabl} +0 -0
  61. data/app/views/spree/api/{v1/taxonomies/show.rabl → taxonomies/show.v1.rabl} +1 -1
  62. data/app/views/spree/api/{v1/taxons/index.rabl → taxons/index.v1.rabl} +1 -1
  63. data/app/views/spree/api/{v1/taxons/new.rabl → taxons/new.v1.rabl} +0 -0
  64. data/app/views/spree/api/{v1/taxons/show.rabl → taxons/show.v1.rabl} +0 -0
  65. data/app/views/spree/api/{v1/taxons/taxons.rabl → taxons/taxons.v1.rabl} +1 -1
  66. data/app/views/spree/api/{v1/variants/index.rabl → variants/index.v1.rabl} +0 -0
  67. data/app/views/spree/api/{v1/variants/new.rabl → variants/new.v1.rabl} +0 -0
  68. data/app/views/spree/api/{v1/variants/show.rabl → variants/show.v1.rabl} +1 -1
  69. data/app/views/spree/api/{v1/variants/variant.rabl → variants/variant.v1.rabl} +0 -0
  70. data/app/views/spree/api/{v1/zones/index.rabl → zones/index.v1.rabl} +1 -1
  71. data/app/views/spree/api/{v1/zones/show.rabl → zones/show.v1.rabl} +0 -0
  72. data/config/initializers/metal_load_paths.rb +1 -1
  73. data/config/routes.rb +34 -37
  74. data/db/migrate/20120411123334_resize_api_key_field.rb +1 -1
  75. data/lib/spree/api/engine.rb +8 -1
  76. data/lib/spree_api.rb +1 -0
  77. data/spec/controllers/spree/api/{v1/addresses_controller_spec.rb → addresses_controller_spec.rb} +3 -3
  78. data/spec/controllers/spree/api/{v1/base_controller_spec.rb → base_controller_spec.rb} +2 -9
  79. data/spec/controllers/spree/api/{v1/countries_controller_spec.rb → countries_controller_spec.rb} +5 -5
  80. data/spec/controllers/spree/api/{v1/images_controller_spec.rb → images_controller_spec.rb} +1 -1
  81. data/spec/controllers/spree/api/{v1/line_items_controller_spec.rb → line_items_controller_spec.rb} +2 -2
  82. data/spec/controllers/spree/api/{v1/orders_controller_spec.rb → orders_controller_spec.rb} +7 -12
  83. data/spec/controllers/spree/api/{v1/payments_controller_spec.rb → payments_controller_spec.rb} +2 -2
  84. data/spec/controllers/spree/api/{v1/product_properties_controller_spec.rb → product_properties_controller_spec.rb} +2 -3
  85. data/spec/controllers/spree/api/{v1/products_controller_spec.rb → products_controller_spec.rb} +14 -7
  86. data/spec/controllers/spree/api/{v1/return_authorizations_controller_spec.rb → return_authorizations_controller_spec.rb} +4 -4
  87. data/spec/controllers/spree/api/{v1/shipments_controller_spec.rb → shipments_controller_spec.rb} +4 -9
  88. data/spec/controllers/spree/api/{v1/taxonomies_controller_spec.rb → taxonomies_controller_spec.rb} +11 -11
  89. data/spec/controllers/spree/api/{v1/taxons_controller_spec.rb → taxons_controller_spec.rb} +7 -7
  90. data/spec/controllers/spree/api/{v1/unauthenticated_products_controller_spec.rb → unauthenticated_products_controller_spec.rb} +1 -1
  91. data/spec/controllers/spree/api/{v1/variants_controller_spec.rb → variants_controller_spec.rb} +4 -5
  92. data/spec/controllers/spree/api/{v1/zones_controller_spec.rb → zones_controller_spec.rb} +7 -7
  93. data/spec/support/controller_hacks.rb +1 -1
  94. data/spec/support/have_attributes_matcher.rb +2 -2
  95. data/spree_api.gemspec +1 -0
  96. metadata +139 -115
  97. checksums.yaml +0 -7
  98. data/app/controllers/spree/api/v1/addresses_controller.rb +0 -19
  99. data/app/controllers/spree/api/v1/base_controller.rb +0 -111
  100. data/app/controllers/spree/api/v1/countries_controller.rb +0 -18
  101. data/app/controllers/spree/api/v1/images_controller.rb +0 -32
  102. data/app/controllers/spree/api/v1/inventory_units_controller.rb +0 -50
  103. data/app/controllers/spree/api/v1/line_items_controller.rb +0 -40
  104. data/app/controllers/spree/api/v1/orders_controller.rb +0 -83
  105. data/app/controllers/spree/api/v1/payments_controller.rb +0 -77
  106. data/app/controllers/spree/api/v1/product_properties_controller.rb +0 -64
  107. data/app/controllers/spree/api/v1/products_controller.rb +0 -47
  108. data/app/controllers/spree/api/v1/return_authorizations_controller.rb +0 -53
  109. data/app/controllers/spree/api/v1/shipments_controller.rb +0 -43
  110. data/app/controllers/spree/api/v1/taxonomies_controller.rb +0 -50
  111. data/app/controllers/spree/api/v1/taxons_controller.rb +0 -51
  112. data/app/controllers/spree/api/v1/variants_controller.rb +0 -74
  113. data/app/controllers/spree/api/v1/zones_controller.rb +0 -45
  114. data/app/views/spree/api/v1/inventory_units/show.rabl +0 -2
  115. data/spec/controllers/spree/api/v1/inventory_units_controller_spec.rb +0 -46
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 54f6dbb9f70a86f5a1de1ce03b891244b3b9710f
4
- data.tar.gz: 7be3cb5a340625cb7739d941f79d20f769b03632
5
- SHA512:
6
- metadata.gz: 687d37cad06765fef369e378d961842981b2933821a262d6037f81e94cf471d23d1f4efa6d14a26285bad8dc1b24e1fe5f585a740678ed7f0c6b174a2011dd70
7
- data.tar.gz: 6554da72d40fbd9222941c8e9d79162bd6e628fbe6e71dfbb6c7d434fa99ca46cce088a17fb50cdf731cbf8b4bdd550b738df0c6a941de4e670048e4263c69f2
@@ -1,19 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class AddressesController < Spree::Api::V1::BaseController
5
- def show
6
- @address = Address.find(params[:id])
7
- authorize! :read, @address
8
- end
9
-
10
- def update
11
- @address = Address.find(params[:id])
12
- authorize! :read, @address
13
- @address.update_attributes(params[:address])
14
- render :show, :status => 200
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,111 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class BaseController < ActionController::Metal
5
- include Spree::Api::ControllerSetup
6
-
7
- attr_accessor :current_api_user
8
-
9
- before_filter :set_content_type
10
- before_filter :check_for_api_key, :if => :requires_authentication?
11
- before_filter :authenticate_user
12
-
13
- rescue_from Exception, :with => :error_during_processing
14
- rescue_from CanCan::AccessDenied, :with => :unauthorized
15
- rescue_from ActiveRecord::RecordNotFound, :with => :not_found
16
-
17
- helper Spree::Api::ApiHelpers
18
-
19
- def map_nested_attributes_keys(klass, attributes)
20
- nested_keys = klass.nested_attributes_options.keys
21
- attributes.inject({}) do |h, (k,v)|
22
- key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
23
- h[key] = v
24
- h
25
- end.with_indifferent_access
26
- end
27
-
28
- private
29
-
30
- def set_content_type
31
- content_type = case params[:format]
32
- when "json"
33
- "application/json"
34
- when "xml"
35
- "text/xml"
36
- end
37
- headers["Content-Type"] = content_type
38
- end
39
-
40
- def check_for_api_key
41
- render "spree/api/v1/errors/must_specify_api_key", :status => 401 and return if api_key.blank?
42
- end
43
-
44
- def authenticate_user
45
- if requires_authentication? || api_key.present?
46
- unless @current_api_user = Spree.user_class.find_by_spree_api_key(api_key.to_s)
47
- render "spree/api/v1/errors/invalid_api_key", :status => 401 and return
48
- end
49
- else
50
- # Effectively, an anonymous user
51
- @current_api_user = Spree.user_class.new
52
- end
53
- end
54
-
55
- def unauthorized
56
- render "spree/api/v1/errors/unauthorized", :status => 401 and return
57
- end
58
-
59
- def requires_authentication?
60
- Spree::Api::Config[:requires_authentication]
61
- end
62
-
63
- def not_found
64
- render "spree/api/v1/errors/not_found", :status => 404 and return
65
- end
66
-
67
- def error_during_processing(exception)
68
- render :text => { :exception => exception.message }.to_json,
69
- :status => 422 and return
70
- end
71
-
72
- def current_ability
73
- Spree::Ability.new(current_api_user)
74
- end
75
-
76
- def invalid_resource!(resource)
77
- @resource = resource
78
- render "spree/api/v1/errors/invalid_resource", :status => 422
79
- end
80
-
81
- def api_key
82
- request.headers["X-Spree-Token"] || params[:token]
83
- end
84
- helper_method :api_key
85
-
86
- def find_product(id)
87
- begin
88
- product_scope.find_by_permalink!(id.to_s)
89
- rescue ActiveRecord::RecordNotFound
90
- product_scope.find(id)
91
- end
92
- end
93
-
94
- def product_scope
95
- if current_api_user.has_spree_role?("admin")
96
- scope = Product
97
- unless params[:show_deleted]
98
- scope = scope.not_deleted
99
- end
100
- else
101
- scope = Product.active
102
- end
103
-
104
- scope.includes(:master)
105
- end
106
-
107
- end
108
- end
109
- end
110
- end
111
-
@@ -1,18 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class CountriesController < Spree::Api::V1::BaseController
5
- def index
6
- @countries = Country.
7
- ransack(params[:q]).result.
8
- includes(:states).order('name ASC').
9
- page(params[:page]).per(params[:per_page])
10
- end
11
-
12
- def show
13
- @country = Country.find(params[:id])
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,32 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class ImagesController < Spree::Api::V1::BaseController
5
- def show
6
- @image = Image.find(params[:id])
7
- end
8
-
9
- def create
10
- authorize! :create, Image
11
- @image = Image.create(params[:image])
12
- render :show, :status => 201
13
- end
14
-
15
- def update
16
- authorize! :update, Image
17
- @image = Image.find(params[:id])
18
- @image.update_attributes(params[:image])
19
- render :show, :status => 200
20
- end
21
-
22
- def destroy
23
- authorize! :delete, Image
24
- @image = Image.find(params[:id])
25
- @image.destroy
26
- render :text => nil, :status => 204
27
- end
28
-
29
- end
30
- end
31
- end
32
- end
@@ -1,50 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class InventoryUnitsController < Spree::Api::V1::BaseController
5
- before_filter :prepare_event, :only => :update
6
-
7
- def show
8
- @inventory_unit = inventory_unit
9
- end
10
-
11
- def update
12
- authorize! :update, Order
13
-
14
- inventory_unit.transaction do
15
- if inventory_unit.update_attributes(params[:inventory_unit])
16
- fire
17
- render :show, :status => 200
18
- else
19
- invalid_resource!(inventory_unit)
20
- end
21
- end
22
- end
23
-
24
- private
25
-
26
- def inventory_unit
27
- @inventory_unit ||= InventoryUnit.find(params[:id])
28
- end
29
-
30
- def prepare_event
31
- return unless @event = params[:fire]
32
-
33
- can_event = "can_#{@event}?"
34
-
35
- unless inventory_unit.respond_to?(can_event) &&
36
- inventory_unit.send(can_event)
37
- render :text => { :exception => "cannot transition to #{@event}" }.to_json,
38
- :status => 200
39
- false
40
- end
41
- end
42
-
43
- def fire
44
- inventory_unit.send("#{@event}!") if @event
45
- end
46
-
47
- end
48
- end
49
- end
50
- end
@@ -1,40 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class LineItemsController < Spree::Api::V1::BaseController
5
- def create
6
- authorize! :read, order
7
- @line_item = order.line_items.build(params[:line_item], :as => :api)
8
- if @line_item.save
9
- render :show, :status => 201
10
- else
11
- invalid_resource!(@line_item)
12
- end
13
- end
14
-
15
- def update
16
- authorize! :read, order
17
- @line_item = order.line_items.find(params[:id])
18
- if @line_item.update_attributes(params[:line_item])
19
- render :show
20
- else
21
- invalid_resource!(@line_item)
22
- end
23
- end
24
-
25
- def destroy
26
- authorize! :read, order
27
- @line_item = order.line_items.find(params[:id])
28
- @line_item.destroy
29
- render :text => nil, :status => 204
30
- end
31
-
32
- private
33
-
34
- def order
35
- @order ||= Order.find_by_number!(params[:order_id])
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,83 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class OrdersController < Spree::Api::V1::BaseController
5
- before_filter :authorize_read!, :except => [:index, :search, :create]
6
-
7
- def index
8
- # should probably look at turning this into a CanCan step
9
- raise CanCan::AccessDenied unless current_api_user.has_spree_role?("admin")
10
- @orders = Order.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
11
- end
12
-
13
- def show
14
- end
15
-
16
- def create
17
- @order = Order.build_from_api(current_api_user, nested_params)
18
- next!(:status => 201)
19
- end
20
-
21
- def update
22
- authorize! :update, Order
23
- if order.update_attributes(nested_params)
24
- order.update!
25
- render :show
26
- else
27
- invalid_resource!(order)
28
- end
29
- end
30
-
31
- def address
32
- order.build_ship_address(params[:shipping_address]) if params[:shipping_address]
33
- order.build_bill_address(params[:billing_address]) if params[:billing_address]
34
- next!
35
- end
36
-
37
- def delivery
38
- begin
39
- ShippingMethod.find(params[:shipping_method_id])
40
- rescue ActiveRecord::RecordNotFound
41
- render :invalid_shipping_method, :status => 422
42
- else
43
- order.update_attribute(:shipping_method_id, params[:shipping_method_id])
44
- next!
45
- end
46
- end
47
-
48
- def cancel
49
- order.cancel!
50
- render :show
51
- end
52
-
53
- def empty
54
- order.line_items.destroy_all
55
- order.update!
56
- render :text => nil, :status => 200
57
- end
58
-
59
- private
60
-
61
- def nested_params
62
- map_nested_attributes_keys Order, params[:order] || {}
63
- end
64
-
65
- def order
66
- @order ||= Order.find_by_number!(params[:id])
67
- end
68
-
69
- def next!(options={})
70
- if @order.valid? && @order.next
71
- render :show, :status => options[:status] || 200
72
- else
73
- render :could_not_transition, :status => 422
74
- end
75
- end
76
-
77
- def authorize_read!
78
- authorize! :read, order
79
- end
80
- end
81
- end
82
- end
83
- end
@@ -1,77 +0,0 @@
1
- module Spree
2
- module Api
3
- module V1
4
- class PaymentsController < Spree::Api::V1::BaseController
5
- before_filter :find_order
6
- before_filter :find_payment, :only => [:show, :authorize, :purchase, :capture, :void, :credit]
7
-
8
- def index
9
- @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
10
- end
11
-
12
- def new
13
- @payment_methods = Spree::PaymentMethod.where(:environment => Rails.env)
14
- end
15
-
16
- def create
17
- @payment = @order.payments.build(params[:payment])
18
- if @payment.save
19
- render :show, :status => 201
20
- else
21
- invalid_resource!(@payment)
22
- end
23
- end
24
-
25
- def show
26
- end
27
-
28
- def authorize
29
- perform_payment_action(:authorize)
30
- end
31
-
32
- def capture
33
- perform_payment_action(:capture)
34
- end
35
-
36
- def purchase
37
- perform_payment_action(:purchase)
38
- end
39
-
40
- def void
41
- perform_payment_action(:void_transaction)
42
- end
43
-
44
- def credit
45
- if params[:amount].to_f > @payment.credit_allowed
46
- render "spree/api/v1/payments/credit_over_limit", :status => 422
47
- else
48
- perform_payment_action(:credit, params[:amount])
49
- end
50
- end
51
-
52
- private
53
-
54
- def find_order
55
- @order = Order.find_by_number(params[:order_id])
56
- authorize! :read, @order
57
- end
58
-
59
- def find_payment
60
- @payment = @order.payments.find(params[:id])
61
- end
62
-
63
- def perform_payment_action(action, *args)
64
- authorize! action, Payment
65
-
66
- begin
67
- @payment.send("#{action}!", *args)
68
- render :show
69
- rescue Spree::Core::GatewayError => e
70
- @error = e.message
71
- render "spree/api/v1/errors/gateway_error", :status => 422
72
- end
73
- end
74
- end
75
- end
76
- end
77
- end