apispree_api 0.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/analyses_controller.rb +134 -0
  6. data/app/controllers/api/base_controller.rb +187 -0
  7. data/app/controllers/api/checkout1_controller.rb +4 -0
  8. data/app/controllers/api/countries_controller.rb +14 -0
  9. data/app/controllers/api/images_controller.rb +15 -0
  10. data/app/controllers/api/inventory_units_controller.rb +19 -0
  11. data/app/controllers/api/line_items_controller.rb +37 -0
  12. data/app/controllers/api/mail_methods_controller.rb +10 -0
  13. data/app/controllers/api/option_types_controller.rb +24 -0
  14. data/app/controllers/api/orders_controller.rb +51 -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 +25 -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 +19 -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 +144 -0
  42. data/db/migrate/20100107141738_add_api_key_to_users.rb +9 -0
  43. data/lib/apispree_api.rb +16 -0
  44. data/lib/spree_api_hooks.rb +3 -0
  45. data/lib/tasks/install.rake +23 -0
  46. metadata +112 -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.
@@ -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,134 @@
1
+ class Api::AnalysesController< ActionController::Base
2
+
3
+ def best_selling_products
4
+ return_data=Hash.new
5
+ prod_array=Array.new
6
+ best=ActiveRecord::Base.connection.execute("Select A.id,A.name,sum(C.quantity) qty from products A, variants B, line_items C,orders D where A.id=B.product_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.name order by 3,1")
7
+ best.each do |pr|
8
+ prod_dtl=Hash.new
9
+ prod_dtl[:id]=pr[0]
10
+ prod_dtl[:name]=pr[1]
11
+ prod_dtl[:qty]=pr[2]
12
+ prod_array.push prod_dtl
13
+ end
14
+ return_data[:products] = prod_array
15
+ render :json => return_data.to_json, :status => 201
16
+ end
17
+
18
+ def gross_selling_products
19
+ return_data=Hash.new
20
+ prod_array=Array.new
21
+ best=ActiveRecord::Base.connection.execute("Select A.id,A.name,sum(B.cost_price * C.quantity) amount from products A, variants B, line_items C,orders D where A.id=B.product_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.name order by 3,1")
22
+ best.each do |pr|
23
+ prod_dtl=Hash.new
24
+ prod_dtl[:id]=pr[0]
25
+ prod_dtl[:name]=pr[1]
26
+ prod_dtl[:amount]=pr[2]
27
+ prod_array.push prod_dtl
28
+ end
29
+ return_data[:products] = prod_array
30
+ render :json => return_data.to_json, :status => 201
31
+ end
32
+
33
+ def top_spenders
34
+ return_data=Hash.new
35
+ prod_array=Array.new
36
+ best=ActiveRecord::Base.connection.execute("Select A.id,A.email,sum(C.quantity),sum(B.cost_price * C.quantity) from users A, variants B, line_items C, orders D where A.id=D.user_id and B.id=C.variant_id and C.order_id=D.id and D.payment_state in ('paid','completed','payment','complete') group by A.id,A.email order by 4,1")
37
+ best.each do |pr|
38
+ prod_dtl=Hash.new
39
+ prod_dtl[:id]=pr[0]
40
+ prod_dtl[:email]=pr[1]
41
+ prod_dtl[:qty]=pr[2]
42
+ prod_dtl[:value]=pr[3]
43
+ prod_array.push prod_dtl
44
+ end
45
+ return_data[:spenders] = prod_array
46
+ render :json => return_data.to_json, :status => 201
47
+ end
48
+ def recent_orders
49
+ return_data=Hash.new
50
+ prod_array=Array.new
51
+ best=ActiveRecord::Base.connection.execute("Select A.id,A.email,D.id,D.number,D.created_at,D.total from users A, orders D where A.id=D.user_id and D.payment_state in ('paid','completed','payment','complete') order by 4,3")
52
+ best.each do |pr|
53
+ prod_dtl=Hash.new
54
+ prod_dtl[:user_id]=pr[0]
55
+ prod_dtl[:email]=pr[1]
56
+ prod_dtl[:order_id]=pr[2]
57
+ prod_dtl[:order_number]=pr[3]
58
+ prod_dtl[:order_date]=pr[4]
59
+ prod_dtl[:order_total]=pr[5]
60
+ prod_array.push prod_dtl
61
+ end
62
+ return_data[:orders] = prod_array
63
+ render :json => return_data.to_json, :status => 201
64
+ end
65
+ def out_of_stock
66
+ return_data=Hash.new
67
+ prod_array=Array.new
68
+ best=ActiveRecord::Base.connection.execute("Select A.id,A.name,B.count_on_hand from products A, variants B where A.id=B.product_id and B.count_on_hand <=0 order by 1,2")
69
+ best.each do |pr|
70
+ prod_dtl=Hash.new
71
+ prod_dtl[:id]=pr[0]
72
+ prod_dtl[:name]=pr[1]
73
+ prod_dtl[:count_on_hand]=pr[2]
74
+ prod_array.push prod_dtl
75
+ end
76
+ return_data[:products] = prod_array
77
+ render :json => return_data.to_json, :status => 201
78
+ end
79
+ def day_order_count
80
+ return_data=Hash.new
81
+ prod_array=Array.new
82
+ best=ActiveRecord::Base.connection.execute("Select DATE(created_at),count(*) from orders where payment_state in ('paid','completed','payment','complete') group by DATE(created_at) order by 1 DESC")
83
+ best.each do |pr|
84
+ prod_dtl=Hash.new
85
+ prod_dtl[:order_date]=pr[0]
86
+ prod_dtl[:order_count]=pr[1]
87
+ prod_array.push prod_dtl
88
+ end
89
+ return_data[:orders] = prod_array
90
+ render :json => return_data.to_json, :status => 201
91
+ end
92
+ def day_order_value
93
+ return_data=Hash.new
94
+ prod_array=Array.new
95
+ best=ActiveRecord::Base.connection.execute("Select DATE(created_at),sum(total) from orders where payment_state in ('paid','completed','payment','complete') group by DATE(created_at) order by 1 DESC")
96
+ best.each do |pr|
97
+ prod_dtl=Hash.new
98
+ prod_dtl[:order_date]=pr[0]
99
+ prod_dtl[:total_order_value]=pr[1]
100
+ prod_array.push prod_dtl
101
+ end
102
+ return_data[:orders] = prod_array
103
+ render :json => return_data.to_json, :status => 201
104
+ end
105
+ def month_order_value
106
+ return_data=Hash.new
107
+ prod_array=Array.new
108
+ best=ActiveRecord::Base.connection.execute("Select Month(created_at),Year(created_at),sum(total) from orders where payment_state in ('paid','completed','payment','complete') group by Month(created_at),Year(created_at) order by 2 DESC ,1 DESC")
109
+ best.each do |pr|
110
+ prod_dtl=Hash.new
111
+ prod_dtl[:order_month]=pr[0]
112
+ prod_dtl[:order_year]=pr[1]
113
+ prod_dtl[:total_order_value]=pr[2]
114
+ prod_array.push prod_dtl
115
+ end
116
+ return_data[:orders] = prod_array
117
+ render :json => return_data.to_json, :status => 201
118
+ end
119
+ def month_order_count
120
+ return_data=Hash.new
121
+ prod_array=Array.new
122
+ best=ActiveRecord::Base.connection.execute("Select Month(created_at),Year(created_at),count(*) from orders where payment_state in ('paid','completed','payment','complete') group by Month(created_at),Year(created_at) order by 2 DESC ,1 DESC")
123
+ best.each do |pr|
124
+ prod_dtl=Hash.new
125
+ prod_dtl[:order_month]=pr[0]
126
+ prod_dtl[:order_year]=pr[1]
127
+ prod_dtl[:order_count]=pr[2]
128
+ prod_array.push prod_dtl
129
+ end
130
+ return_data[:orders] = prod_array
131
+ render :json => return_data.to_json, :status => 201
132
+ end
133
+
134
+ end
@@ -0,0 +1,187 @@
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
+ {}
135
+ end
136
+
137
+ def object_serialization_options
138
+ {}
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 request.headers['HTTP_AUTHORIZATION'].blank?
179
+ #~ render :text => "Access Denied\n", :status => 401
180
+ #~ end
181
+ if current_user.authentication_token!=params[:authentication_token]
182
+ # if request.headers['HTTP_AUTHORIZATION'].blank?
183
+ render :text => "Access Denied\n", :status => 401
184
+ end if current_user
185
+ end
186
+
187
+ end
@@ -0,0 +1,4 @@
1
+ class Api::Checkout1Controller < CheckoutController
2
+
3
+
4
+ end
@@ -0,0 +1,14 @@
1
+ class Api::CountriesController < Api::BaseController
2
+ before_filter :access_denied, :except => [:index, :show,:create,:update,:destroy]
3
+ public
4
+ def destroy
5
+ puts "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
6
+ puts params[object_name]
7
+ puts params[:id]
8
+ @object=Country.find_by_id(params[:id])
9
+ @object.destroy
10
+ if @object.destroy
11
+ render :text => 'Destroyed'
12
+ end
13
+ end
14
+ 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