solidus_api 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of solidus_api might be problematic. Click here for more details.

Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/CHANGELOG.md +1 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE +27 -0
  6. data/Rakefile +16 -0
  7. data/app/controllers/spree/api/addresses_controller.rb +43 -0
  8. data/app/controllers/spree/api/base_controller.rb +189 -0
  9. data/app/controllers/spree/api/checkouts_controller.rb +133 -0
  10. data/app/controllers/spree/api/classifications_controller.rb +18 -0
  11. data/app/controllers/spree/api/config_controller.rb +6 -0
  12. data/app/controllers/spree/api/countries_controller.rb +23 -0
  13. data/app/controllers/spree/api/credit_cards_controller.rb +25 -0
  14. data/app/controllers/spree/api/images_controller.rb +47 -0
  15. data/app/controllers/spree/api/inventory_units_controller.rb +52 -0
  16. data/app/controllers/spree/api/line_items_controller.rb +74 -0
  17. data/app/controllers/spree/api/option_types_controller.rb +49 -0
  18. data/app/controllers/spree/api/option_values_controller.rb +58 -0
  19. data/app/controllers/spree/api/orders_controller.rb +155 -0
  20. data/app/controllers/spree/api/payments_controller.rb +81 -0
  21. data/app/controllers/spree/api/product_properties_controller.rb +72 -0
  22. data/app/controllers/spree/api/products_controller.rb +129 -0
  23. data/app/controllers/spree/api/promotions_controller.rb +26 -0
  24. data/app/controllers/spree/api/properties_controller.rb +71 -0
  25. data/app/controllers/spree/api/return_authorizations_controller.rb +71 -0
  26. data/app/controllers/spree/api/shipments_controller.rb +172 -0
  27. data/app/controllers/spree/api/states_controller.rb +35 -0
  28. data/app/controllers/spree/api/stock_items_controller.rb +84 -0
  29. data/app/controllers/spree/api/stock_locations_controller.rb +50 -0
  30. data/app/controllers/spree/api/stock_movements_controller.rb +42 -0
  31. data/app/controllers/spree/api/stock_transfers_controller.rb +19 -0
  32. data/app/controllers/spree/api/store_credit_events_controller.rb +9 -0
  33. data/app/controllers/spree/api/stores_controller.rb +55 -0
  34. data/app/controllers/spree/api/taxonomies_controller.rb +64 -0
  35. data/app/controllers/spree/api/taxons_controller.rb +93 -0
  36. data/app/controllers/spree/api/transfer_items_controller.rb +42 -0
  37. data/app/controllers/spree/api/users_controller.rb +56 -0
  38. data/app/controllers/spree/api/variants_controller.rb +75 -0
  39. data/app/controllers/spree/api/zones_controller.rb +50 -0
  40. data/app/helpers/spree/api/api_helpers.rb +190 -0
  41. data/app/models/spree/api_configuration.rb +5 -0
  42. data/app/models/spree/option_value_decorator.rb +9 -0
  43. data/app/views/spree/api/addresses/show.v1.rabl +10 -0
  44. data/app/views/spree/api/adjustments/show.v1.rabl +4 -0
  45. data/app/views/spree/api/config/money.v1.rabl +2 -0
  46. data/app/views/spree/api/config/show.v1.rabl +2 -0
  47. data/app/views/spree/api/countries/index.v1.rabl +7 -0
  48. data/app/views/spree/api/countries/show.v1.rabl +5 -0
  49. data/app/views/spree/api/credit_cards/index.v1.rabl +7 -0
  50. data/app/views/spree/api/credit_cards/show.v1.rabl +3 -0
  51. data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
  52. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
  53. data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
  54. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
  55. data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
  56. data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
  57. data/app/views/spree/api/errors/variant_not_in_stock_transfer.v1.rabl +2 -0
  58. data/app/views/spree/api/images/index.v1.rabl +4 -0
  59. data/app/views/spree/api/images/show.v1.rabl +6 -0
  60. data/app/views/spree/api/inventory_units/show.rabl +2 -0
  61. data/app/views/spree/api/line_items/new.v1.rabl +3 -0
  62. data/app/views/spree/api/line_items/show.v1.rabl +15 -0
  63. data/app/views/spree/api/option_types/index.v1.rabl +3 -0
  64. data/app/views/spree/api/option_types/show.v1.rabl +5 -0
  65. data/app/views/spree/api/option_values/index.v1.rabl +3 -0
  66. data/app/views/spree/api/option_values/show.v1.rabl +2 -0
  67. data/app/views/spree/api/orders/address.v1.rabl +0 -0
  68. data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
  69. data/app/views/spree/api/orders/cart.v1.rabl +0 -0
  70. data/app/views/spree/api/orders/complete.v1.rabl +0 -0
  71. data/app/views/spree/api/orders/could_not_apply_coupon.v1.rabl +2 -0
  72. data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
  73. data/app/views/spree/api/orders/expected_total_mismatch.v1.rabl +2 -0
  74. data/app/views/spree/api/orders/index.v1.rabl +7 -0
  75. data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
  76. data/app/views/spree/api/orders/mine.v1.rabl +9 -0
  77. data/app/views/spree/api/orders/order.v1.rabl +9 -0
  78. data/app/views/spree/api/orders/payment.v1.rabl +3 -0
  79. data/app/views/spree/api/orders/show.v1.rabl +52 -0
  80. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
  81. data/app/views/spree/api/payments/index.v1.rabl +7 -0
  82. data/app/views/spree/api/payments/new.v1.rabl +5 -0
  83. data/app/views/spree/api/payments/show.v1.rabl +2 -0
  84. data/app/views/spree/api/payments/update_forbidden.v1.rabl +2 -0
  85. data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
  86. data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
  87. data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
  88. data/app/views/spree/api/products/index.v1.rabl +9 -0
  89. data/app/views/spree/api/products/new.v1.rabl +3 -0
  90. data/app/views/spree/api/products/product.v1.rabl +1 -0
  91. data/app/views/spree/api/products/show.v1.rabl +31 -0
  92. data/app/views/spree/api/promotions/handler.v1.rabl +5 -0
  93. data/app/views/spree/api/promotions/show.v1.rabl +2 -0
  94. data/app/views/spree/api/properties/index.v1.rabl +7 -0
  95. data/app/views/spree/api/properties/new.v1.rabl +2 -0
  96. data/app/views/spree/api/properties/show.v1.rabl +2 -0
  97. data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
  98. data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
  99. data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
  100. data/app/views/spree/api/shared/stock_location_required.v1.rabl +2 -0
  101. data/app/views/spree/api/shipments/big.v1.rabl +48 -0
  102. data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
  103. data/app/views/spree/api/shipments/mine.v1.rabl +9 -0
  104. data/app/views/spree/api/shipments/show.v1.rabl +32 -0
  105. data/app/views/spree/api/shipments/small.v1.rabl +37 -0
  106. data/app/views/spree/api/shipping_rates/show.v1.rabl +2 -0
  107. data/app/views/spree/api/states/index.v1.rabl +14 -0
  108. data/app/views/spree/api/states/show.v1.rabl +2 -0
  109. data/app/views/spree/api/stock_items/index.v1.rabl +7 -0
  110. data/app/views/spree/api/stock_items/show.v1.rabl +5 -0
  111. data/app/views/spree/api/stock_locations/index.v1.rabl +7 -0
  112. data/app/views/spree/api/stock_locations/show.v1.rabl +8 -0
  113. data/app/views/spree/api/stock_movements/index.v1.rabl +7 -0
  114. data/app/views/spree/api/stock_movements/show.v1.rabl +5 -0
  115. data/app/views/spree/api/stock_transfers/receive.v1.rabl +5 -0
  116. data/app/views/spree/api/store_credit_events/mine.v1.rabl +10 -0
  117. data/app/views/spree/api/stores/index.v1.rabl +4 -0
  118. data/app/views/spree/api/stores/show.v1.rabl +2 -0
  119. data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
  120. data/app/views/spree/api/taxonomies/jstree.rabl +8 -0
  121. data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
  122. data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
  123. data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
  124. data/app/views/spree/api/taxons/index.v1.rabl +12 -0
  125. data/app/views/spree/api/taxons/jstree.rabl +8 -0
  126. data/app/views/spree/api/taxons/new.v1.rabl +3 -0
  127. data/app/views/spree/api/taxons/show.v1.rabl +6 -0
  128. data/app/views/spree/api/taxons/taxons.v1.rabl +5 -0
  129. data/app/views/spree/api/transfer_items/show.v1.rabl +6 -0
  130. data/app/views/spree/api/users/index.v1.rabl +7 -0
  131. data/app/views/spree/api/users/new.v1.rabl +3 -0
  132. data/app/views/spree/api/users/show.v1.rabl +10 -0
  133. data/app/views/spree/api/variants/big.v1.rabl +17 -0
  134. data/app/views/spree/api/variants/index.v1.rabl +9 -0
  135. data/app/views/spree/api/variants/new.v1.rabl +2 -0
  136. data/app/views/spree/api/variants/show.v1.rabl +3 -0
  137. data/app/views/spree/api/variants/small.v1.rabl +17 -0
  138. data/app/views/spree/api/zones/index.v1.rabl +7 -0
  139. data/app/views/spree/api/zones/show.v1.rabl +6 -0
  140. data/config/initializers/metal_load_paths.rb +1 -0
  141. data/config/locales/en.yml +29 -0
  142. data/config/routes.rb +139 -0
  143. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
  144. data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
  145. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
  146. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +7 -0
  147. data/lib/solidus_api.rb +1 -0
  148. data/lib/spree/api/engine.rb +38 -0
  149. data/lib/spree/api/responders/rabl_template.rb +31 -0
  150. data/lib/spree/api/responders.rb +11 -0
  151. data/lib/spree/api/testing_support/caching.rb +10 -0
  152. data/lib/spree/api/testing_support/helpers.rb +44 -0
  153. data/lib/spree/api/testing_support/setup.rb +16 -0
  154. data/lib/spree/api.rb +10 -0
  155. data/lib/spree_api.rb +3 -0
  156. data/script/rails +9 -0
  157. data/solidus_api.gemspec +21 -0
  158. data/spec/controllers/spree/api/addresses_controller_spec.rb +56 -0
  159. data/spec/controllers/spree/api/base_controller_spec.rb +164 -0
  160. data/spec/controllers/spree/api/checkouts_controller_spec.rb +386 -0
  161. data/spec/controllers/spree/api/classifications_controller_spec.rb +48 -0
  162. data/spec/controllers/spree/api/config_controller_spec.rb +23 -0
  163. data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
  164. data/spec/controllers/spree/api/credit_cards_controller_spec.rb +80 -0
  165. data/spec/controllers/spree/api/images_controller_spec.rb +93 -0
  166. data/spec/controllers/spree/api/inventory_units_controller_spec.rb +50 -0
  167. data/spec/controllers/spree/api/line_items_controller_spec.rb +186 -0
  168. data/spec/controllers/spree/api/option_types_controller_spec.rb +116 -0
  169. data/spec/controllers/spree/api/option_values_controller_spec.rb +135 -0
  170. data/spec/controllers/spree/api/orders_controller_spec.rb +759 -0
  171. data/spec/controllers/spree/api/payments_controller_spec.rb +254 -0
  172. data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
  173. data/spec/controllers/spree/api/products_controller_spec.rb +454 -0
  174. data/spec/controllers/spree/api/promotion_application_spec.rb +50 -0
  175. data/spec/controllers/spree/api/promotions_controller_spec.rb +64 -0
  176. data/spec/controllers/spree/api/properties_controller_spec.rb +102 -0
  177. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +173 -0
  178. data/spec/controllers/spree/api/shipments_controller_spec.rb +252 -0
  179. data/spec/controllers/spree/api/states_controller_spec.rb +82 -0
  180. data/spec/controllers/spree/api/stock_items_controller_spec.rb +307 -0
  181. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +172 -0
  182. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +84 -0
  183. data/spec/controllers/spree/api/stock_transfers_controller_spec.rb +83 -0
  184. data/spec/controllers/spree/api/store_credit_events_controller_spec.rb +68 -0
  185. data/spec/controllers/spree/api/stores_controller_spec.rb +133 -0
  186. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +114 -0
  187. data/spec/controllers/spree/api/taxons_controller_spec.rb +177 -0
  188. data/spec/controllers/spree/api/transfer_items_controller_spec.rb +152 -0
  189. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
  190. data/spec/controllers/spree/api/users_controller_spec.rb +153 -0
  191. data/spec/controllers/spree/api/variants_controller_spec.rb +235 -0
  192. data/spec/controllers/spree/api/zones_controller_spec.rb +115 -0
  193. data/spec/features/checkout_spec.rb +187 -0
  194. data/spec/fixtures/thinking-cat.jpg +0 -0
  195. data/spec/models/spree/legacy_user_spec.rb +45 -0
  196. data/spec/requests/rabl_cache_spec.rb +32 -0
  197. data/spec/shared_examples/protect_product_actions.rb +17 -0
  198. data/spec/spec_helper.rb +60 -0
  199. data/spec/support/controller_hacks.rb +38 -0
  200. data/spec/support/database_cleaner.rb +14 -0
  201. data/spec/support/have_attributes_matcher.rb +13 -0
  202. metadata +334 -0
