My-Commerce_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/LICENSE +26 -0
  2. data/README.md +16 -0
  3. data/app/controllers/admin/users_controller_decorator.rb +19 -0
  4. data/app/controllers/api/adjustments_controller.rb +13 -0
  5. data/app/controllers/api/base_controller.rb +183 -0
  6. data/app/controllers/api/checkout1_controller.rb +89 -0
  7. data/app/controllers/api/countries_controller.rb +3 -0
  8. data/app/controllers/api/images_controller.rb +15 -0
  9. data/app/controllers/api/inventory_units_controller.rb +19 -0
  10. data/app/controllers/api/line_items_controller.rb +37 -0
  11. data/app/controllers/api/mail_methods_controller.rb +10 -0
  12. data/app/controllers/api/option_types_controller.rb +24 -0
  13. data/app/controllers/api/orders_controller.rb +51 -0
  14. data/app/controllers/api/overview_controller.rb +132 -0
  15. data/app/controllers/api/payment_methods_controller.rb +24 -0
  16. data/app/controllers/api/payments_controller.rb +3 -0
  17. data/app/controllers/api/product_groups_controller.rb +14 -0
  18. data/app/controllers/api/products_controller.rb +14 -0
  19. data/app/controllers/api/promotions_controller.rb +14 -0
  20. data/app/controllers/api/properties_controller.rb +26 -0
  21. data/app/controllers/api/prototypes_controller.rb +24 -0
  22. data/app/controllers/api/reports_controller.rb +8 -0
  23. data/app/controllers/api/shipments_controller.rb +37 -0
  24. data/app/controllers/api/shipping_categories_controller.rb +13 -0
  25. data/app/controllers/api/shipping_methods_controller.rb +24 -0
  26. data/app/controllers/api/states_controller.rb +8 -0
  27. data/app/controllers/api/tax_categories_controller.rb +24 -0
  28. data/app/controllers/api/tax_rates_controller.rb +24 -0
  29. data/app/controllers/api/taxonomies_controller.rb +12 -0
  30. data/app/controllers/api/taxons_controller.rb +17 -0
  31. data/app/controllers/api/users_controller.rb +3 -0
  32. data/app/controllers/api/variants_controller.rb +24 -0
  33. data/app/controllers/api/zones_controller.rb +24 -0
  34. data/app/models/line_item_decorator.rb +7 -0
  35. data/app/models/order_decorator.rb +5 -0
  36. data/app/models/shipment_decorator.rb +5 -0
  37. data/app/models/user_decorator.rb +22 -0
  38. data/app/views/admin/users/_api_fields.html.erb +16 -0
  39. data/config/cucumber.yml +10 -0
  40. data/config/locales/en.yml +16 -0
  41. data/config/routes.rb +143 -0
  42. data/db/migrate/20100107141738_add_api_key_to_users.rb +9 -0
  43. data/lib/spree_api.rb +16 -0
  44. data/lib/spree_api_hooks.rb +3 -0
  45. data/lib/tasks/install.rake +23 -0
  46. metadata +115 -0
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2007-2010, Rails Dog LLC and other contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ Spree API
2
+ =========
3
+ Manage orders,shipments etc. with a simple REST API
4
+
5
+ See [RESTful API guide](http://spreecommerce.com/documentation/rest.html) for more details.
6
+
7
+ Testing
8
+ =======
9
+
10
+ Create the test site
11
+
12
+ rake test_app
13
+
14
+ Run the tests
15
+
16
+ rake spec
@@ -0,0 +1,19 @@
1
+ Admin::UsersController.class_eval do
2
+
3
+ before_filter :load_roles, :only => [:edit, :new, :update, :create, :generate_api_key, :clear_api_key]
4
+
5
+ def generate_api_key
6
+ if @user.generate_api_key!
7
+ flash.notice = t('api.key_generated')
8
+ end
9
+ redirect_to edit_admin_user_path(@user)
10
+ end
11
+
12
+ def clear_api_key
13
+ if @user.clear_api_key!
14
+ flash.notice = t('api.key_cleared')
15
+ end
16
+ redirect_to edit_admin_user_path(@user)
17
+ end
18
+
19
+ end
@@ -0,0 +1,13 @@
1
+ class Api::AdjustmentsController < Api::BaseController
2
+ public
3
+ def destroy
4
+ puts "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
5
+ puts params[object_name]
6
+ puts params[:id]
7
+ @object=Adjustment.find_by_id(params[:id])
8
+ @object.destroy
9
+ if @object.destroy
10
+ render :text => 'Destroyed Successfully'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,183 @@
1
+ class Api::BaseController < Spree::BaseController
2
+ before_filter :check_http_authorization
3
+ before_filter :load_resource
4
+ skip_before_filter :verify_authenticity_token, :if => lambda { admin_token_passed_in_headers }
5
+ authorize_resource
6
+
7
+ respond_to :json
8
+
9
+ def index
10
+ respond_with(@collection) do |format|
11
+ format.json { render :json => @collection.to_json(collection_serialization_options) }
12
+ end
13
+ end
14
+
15
+ def show
16
+ respond_with(@object) do |format|
17
+ format.json { render :json => @object.to_json(object_serialization_options) }
18
+ end
19
+ end
20
+
21
+ def create
22
+ if @object.save
23
+ # render :text => "Resource created\n", :status => 201, :location => object_url
24
+ render :json => @object.to_json, :status => 201
25
+ else
26
+ respond_with(@object.errors, :status => 422)
27
+ end
28
+ end
29
+
30
+ def update
31
+ if @object.update_attributes(params[object_name])
32
+
33
+ render :json => @object.to_json, :status => 201
34
+
35
+ else
36
+ respond_with(@object.errors, :status => 422)
37
+ end
38
+ end
39
+
40
+ def admin_token_passed_in_headers
41
+ request.headers['HTTP_AUTHORIZATION'].present?
42
+ end
43
+
44
+ def access_denied
45
+ render :text => 'access_denied', :status => 401
46
+ end
47
+
48
+ # Generic action to handle firing of state events on an object
49
+ def event
50
+ valid_events = model_class.state_machine.events.map(&:name)
51
+ valid_events_for_object = @object ? @object.state_transitions.map(&:event) : []
52
+
53
+ if params[:e].blank?
54
+ errors = t('api.errors.missing_event')
55
+ elsif valid_events_for_object.include?(params[:e].to_sym)
56
+ @object.send("#{params[:e]}!")
57
+ errors = nil
58
+ elsif valid_events.include?(params[:e].to_sym)
59
+ errors = t('api.errors.invalid_event_for_object', :events => valid_events_for_object.join(','))
60
+ else
61
+ errors = t('api.errors.invalid_event', :events => valid_events.join(','))
62
+ end
63
+
64
+ respond_to do |wants|
65
+ wants.json do
66
+ if errors.blank?
67
+ render :nothing => true
68
+ else
69
+ render :json => errors.to_json, :status => 422
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ protected
76
+ def model_class
77
+ controller_name.classify.constantize
78
+ end
79
+
80
+ def object_name
81
+ controller_name.singularize
82
+ end
83
+
84
+ def load_resource
85
+ if member_action?
86
+ @object ||= load_resource_instance
87
+ instance_variable_set("@#{object_name}", @object)
88
+ else
89
+ @collection ||= collection
90
+ instance_variable_set("@#{controller_name}", @collection)
91
+ end
92
+ end
93
+
94
+ def load_resource_instance
95
+ if new_actions.include?(params[:action].to_sym)
96
+ build_resource
97
+ elsif params[:id]
98
+ find_resource
99
+ end
100
+ end
101
+
102
+ def parent
103
+ nil
104
+ end
105
+
106
+ def find_resource
107
+ if parent.present?
108
+ parent.send(controller_name).find(params[:id])
109
+ else
110
+ model_class.includes(eager_load_associations).find(params[:id])
111
+ end
112
+ end
113
+
114
+ def build_resource
115
+ if parent.present?
116
+ parent.send(controller_name).build(params[object_name])
117
+ else
118
+ model_class.new(params[object_name])
119
+ end
120
+ end
121
+
122
+ def collection
123
+ return @search unless @search.nil?
124
+ params[:search] = {} if params[:search].blank?
125
+ params[:search][:meta_sort] = 'created_at.desc' if params[:search][:meta_sort].blank?
126
+
127
+ scope = parent.present? ? parent.send(controller_name) : model_class.scoped
128
+
129
+ @search = scope.metasearch(params[:search]).relation.limit(100)
130
+ @search
131
+ end
132
+
133
+ def collection_serialization_options
134
+ {:methods => :authentication_token}
135
+ end
136
+
137
+ def object_serialization_options
138
+ {:methods=>:authentication_token}
139
+ end
140
+
141
+ def eager_load_associations
142
+ nil
143
+ end
144
+
145
+ def object_errors
146
+ {:errors => object.errors.full_messages}
147
+ end
148
+
149
+ def object_url(object = nil, options = {})
150
+ target = object ? object : @object
151
+ puts @object.inspect
152
+ puts object_name
153
+ if parent.present? && object_name == "state"
154
+ send "api_country_#{object_name}_url", parent, target, options
155
+ elsif parent.present? && object_name == "taxon"
156
+ send "api_taxonomy_#{object_name}_url", parent, target, options
157
+ elsif parent.present?
158
+ send "api_#{parent[:model_name]}_#{object_name}_url", parent, target, options
159
+ else
160
+ send "api_#{object_name}_url",parent, target, options
161
+ end
162
+ end
163
+
164
+ def collection_actions
165
+ [:index]
166
+ end
167
+
168
+ def member_action?
169
+ !collection_actions.include? params[:action].to_sym
170
+ end
171
+
172
+ def new_actions
173
+ [:new, :create]
174
+ end
175
+
176
+ private
177
+ def check_http_authorization
178
+ if current_user.authentication_token!=params[:authentication_token]
179
+ render :text => "Access Denied\n", :status => 401
180
+ end if current_user
181
+ end
182
+
183
+ end
@@ -0,0 +1,89 @@
1
+ class Api::Checkout1Controller < CheckoutController
2
+
3
+ #~ ssl_required
4
+
5
+ #~ before_filter :load_order
6
+ #~ rescue_from Spree::GatewayError, :with => :rescue_from_spree_gateway_error
7
+
8
+ #~ respond_to :json
9
+ #~ def update_order
10
+ #~ @order = current_order
11
+ #~ if @order.update_attributes(object_params)
12
+ #~ puts @order.state
13
+ #~ if @order.next
14
+ #~ state_callback(:after)
15
+ #~ else
16
+ #~ #flash[:error] = I18n.t(:payment_processing_failed)
17
+ #~ render :json => @order.errors, :status => 422
18
+ #~ return
19
+ #~ end
20
+ #~ puts @order.state
21
+ #~ if @order.state == "complete" || @order.completed?
22
+ #~ flash[:notice] = I18n.t(:order_processed_successfully)
23
+ #~ flash[:commerce_tracking] = "nothing special"
24
+ #~ render :json => @order.to_json, :status => 201
25
+ #~ else
26
+ #~ respond_with(@order, :location => checkout_state_path(@order.state))
27
+ #~ end
28
+ #~ else
29
+ #~ render :json => @order.errors, :status => 422
30
+ #~ #respond_with(@order) { |format| format.html { render :edit } }
31
+ #~ end
32
+ #~ end
33
+
34
+ #~ private
35
+
36
+ #~ # Provides a route to redirect after order completion
37
+ #~ def completion_route
38
+ #~ order_path(@order)
39
+ #~ end
40
+
41
+ #~ def object_params
42
+ #~ # For payment step, filter order parameters to produce the expected nested attributes for a single payment and its source, discarding attributes for payment methods other than the one selected
43
+ #~ if @order.payment
44
+ #~ if params[:payment_source].present? && source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].underscore]
45
+ #~ params[:order][:payments_attributes].first[:source_attributes] = source_params
46
+ #~ end
47
+ #~ if (params[:order][:payments_attributes])
48
+ #~ params[:order][:payments_attributes].first[:amount] = @order.total
49
+ #~ end
50
+ #~ end
51
+ #~ params[:order]
52
+ #~ end
53
+
54
+ #~ def load_order
55
+ #~ @order = current_order
56
+ #~ render :text => 'Order Not Available / Checkout Not Possible' and return unless @order and @order.checkout_allowed?
57
+ #~ render :text => 'Order Already Completed' and return if @order.completed?
58
+ #~ @order.state = params[:state] if params[:state]
59
+ #~ state_callback(:before)
60
+ #~ end
61
+
62
+ #~ def state_callback(before_or_after = :before)
63
+ #~ method_name = :"#{before_or_after}_#{@order.state}"
64
+ #~ send(method_name) if respond_to?(method_name, true)
65
+ #~ end
66
+
67
+ #~ def before_address
68
+ #~ @order.bill_address ||= Address.default
69
+ #~ @order.ship_address ||= Address.default
70
+ #~ end
71
+
72
+ #~ def before_delivery
73
+ #~ return if params[:order].present?
74
+ #~ @order.shipping_method ||= (@order.rate_hash.first && @order.rate_hash.first[:shipping_method])
75
+ #~ end
76
+
77
+ #~ def before_payment
78
+ #~ current_order.payments.destroy_all if request.put?
79
+ #~ end
80
+
81
+ #~ def after_complete
82
+ #~ session[:order_id] = nil
83
+ #~ end
84
+
85
+ #~ def rescue_from_spree_gateway_error
86
+ #~ flash[:error] = t('spree_gateway_error_flash_for_checkout')
87
+ #~ render :edit
88
+ #~ end
89
+ end
@@ -0,0 +1,3 @@
1
+ class Api::CountriesController < Api::BaseController
2
+ before_filter :access_denied, :except => [:index, :show]
3
+ end
@@ -0,0 +1,15 @@
1
+ class Api::ImagesController < Spree::Api::BaseController
2
+
3
+ public
4
+ def destroy
5
+ puts "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
6
+ puts params[object_name]
7
+ puts params[:id]
8
+ @object=Image.find_by_id(params[:id])
9
+ @object.destroy
10
+ if @object.destroy
11
+ render :text => 'Image Destroyed Successfully'
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,19 @@
1
+ class Api::InventoryUnitsController < Api::BaseController
2
+ private
3
+ def parent
4
+ if params[:order_id]
5
+ @parent = Order.find_by_param(params[:order_id])
6
+ elsif params[:shipment_id]
7
+ @parent = Shipment.find_by_param(params[:shipment_id])
8
+ end
9
+ end
10
+
11
+ def parent_data
12
+ [params[:order_id], params[:shipment_id]].compact
13
+ end
14
+
15
+ def eager_load_associations
16
+ [:variant]
17
+ end
18
+
19
+ end
@@ -0,0 +1,37 @@
1
+ class Api::LineItemsController < Api::BaseController
2
+
3
+ private
4
+ def parent
5
+ if params[:order_id]
6
+ @parent ||= Order.find_by_param(params[:order_id])
7
+ end
8
+ end
9
+
10
+ def parent_data
11
+ params[:order_id]
12
+ end
13
+
14
+ def collection_serialization_options
15
+ { :include => [:variant], :methods => [:description] }
16
+ end
17
+
18
+ def object_serialization_options
19
+ collection_serialization_options
20
+ end
21
+ public
22
+ def create
23
+ quantity = params[:line_item][:quantity]
24
+ @variant = Variant.find_by_id(params[:line_item][:variant_id])
25
+ @order = current_order(true)
26
+ @order.add_variant(@variant, quantity.to_i) if quantity.to_i > 0
27
+ @response = Order.find_by_id(@order.id)
28
+ render :json => @response.to_json, :status => 201
29
+ end
30
+ def destroy
31
+ @object=LineItem.find_by_id(params[:id])
32
+ @object.destroy
33
+ if @object.destroy
34
+ render :text => 'Destroyed'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ class Api::MailMethodsController < Api::BaseController
2
+ def testmail
3
+ p "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
4
+ if TestMailer.test_email(@mail_method, current_user).deliver
5
+ respond_to do |f|
6
+ f.json {render :json=>{:response=>:sucess}}
7
+ end
8
+ end
9
+ end
10
+ end