dup_spree_api 1.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +22 -0
  5. data/Rakefile +30 -0
  6. data/app/controllers/spree/api/addresses_controller.rb +20 -0
  7. data/app/controllers/spree/api/base_controller.rb +114 -0
  8. data/app/controllers/spree/api/countries_controller.rb +18 -0
  9. data/app/controllers/spree/api/images_controller.rb +32 -0
  10. data/app/controllers/spree/api/line_items_controller.rb +40 -0
  11. data/app/controllers/spree/api/orders_controller.rb +85 -0
  12. data/app/controllers/spree/api/payments_controller.rb +80 -0
  13. data/app/controllers/spree/api/product_properties_controller.rb +65 -0
  14. data/app/controllers/spree/api/products_controller.rb +49 -0
  15. data/app/controllers/spree/api/return_authorizations_controller.rb +54 -0
  16. data/app/controllers/spree/api/shipments_controller.rb +43 -0
  17. data/app/controllers/spree/api/taxonomies_controller.rb +50 -0
  18. data/app/controllers/spree/api/taxons_controller.rb +53 -0
  19. data/app/controllers/spree/api/variants_controller.rb +75 -0
  20. data/app/controllers/spree/api/zones_controller.rb +46 -0
  21. data/app/helpers/spree/api/api_helpers.rb +76 -0
  22. data/app/models/spree/api_configuration.rb +5 -0
  23. data/app/models/spree/line_item_decorator.rb +3 -0
  24. data/app/models/spree/option_value_decorator.rb +5 -0
  25. data/app/models/spree/order_decorator.rb +13 -0
  26. data/app/models/spree/user_decorator.rb +13 -0
  27. data/app/overrides/api_admin_user_edit_form.rb +6 -0
  28. data/app/views/spree/admin/users/_api_fields.html.erb +32 -0
  29. data/app/views/spree/api/addresses/show.v1.rabl +11 -0
  30. data/app/views/spree/api/countries/index.v1.rabl +7 -0
  31. data/app/views/spree/api/countries/show.v1.rabl +5 -0
  32. data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
  33. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
  34. data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
  35. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
  36. data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
  37. data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
  38. data/app/views/spree/api/images/show.v1.rabl +3 -0
  39. data/app/views/spree/api/line_items/new.v1.rabl +3 -0
  40. data/app/views/spree/api/line_items/show.v1.rabl +5 -0
  41. data/app/views/spree/api/orders/address.v1.rabl +0 -0
  42. data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
  43. data/app/views/spree/api/orders/cart.v1.rabl +0 -0
  44. data/app/views/spree/api/orders/complete.v1.rabl +0 -0
  45. data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
  46. data/app/views/spree/api/orders/delivery.v1.rabl +3 -0
  47. data/app/views/spree/api/orders/index.v1.rabl +7 -0
  48. data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
  49. data/app/views/spree/api/orders/payment.v1.rabl +4 -0
  50. data/app/views/spree/api/orders/show.v1.rabl +29 -0
  51. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
  52. data/app/views/spree/api/payments/index.v1.rabl +7 -0
  53. data/app/views/spree/api/payments/new.v1.rabl +6 -0
  54. data/app/views/spree/api/payments/show.v1.rabl +2 -0
  55. data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
  56. data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
  57. data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
  58. data/app/views/spree/api/products/index.v1.rabl +8 -0
  59. data/app/views/spree/api/products/new.v1.rabl +3 -0
  60. data/app/views/spree/api/products/product.v1.rabl +1 -0
  61. data/app/views/spree/api/products/show.v1.rabl +25 -0
  62. data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
  63. data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
  64. data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
  65. data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
  66. data/app/views/spree/api/shipments/show.v1.rabl +7 -0
  67. data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
  68. data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
  69. data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
  70. data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
  71. data/app/views/spree/api/taxons/index.v1.rabl +4 -0
  72. data/app/views/spree/api/taxons/new.v1.rabl +3 -0
  73. data/app/views/spree/api/taxons/show.v1.rabl +8 -0
  74. data/app/views/spree/api/taxons/taxons.v1.rabl +7 -0
  75. data/app/views/spree/api/variants/index.v1.rabl +10 -0
  76. data/app/views/spree/api/variants/new.v1.rabl +2 -0
  77. data/app/views/spree/api/variants/show.v1.rabl +3 -0
  78. data/app/views/spree/api/variants/variant.v1.rabl +1 -0
  79. data/app/views/spree/api/zones/index.v1.rabl +7 -0
  80. data/app/views/spree/api/zones/show.v1.rabl +6 -0
  81. data/config/initializers/metal_load_paths.rb +1 -0
  82. data/config/locales/en.yml +23 -0
  83. data/config/routes.rb +56 -0
  84. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
  85. data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
  86. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
  87. data/lib/spree/api.rb +12 -0
  88. data/lib/spree/api/controller_setup.rb +27 -0
  89. data/lib/spree/api/engine.rb +35 -0
  90. data/lib/spree/api/responders.rb +11 -0
  91. data/lib/spree/api/responders/rabl_template.rb +22 -0
  92. data/lib/spree/api/testing_support/helpers.rb +35 -0
  93. data/lib/spree/api/testing_support/setup.rb +28 -0
  94. data/lib/spree/api/version.rb +5 -0
  95. data/lib/spree_api.rb +3 -0
  96. data/script/rails +9 -0
  97. data/spec/controllers/spree/api/addresses_controller_spec.rb +45 -0
  98. data/spec/controllers/spree/api/base_controller_spec.rb +42 -0
  99. data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
  100. data/spec/controllers/spree/api/images_controller_spec.rb +66 -0
  101. data/spec/controllers/spree/api/line_items_controller_spec.rb +77 -0
  102. data/spec/controllers/spree/api/orders_controller_spec.rb +255 -0
  103. data/spec/controllers/spree/api/payments_controller_spec.rb +203 -0
  104. data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
  105. data/spec/controllers/spree/api/products_controller_spec.rb +211 -0
  106. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +155 -0
  107. data/spec/controllers/spree/api/shipments_controller_spec.rb +59 -0
  108. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +107 -0
  109. data/spec/controllers/spree/api/taxons_controller_spec.rb +87 -0
  110. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
  111. data/spec/controllers/spree/api/variants_controller_spec.rb +155 -0
  112. data/spec/controllers/spree/api/zones_controller_spec.rb +111 -0
  113. data/spec/fixtures/thinking-cat.jpg +0 -0
  114. data/spec/models/spree/legacy_user_spec.rb +19 -0
  115. data/spec/models/spree/order_spec.rb +18 -0
  116. data/spec/shared_examples/protect_product_actions.rb +17 -0
  117. data/spec/spec_helper.rb +27 -0
  118. data/spec/support/controller_hacks.rb +27 -0
  119. data/spec/support/database_cleaner.rb +14 -0
  120. data/spec/support/have_attributes_matcher.rb +13 -0
  121. data/spree_api.gemspec +23 -0
  122. metadata +254 -0