@@ -0,0 +1,9 @@
1
+ object false
2
+ node(:count) { @variants.count }
3
+ node(:total_count) { @variants.total_count }
4
+ node(:current_page) { params[:page] ? params[:page].to_i : 1 }
5
+ node(:pages) { @variants.num_pages }
6
+
7
+ child(@variants => :variants) do
8
+ extends "spree/api/variants/big"
9
+ end
@@ -0,0 +1,2 @@
1
+ node(:attributes) { [*variant_attributes] }
2
+ node(:required_attributes) { [] }
@@ -0,0 +1,3 @@
1
+ object @variant
2
+ cache [I18n.locale, @current_user_roles.include?('admin'), 'show', root_object]
3
+ extends "spree/api/variants/big"
@@ -0,0 +1,17 @@
1
+ cache [I18n.locale, @current_user_roles.include?('admin'), 'small_variant', root_object]
2
+
3
+ attributes *variant_attributes
4
+
5
+ node(:display_price) { |p| p.display_price.to_s }
6
+ node(:options_text) { |v| v.options_text }
7
+ node(:track_inventory) { |v| v.should_track_inventory? }
8
+ node(:in_stock) { |v| v.in_stock? }
9
+ node(:is_backorderable) { |v| v.is_backorderable? }
10
+ node(:total_on_hand) { |v| v.total_on_hand }
11
+ node(:is_destroyed) { |v| v.destroyed? }
12
+
13
+ child :option_values => :option_values do
14
+ attributes *option_value_attributes
15
+ end
16
+
17
+ child(:images => :images) { extends "spree/api/images/show" }
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@zones => :zones) do
3
+ extends 'spree/api/zones/show'
4
+ end
5
+ node(:count) { @zones.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @zones.num_pages }
@@ -0,0 +1,6 @@
1
+ object @zone
2
+ attributes :id, :name, :description
3
+
4
+ child :zone_members => :zone_members do
5
+ attributes :id, :name, :zoneable_type, :zoneable_id
6
+ end
@@ -0,0 +1 @@
1
+ Spree::Api::BaseController.append_view_path(ApplicationController.view_paths)
@@ -0,0 +1,29 @@
1
+ en:
2
+ spree:
3
+ api:
4
+ must_specify_api_key: "You must specify an API key."
5
+ invalid_api_key: "Invalid API key (%{key}) specified."
6
+ unauthorized: "You are not authorized to perform that action."
7
+ invalid_resource: "Invalid resource. Please fix errors and try again."
8
+ resource_not_found: "The resource you were looking for could not be found."
9
+ gateway_error: "There was a problem with the payment gateway: %{text}"
10
+ access: "API Access"
11
+ key: "Key"
12
+ clear_key: "Clear key"
13
+ regenerate_key: "Regenerate Key"
14
+ no_key: "No key"
15
+ generate_key: "Generate API key"
16
+ key_generated: "Key generated"
17
+ key_cleared: "Key cleared"
18
+ order:
19
+ could_not_transition: "The order could not be transitioned. Please fix the errors and try again."
20
+ invalid_shipping_method: "Invalid shipping method specified."
21
+ expected_total_mismatch: "Expected total does not match actual total."
22
+ quantity_is_not_available: "Quantity is not available for items in your order"
23
+ payment:
24
+ credit_over_limit: "This payment can only be credited up to %{limit}. Please specify an amount less than or equal to this number."
25
+ update_forbidden: "This payment cannot be updated because it is %{state}."
26
+ shipment:
27
+ cannot_ready: "Cannot ready shipment."
28
+ stock_location_required: "A stock_location_id parameter must be provided in order to retrieve stock movements."
29
+ invalid_taxonomy_id: "Invalid taxonomy id."
data/config/routes.rb ADDED
@@ -0,0 +1,139 @@
1
+ Spree::Core::Engine.add_routes do
2
+ namespace :admin do
3
+ resources :users do
4
+ member do
5
+ put :generate_api_key
6
+ put :clear_api_key
7
+ end
8
+ end
9
+ end
10
+
11
+ namespace :api, defaults: { format: 'json' } do
12
+ resources :promotions, only: [:show]
13
+
14
+ resources :products do
15
+ resources :images
16
+ resources :variants
17
+ resources :product_properties
18
+ end
19
+
20
+ concern :order_routes do
21
+ member do
22
+ put :cancel
23
+ put :empty
24
+ put :apply_coupon_code
25
+ end
26
+
27
+ resources :line_items
28
+ resources :payments do
29
+ member do
30
+ put :authorize
31
+ put :capture
32
+ put :purchase
33
+ put :void
34
+ put :credit
35
+ end
36
+ end
37
+
38
+ resources :addresses, only: [:show, :update]
39
+
40
+ resources :return_authorizations do
41
+ member do
42
+ put :add
43
+ put :cancel
44
+ put :receive
45
+ end
46
+ end
47
+ end
48
+
49
+ resources :checkouts, only: [:update], concerns: :order_routes do
50
+ member do
51
+ put :next
52
+ put :advance
53
+ put :complete
54
+ end
55
+ end
56
+
57
+ resources :variants do
58
+ resources :images
59
+ end
60
+
61
+ resources :option_types do
62
+ resources :option_values
63
+ end
64
+
65
+ resources :option_values, only: :index
66
+ get '/orders/mine', to: 'orders#mine', as: 'my_orders'
67
+ get "/orders/current", to: "orders#current", as: "current_order"
68
+
69
+ resources :orders, concerns: :order_routes
70
+
71
+ resources :zones
72
+ resources :countries, only: [:index, :show] do
73
+ resources :states, only: [:index, :show]
74
+ end
75
+
76
+ resources :shipments, only: [:create, :update] do
77
+ collection do
78
+ post 'transfer_to_location'
79
+ post 'transfer_to_shipment'
80
+ get :mine
81
+ end
82
+
83
+ member do
84
+ put :ready
85
+ put :ship
86
+ put :add
87
+ put :remove
88
+ end
89
+ end
90
+ resources :states, only: [:index, :show]
91
+
92
+ resources :taxonomies do
93
+ member do
94
+ get :jstree
95
+ end
96
+ resources :taxons do
97
+ member do
98
+ get :jstree
99
+ end
100
+ end
101
+ end
102
+
103
+ resources :taxons, only: [:index]
104
+
105
+ resources :inventory_units, only: [:show, :update]
106
+
107
+ resources :users do
108
+ resources :credit_cards, only: [:index]
109
+ end
110
+
111
+ resources :properties
112
+ resources :stock_locations do
113
+ resources :stock_movements
114
+ resources :stock_items
115
+ end
116
+
117
+ resources :stock_items, only: [:index, :update, :destroy]
118
+
119
+ resources :stock_transfers, only: [] do
120
+ member do
121
+ post :receive
122
+ end
123
+ resources :transfer_items, only: [:create, :update, :destroy]
124
+ end
125
+
126
+ resources :stores
127
+
128
+ resources :store_credit_events, only: [] do
129
+ collection do
130
+ get :mine
131
+ end
132
+ end
133
+
134
+ get '/config/money', to: 'config#money'
135
+ get '/config', to: 'config#show'
136
+ put '/classifications', to: 'classifications#update', as: :classifications
137
+ get '/taxons/products', to: 'taxons#products', as: :taxon_products
138
+ end
139
+ end
@@ -0,0 +1,7 @@
1
+ class AddApiKeyToSpreeUsers < ActiveRecord::Migration
2
+ def change
3
+ unless defined?(User)
4
+ add_column :spree_users, :api_key, :string, :limit => 40
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class ResizeApiKeyField < ActiveRecord::Migration
2
+ def change
3
+ unless defined?(User)
4
+ change_column :spree_users, :api_key, :string, :limit => 48
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class RenameApiKeyToSpreeApiKey < ActiveRecord::Migration
2
+ def change
3
+ unless defined?(User)
4
+ rename_column :spree_users, :api_key, :spree_api_key
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class AddIndexToUserSpreeApiKey < ActiveRecord::Migration
2
+ def change
3
+ unless defined?(User)
4
+ add_index :spree_users, :spree_api_key
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ require 'spree_api'
@@ -0,0 +1,38 @@
1
+ require 'rails/engine'
2
+
3
+ module Spree
4
+ module Api
5
+ class Engine < Rails::Engine
6
+ isolate_namespace Spree
7
+ engine_name 'spree_api'
8
+
9
+ Rabl.configure do |config|
10
+ config.include_json_root = false
11
+ config.include_child_root = false
12
+
13
+ # Motivation here it make it call as_json when rendering timestamps
14
+ # and therefore display miliseconds. Otherwise it would fall to
15
+ # JSON.dump which doesn't display the miliseconds
16
+ config.json_engine = ActiveSupport::JSON
17
+ end
18
+
19
+ config.view_versions = [1]
20
+ config.view_version_extraction_strategy = :http_header
21
+
22
+ initializer "spree.api.environment", :before => :load_config_initializers do |app|
23
+ Spree::Api::Config = Spree::ApiConfiguration.new
24
+ end
25
+
26
+ def self.activate
27
+ Dir.glob(File.join(File.dirname(__FILE__), "../../../app/**/*_decorator*.rb")) do |c|
28
+ Rails.configuration.cache_classes ? require(c) : load(c)
29
+ end
30
+ end
31
+ config.to_prepare &method(:activate).to_proc
32
+
33
+ def self.root
34
+ @root ||= Pathname.new(File.expand_path('../../../../', __FILE__))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ module Spree
2
+ module Api
3
+ module Responders
4
+ module RablTemplate
5
+ def to_format
6
+ if template
7
+ render template, :status => options[:status] || 200
8
+ else
9
+ super
10
+ end
11
+
12
+ rescue ActionView::MissingTemplate
13
+ api_behavior
14
+ end
15
+
16
+ def template
17
+ request.headers['X-Spree-Template'] || controller.params[:template] || options[:default_template]
18
+ end
19
+
20
+ def api_behavior
21
+ if controller.params[:action] == "destroy"
22
+ # Render a blank template
23
+ super
24
+ else
25
+ # Do nothing and fallback to the default template
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ require 'spree/api/responders/rabl_template'
2
+
3
+ module Spree
4
+ module Api
5
+ module Responders
6
+ class AppResponder < ActionController::Responder
7
+ include RablTemplate
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ RSpec.configure do |config|
2
+ config.before(:each, :caching => true) do
3
+ ActionController::Base.perform_caching = true
4
+ end
5
+
6
+ config.after(:each, :caching => true) do
7
+ ActionController::Base.perform_caching = false
8
+ Rails.cache.clear
9
+ end
10
+ end
@@ -0,0 +1,44 @@
1
+ module Spree
2
+ module Api
3
+ module TestingSupport
4
+ module Helpers
5
+ def json_response
6
+ case body = JSON.parse(response.body)
7
+ when Hash
8
+ body.with_indifferent_access
9
+ when Array
10
+ body
11
+ end
12
+ end
13
+
14
+ def assert_not_found!
15
+ expect(json_response).to eq({ "error" => "The resource you were looking for could not be found." })
16
+ expect(response.status).to eq 404
17
+ end
18
+
19
+ def assert_unauthorized!
20
+ expect(json_response).to eq({ "error" => "You are not authorized to perform that action." })
21
+ expect(response.status).to eq 401
22
+ end
23
+
24
+ def stub_authentication!
25
+ allow(Spree.user_class).to receive(:find_by).with(hash_including(:spree_api_key)) { current_api_user }
26
+ end
27
+
28
+ # This method can be overriden (with a let block) inside a context
29
+ # For instance, if you wanted to have an admin user instead.
30
+ def current_api_user
31
+ @current_api_user ||= stub_model(Spree::LegacyUser, email: "spree@example.com")
32
+ end
33
+
34
+ def image(filename)
35
+ File.open(Spree::Api::Engine.root + "spec/fixtures" + filename)
36
+ end
37
+
38
+ def upload_image(filename)
39
+ fixture_file_upload(image(filename).path, 'image/jpg')
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ module Spree
2
+ module Api
3
+ module TestingSupport
4
+ module Setup
5
+ def sign_in_as_admin!
6
+ let!(:current_api_user) do
7
+ user = stub_model(Spree::LegacyUser)
8
+ allow(user).to receive_message_chain(:spree_roles, :pluck).and_return(["admin"])
9
+ allow(user).to receive(:has_spree_role?).with("admin").and_return(true)
10
+ user
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/spree/api.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'spree/core'
2
+
3
+ require 'rabl'
4
+
5
+ module Spree
6
+ module Api
7
+ end
8
+ end
9
+
10
+ require 'spree/api/engine'
data/lib/spree_api.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'spree/api'
2
+ require 'spree/api/responders'
3
+ require 'versioncake'
data/script/rails ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/spree/api/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'
9
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ version = File.read(File.expand_path("../../SOLIDUS_VERSION", __FILE__)).strip
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.author = 'Solidus Team'
6
+ gem.email = 'contact@solidus.io'
7
+ gem.description = %q{Spree's API}
8
+ gem.summary = %q{Spree's API}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "solidus_api"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = version
17
+
18
+ gem.add_dependency 'solidus_core', version
19
+ gem.add_dependency 'rabl', ['>= 0.9.4.pre1', '< 0.12.0']
20
+ gem.add_dependency 'versioncake', '~> 2.3.1'
21
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::AddressesController, :type => :controller do
5
+ render_views
6
+
7
+ before do
8
+ stub_authentication!
9
+ @address = create(:address)
10
+ @order = create(:order, :bill_address => @address)
11
+ end
12
+
13
+ context "with their own address" do
14
+ before do
15
+ allow_any_instance_of(Order).to receive_messages :user => current_api_user
16
+ end
17
+
18
+ it "gets an address" do
19
+ api_get :show, :id => @address.id, :order_id => @order.number
20
+ expect(json_response['address1']).to eq @address.address1
21
+ end
22
+
23
+ it "updates an address" do
24
+ api_put :update, :id => @address.id, :order_id => @order.number,
25
+ :address => { :address1 => "123 Test Lane" }
26
+ expect(json_response['address1']).to eq '123 Test Lane'
27
+ end
28
+
29
+ it "receives the errors object if address is invalid" do
30
+ api_put :update, :id => @address.id, :order_id => @order.number,
31
+ :address => { :address1 => "" }
32
+
33
+ expect(json_response['error']).not_to be_nil
34
+ expect(json_response['errors']).not_to be_nil
35
+ expect(json_response['errors']['address1'].first).to eq "can't be blank"
36
+ end
37
+ end
38
+
39
+ context "on an address that does not belong to this order" do
40
+ before do
41
+ @order.bill_address_id = nil
42
+ @order.ship_address = nil
43
+ end
44
+
45
+ it "cannot retrieve address information" do
46
+ api_get :show, :id => @address.id, :order_id => @order.number
47
+ assert_unauthorized!
48
+ end
49
+
50
+ it "cannot update address information" do
51
+ api_get :update, :id => @address.id, :order_id => @order.number
52
+ assert_unauthorized!
53
+ end
54
+ end
55
+ end
56
+ end