@@ -0,0 +1,5 @@
1
+ module Spree
2
+ class ApiConfiguration < Preferences::Configuration
3
+ preference :requires_authentication, :boolean, :default => true
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ Spree::LineItem.class_eval do
2
+ attr_accessible :quantity, :variant_id, :as => :api
3
+ end
@@ -0,0 +1,5 @@
1
+ Spree::OptionValue.class_eval do
2
+ def option_type_name
3
+ option_type.name
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ Spree::Order.class_eval do
2
+ def self.build_from_api(user, params)
3
+ order = create
4
+ params[:line_items_attributes] ||= []
5
+ params[:line_items_attributes].each do |line_item|
6
+ order.add_variant(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
7
+ end
8
+
9
+ order.user = user
10
+ order.email = user.email
11
+ order
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ if Spree.user_class
2
+ Spree.user_class.class_eval do
3
+ def generate_spree_api_key!
4
+ self.spree_api_key = SecureRandom.hex(24)
5
+ save!
6
+ end
7
+
8
+ def clear_spree_api_key!
9
+ self.spree_api_key = nil
10
+ save!
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ Deface::Override.new(:virtual_path => "spree/admin/users/edit",
2
+ :name => "api_admin_user_edit_form",
3
+ :insert_after => "[data-hook='admin_user_edit_general_settings']",
4
+ :partial => "spree/admin/users/api_fields",
5
+ :disabled => false)
6
+
@@ -0,0 +1,32 @@
1
+ <fieldset data-hook="admin_user_api_key" class="omega six columns">
2
+ <legend><%= t('access', :scope => 'spree.api') %></legend>
3
+
4
+ <% if @user.spree_api_key.present? %>
5
+ <div class="field">
6
+ <%= label_tag t('key', :scope => 'spree.api') %>:
7
+ <%= @user.spree_api_key %>
8
+ </div>
9
+ <div class="filter-actions actions">
10
+ <%= form_tag spree.clear_api_key_admin_user_path(@user), :method => :put do %>
11
+ <%= button t('clear_key', :scope => 'spree.api'), 'icon-trash' %>
12
+ <% end %>
13
+
14
+ <span class="or"><%= t(:or)%></span>
15
+
16
+ <%= form_tag spree.generate_api_key_admin_user_path(@user), :method => :put do %>
17
+ <%= button t('regenerate_key', :scope => 'spree.api'), 'icon-refresh' %>
18
+ <% end %>
19
+ </div>
20
+
21
+ <% else %>
22
+
23
+ <div class="no-objects-found"><%= t('no_key', :scope => 'spree.api') %></div>
24
+
25
+ <div class="filter-actions actions">
26
+ <%= form_tag spree.generate_api_key_admin_user_path(@user), :method => :put do %>
27
+ <%= button t('generate_key', :scope => 'spree.api'), 'icon-key' %>
28
+ <% end %>
29
+ </div>
30
+ <% end %>
31
+
32
+ </fieldset>
@@ -0,0 +1,11 @@
1
+ object @address
2
+ attributes :id, :firstname, :lastname, :address1, :address2,
3
+ :city, :zipcode, :phone,
4
+ :company, :alternative_phone, :country_id, :state_id,
5
+ :state_name
6
+ child(:country) do |address|
7
+ attributes :id, :iso_name, :iso, :iso3, :name, :numcode
8
+ end
9
+ child(:state) do |address|
10
+ attributes :abbr, :country_id, :id, :name
11
+ end
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@countries => :countries) do
3
+ attributes *country_attributes
4
+ end
5
+ node(:count) { @countries.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @countries.num_pages }
@@ -0,0 +1,5 @@
1
+ object @country
2
+ attributes *country_attributes
3
+ child :states => :states do
4
+ attributes :id, :name, :abbr, :country_id
5
+ end
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:gateway_error, :scope => "spree.api", :text => @error) }
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:invalid_api_key, :key => api_key, :scope => "spree.api") }
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:error) { I18n.t(:invalid_resource, :scope => "spree.api") }
3
+ node(:errors) { @resource.errors }
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:must_specify_api_key, :scope => "spree.api") }
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:resource_not_found, :scope => "spree.api") }
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:unauthorized, :scope => "spree.api") }
@@ -0,0 +1,3 @@
1
+ object @image
2
+ attributes *image_attributes
3
+ attributes :viewable_type, :viewable_id
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*line_item_attributes] - [:id] }
3
+ node(:required_attributes) { [:variant_id, :quantity] }
@@ -0,0 +1,5 @@
1
+ object @line_item
2
+ attributes *line_item_attributes
3
+ child :variant do
4
+ extends "spree/api/variants/variant"
5
+ end
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:error) { I18n.t(:could_not_transition, :scope => "spree.api.order") }
3
+ node(:errors) { @order.errors }
@@ -0,0 +1,3 @@
1
+ child(:rate_hash => :shipping_methods) do
2
+ attributes :id, :name, :cost
3
+ end
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@orders => :orders) do
3
+ attributes *order_attributes
4
+ end
5
+ node(:count) { @orders.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @orders.num_pages }
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:errors) { [I18n.t(:invalid_shipping_method, :scope => "spree.api.order")] }
@@ -0,0 +1,4 @@
1
+ attributes :id, :amount, :payment_method_id
2
+ child :payment_method => :payment_method do
3
+ attributes :id, :name, :environment
4
+ end
@@ -0,0 +1,29 @@
1
+ object @order
2
+ attributes *order_attributes
3
+
4
+ if lookup_context.find_all("spree/api/orders/#{@order.state}").present?
5
+ extends "spree/api/orders/#{@order.state}"
6
+ end
7
+
8
+ child :billing_address => :bill_address do
9
+ extends "spree/api/addresses/show"
10
+ end
11
+
12
+ child :shipping_address => :ship_address do
13
+ extends "spree/api/addresses/show"
14
+ end
15
+
16
+ child :line_items => :line_items do
17
+ extends "spree/api/line_items/show"
18
+ end
19
+
20
+ child :payments => :payments do
21
+ attributes :id, :amount, :state, :payment_method_id
22
+ child :payment_method => :payment_method do
23
+ attributes :id, :name, :environment
24
+ end
25
+ end
26
+
27
+ child :shipments => :shipments do
28
+ extends "spree/api/shipments/show"
29
+ end
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:credit_over_limit, :limit => @payment.credit_allowed, :scope => 'spree.api') }
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@payments => :payments) do
3
+ attributes *payment_attributes
4
+ end
5
+ node(:count) { @payments.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @payments.num_pages }
@@ -0,0 +1,6 @@
1
+ object false
2
+ node(:attributes) { [*payment_attributes] }
3
+ child @payment_methods => :payment_methods do
4
+ attributes *payment_method_attributes
5
+ end
6
+
@@ -0,0 +1,2 @@
1
+ object @payment
2
+ attributes *payment_attributes
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@product_properties => :product_properties) do
3
+ attributes *product_property_attributes
4
+ end
5
+ node(:count) { @product_properties.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @product_properties.num_pages }
@@ -0,0 +1,2 @@
1
+ node(:attributes) { [*product_property_attributes] }
2
+ node(:required_attributes) { [] }
@@ -0,0 +1,2 @@
1
+ object @product_property
2
+ attributes *product_property_attributes
@@ -0,0 +1,8 @@
1
+ object false
2
+ node(:count) { @products.count }
3
+ node(:total_count) { @products.total_count }
4
+ node(:current_page) { params[:page] ? params[:page].to_i : 1 }
5
+ node(:pages) { @products.num_pages }
6
+ child(@products) do
7
+ extends "spree/api/products/show"
8
+ end
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*product_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::Product) }
@@ -0,0 +1 @@
1
+ attributes *product_attributes
@@ -0,0 +1,25 @@
1
+ object @product
2
+ attributes *product_attributes
3
+ child :variants_including_master => :variants do
4
+ attributes *variant_attributes
5
+
6
+ child :option_values => :option_values do
7
+ attributes *option_value_attributes
8
+ end
9
+ end
10
+
11
+ child :images => :images do
12
+ extends "spree/api/images/show"
13
+ end
14
+
15
+ child :option_types => :option_types do
16
+ attributes *option_type_attributes
17
+
18
+ child :option_values => :option_values do
19
+ attributes *option_value_attributes
20
+ end
21
+ end
22
+
23
+ child :product_properties => :product_properties do
24
+ attributes *product_property_attributes
25
+ end
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@return_authorizations => :return_authorizations) do
3
+ attributes *return_authorization_attributes
4
+ end
5
+ node(:count) { @return_authorizations.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @return_authorizations.num_pages }
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*return_authorization_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::ReturnAuthorization) }
@@ -0,0 +1,2 @@
1
+ object @return_authorization
2
+ attributes *return_authorization_attributes
@@ -0,0 +1,2 @@
1
+ object false
2
+ node(:error) { I18n.t(:cannot_ready, :scope => "spree.api.shipment") }
@@ -0,0 +1,7 @@
1
+ object @shipment
2
+ attributes *shipment_attributes
3
+ node(:order_id) { |shipment| shipment.order.number }
4
+ child :shipping_method => :shipping_method do
5
+ attributes :name, :zone_id, :shipping_category_id
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@taxonomies => :taxonomies) do
3
+ extends "spree/api/taxonomies/show"
4
+ end
5
+ node(:count) { @taxonomies.count }
6
+ node(:current_page) { params[:page] || 1 }
7
+ node(:pages) { @taxonomies.num_pages }
@@ -0,0 +1,11 @@
1
+ attributes *taxonomy_attributes
2
+
3
+ child :root => :root do
4
+ attributes *taxon_attributes
5
+
6
+ child :children => :taxons do
7
+ attributes *taxon_attributes
8
+
9
+ extends "spree/api/taxons/taxons"
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*taxonomy_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::Taxonomy) }
@@ -0,0 +1,15 @@
1
+ object @taxonomy
2
+
3
+ if set = params[:set]
4
+ extends "spree/api/taxonomies/#{set}"
5
+ else
6
+ attributes *taxonomy_attributes
7
+
8
+ child :root => :root do
9
+ attributes *taxon_attributes
10
+
11
+ child :children => :taxons do
12
+ attributes *taxon_attributes
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ collection @taxons
2
+ attributes *taxon_attributes
3
+
4
+ extends "spree/api/taxons/taxons"
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*taxon_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::Taxon) }
@@ -0,0 +1,8 @@
1
+ object @taxon
2
+ attributes *taxon_attributes
3
+
4
+ node do |t|
5
+ child t.children => :taxons do
6
+ attributes *taxon_attributes
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ node do |t|
2
+ child t.children => :taxons do
3
+ attributes *taxon_attributes
4
+
5
+ extends "spree/api/taxons/taxons"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
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
+ attributes *variant_attributes
9
+ child(:option_values => :option_values) { attributes *option_value_attributes }
10
+ end
@@ -0,0 +1,2 @@
1
+ node(:attributes) { [*variant_attributes] }
2
+ node(:required_attributes) { [] }
@@ -0,0 +1,3 @@
1
+ object @variant
2
+ extends "spree/api/variants/variant"
3
+ child(:option_values => :option_values) { attributes *option_value_attributes }
@@ -0,0 +1 @@
1
+ attributes *variant_attributes
@@ -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 }