machineshop 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/machineshop.rb +146 -142
  3. data/lib/machineshop/api_operations/create.rb +1 -1
  4. data/lib/machineshop/api_operations/delete.rb +1 -1
  5. data/lib/machineshop/api_operations/list.rb +1 -1
  6. data/lib/machineshop/api_operations/update.rb +1 -1
  7. data/lib/machineshop/api_resource.rb +1 -1
  8. data/lib/machineshop/configuration.rb +5 -2
  9. data/lib/machineshop/data_source_types.rb +1 -1
  10. data/lib/machineshop/data_sources.rb +2 -2
  11. data/lib/machineshop/device.rb +1 -1
  12. data/lib/machineshop/device_instance.rb +1 -1
  13. data/lib/machineshop/end_points.rb +30 -0
  14. data/lib/machineshop/mapping.rb +3 -3
  15. data/lib/machineshop/models/api_endpoint.rb +8 -0
  16. data/lib/machineshop/models/api_request.rb +1 -0
  17. data/lib/machineshop/models/schema.rb +15 -0
  18. data/lib/machineshop/rule.rb +6 -22
  19. data/lib/machineshop/user.rb +3 -3
  20. data/lib/machineshop/users.rb +3 -3
  21. data/lib/machineshop/util.rb +82 -5
  22. data/lib/machineshop/utility.rb +2 -2
  23. data/lib/machineshop/version.rb +1 -1
  24. data/machineshop.gemspec +2 -6
  25. data/spec/lib/api_calls_spec.rb +628 -0
  26. data/spec/lib/custom_endpoint_test.rb +78 -0
  27. data/spec/lib/customer_spec.rb +39 -14
  28. data/spec/lib/data_source.rb +2 -0
  29. data/spec/lib/database_spec.rb +3 -1
  30. data/spec/lib/device_instances.rb +9 -1
  31. data/spec/lib/device_spec.rb +24 -0
  32. data/spec/lib/endpoint_spec.rb +37 -0
  33. data/spec/lib/{test_spec.rb → geocode_spec} +12 -14
  34. data/spec/lib/mapping_spec.rb +11 -0
  35. data/spec/lib/meter_spec.rb +8 -0
  36. data/spec/lib/report_spec.rb +6 -0
  37. data/spec/lib/rule_spec.rb +50 -26
  38. data/spec/lib/user_spec.rb +11 -0
  39. data/spec/spec_helper.rb +10 -1
  40. metadata +12 -5
  41. data/lib/machineshop/models/people.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5398a18d7fa06b98439cd8fb269584d75470e086
4
- data.tar.gz: 79a3b0359fd318cb575793e235b3c9d874e049ea
3
+ metadata.gz: 3c441ae662e654ac372b9769d7b30dd03acaa18c
4
+ data.tar.gz: 2447a88093b40b35e9fbc01795406260622507d3
5
5
  SHA512:
6
- metadata.gz: 91f0b27d60bc49ebd22a466b5522ecc449e0f3ab4d533c18d0c4a856fb69ba2e593c9938831efd16b0704ca16a37cda0452782ba0f048aaafeba707e470d2f10
7
- data.tar.gz: 06609725eaf1067089ece7db9bbe045b11a13af8b821e86305bcc484749645730ba6688c2d35be197f595ea5fa2c0089617b7c1ec23ca269960a2c94642e4202
6
+ metadata.gz: 45cceddbd5c827716a615014bfb9f1b7e32924f0031db5b1e65f6c084cf5de12a79408582a6b1f8aa4c2f7d58a9bae25a2a124857779e73c2b9f744f9cfc2f7e
7
+ data.tar.gz: e8b4b81157b84c98a1a33be04fc77c7e48fa134ec302188d4bb2ce28ab21ba8cf5fbda921f2f2ecb27de401cdf25f42d63f64be5f43a8e0bc7033d8ff0dad0a5
data/lib/machineshop.rb CHANGED
@@ -27,11 +27,6 @@ require 'machineshop/api_operations/update'
27
27
  require 'machineshop/machineshop_object'
28
28
  require 'machineshop/api_resource'
29
29
  require 'machineshop/device_instance'
30
-
31
- require 'machineshop/data_sources'
32
- require 'machineshop/data_source_types'
33
-
34
-
35
30
  require 'machineshop/device'
36
31
  require 'machineshop/machineshop_object'
37
32
  require 'machineshop/mapping'
@@ -44,6 +39,10 @@ require 'machineshop/users'
44
39
  require 'machineshop/utility'
45
40
  require 'machineshop/json'
46
41
  require 'machineshop/util'
42
+ require 'machineshop/end_points'
43
+
44
+ require 'machineshop/data_sources'
45
+ require 'machineshop/data_source_types'
47
46
 
48
47
  # Errors
49
48
  require 'machineshop/errors/machineshop_error'
@@ -54,13 +53,13 @@ require 'machineshop/errors/api_connection_error'
54
53
 
55
54
  #Models
56
55
  require 'machineshop/models/api_request'
57
- # require 'machineshop/models/device_cache'
56
+ require 'machineshop/models/api_endpoint'
58
57
 
59
58
 
60
59
  module MachineShop
61
60
  class << self
62
61
  # @@api_base_url = 'http://api.machineshop.io/api/v0'
63
- @@api_base_url = 'http://stage.services.machineshop.io/api/v1'
62
+ @@api_base_url = 'http://stage.services.machineshop.io/api/v0'
64
63
 
65
64
  #configs starts
66
65
  attr_writer :configuration
@@ -88,19 +87,41 @@ module MachineShop
88
87
  @@api_base_url
89
88
  end
90
89
 
91
- def get(url, auth_token, body_hash=nil)
90
+ #For Custom endpoints
91
+ def get(name, auth_token, *params)
92
+ url = Util.valid_endpoint(name,auth_token,:get, params)
93
+ platform_request(url, auth_token, nil, :get)
94
+ end
95
+
96
+ def post(name,auth_token, body_hash)
97
+ url = Util.valid_endpoint(name,auth_token,:post,[])
98
+ platform_request(url, auth_token,body_hash,:post)
99
+ end
100
+
101
+ def put(name,auth_token,*params,body_hash)
102
+ url = Util.valid_endpoint(name,auth_token,:put,params)
103
+ platform_request(url, auth_token,body_hash,:put)
104
+ end
105
+
106
+ def delete(name,auth_token,*params)
107
+ url = Util.valid_endpoint(name,auth_token,:delete,params)
108
+ platform_request(url, auth_token, nil ,:delete)
109
+ end
110
+
111
+ #Call for the predefined request
112
+ def gem_get(url, auth_token, body_hash=nil)
92
113
  platform_request(url, auth_token, body_hash)
93
114
  end
94
115
 
95
- def post(url, auth_token, body_hash)
116
+ def gem_post(url, auth_token, body_hash)
96
117
  platform_request(url, auth_token, body_hash, :post)
97
118
  end
98
119
 
99
- def delete(url, auth_token, body_hash)
120
+ def gem_delete(url, auth_token, body_hash)
100
121
  platform_request(url, auth_token, body_hash, :delete)
101
122
  end
102
123
 
103
- def put(url, auth_token, body_hash)
124
+ def gem_put(url, auth_token, body_hash)
104
125
  platform_request(url, auth_token, body_hash, :put)
105
126
  end
106
127
 
@@ -117,9 +138,12 @@ module MachineShop
117
138
  # ApiRequest.cache(url,MachineShop.configuration.expiry_time)
118
139
  if http_verb==:get
119
140
 
120
- if db_connected?
121
- ApiRequest.cache(url, auth_token, MachineShop.configuration.expiry_time) do
141
+ if Util.db_connected?
142
+ xpired=true
122
143
 
144
+ ApiRequest.cache(url, auth_token, MachineShop.configuration.expiry_time) do
145
+ xpired=false
146
+ puts "Not expired , calling from local "
123
147
  rbody = get_from_cache(url,body_hash,auth_token)
124
148
  rcode="200"
125
149
  end
@@ -158,6 +182,8 @@ module MachineShop
158
182
 
159
183
  end
160
184
 
185
+ puts "request params: #{opts} "
186
+
161
187
  begin
162
188
  response = execute_request(opts)
163
189
 
@@ -182,19 +208,19 @@ module MachineShop
182
208
  end
183
209
 
184
210
  rbody = response.body
211
+ # puts rbody
185
212
  rcode = response.code
186
213
  end
187
214
 
188
-
189
-
190
-
191
215
  begin
192
216
  # Would use :symbolize_names => true, but apparently there is
193
217
  # some library out there that makes symbolize_names not work.
194
218
  resp = MachineShop::JSON.load(rbody)
195
219
  resp ||= {}
196
220
  resp = Util.symbolize_names(resp)
221
+
197
222
  save_into_cache(url,resp,auth_token) if (http_verb == :get && cachedContent==:false)
223
+
198
224
  resp.merge!({:http_code => rcode}) if resp.is_a?(Hash)
199
225
  return resp
200
226
  rescue MultiJson::DecodeError
@@ -253,6 +279,7 @@ module MachineShop
253
279
  message = "Unexpected error communicating with MachineShop"
254
280
  end
255
281
  message += "\n\n(Network error: #{e.message})"
282
+ # puts "error message string : #{message}"
256
283
  raise APIConnectionError.new(message)
257
284
  end
258
285
 
@@ -266,168 +293,145 @@ module MachineShop
266
293
  end
267
294
 
268
295
 
269
- #get the classname from url and get the record if exists
296
+ def save_into_cache(url, data,auth_token)
297
+ id,klass= Util.get_klass_from_url(url)
298
+ if !TABLE_NAME_BLACKLIST.include?(klass)
299
+ if Util.db_connected?
270
300
 
271
- #Check if db_connected
272
- def db_connected?
273
- db_connected = true
274
- begin
275
- MachineShop::Database.new
276
- rescue DatabaseError =>e
277
- db_connected= false
278
- rescue SchemaError =>e
279
- # db_connected=true
280
- end
301
+ klass = klass.capitalize+"Cache"
302
+ puts "creating dynamic class #{klass}"
281
303
 
282
- db_connected
283
- end
284
304
 
305
+ modelClass ||= (Object.const_set klass, Class.new(ActiveRecord::Base))
306
+ modelClass.inheritance_column = :_type_disabled
307
+ #Because 'type' is reserved for storing the class in case of inheritance and our array has "TYPE" key
285
308
 
286
- def save_into_cache(url, data,auth_token)
287
- if db_connected?
288
-
289
- id=nil
290
- splitted = url.split('/')
291
- klass = splitted[-1]
292
- if /[0-9]/.match(klass)
293
- id=splitted[-1]
294
- if splitted[-3]=="rule"
295
- klass="rule"
296
- else
297
- klass = splitted[-2]
298
- end
299
- end
309
+ if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
300
310
 
301
- klass = klass.capitalize+"Cache"
302
- modelClass ||= (Object.const_set klass, Class.new(ActiveRecord::Base))
303
- modelClass.inheritance_column = :_type_disabled
304
- #Because 'type' is reserved for storing the class in case of inheritance and our array has "TYPE" key
311
+ #delete all the previous records
305
312
 
306
- if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
307
- if data.class ==Hash
313
+ modelClass.delete_all
314
+ puts "db table #{klass.pluralize.underscore} exists"
315
+ if data.class ==Hash
308
316
 
309
- findId = data[:_id] || data["_id"]
310
- @activeObject = modelClass.find_by(_id: findId) || modelClass.new
317
+ findId = data[:_id] || data["_id"]
318
+ @activeObject = modelClass.new
319
+ # @activeObject = modelClass.find_by(_id: findId) || modelClass.new
320
+ data.each do |k,v|
311
321
 
312
- data.each do |k,v|
322
+ val=nil
313
323
 
314
- val=nil
315
- if v.class==Array
316
- val = v.to_json
317
- elsif v.class==Hash
318
- val = v.to_json
319
- else
320
- val=v
321
- end
322
- if @activeObject.has_attribute?(k)
323
- @activeObject.send("#{k}=",val)
324
+ if v.class==Array
325
+ val = v.to_json
326
+ elsif v.class==Hash
327
+ val = v.to_json
328
+ else
329
+ val=v
330
+ end
331
+ if @activeObject.has_attribute?(k)
332
+ @activeObject.send("#{k}=",val)
333
+ end
334
+ @activeObject.save
324
335
  end
325
- @activeObject.save
326
- end
327
336
 
328
337
 
329
- else
330
- data.each do |data_arr|
331
- if data_arr
332
- if data_arr.first.class==String && data_arr.class==Array
333
- @activeObject = modelClass.find_by(rule_condition: data_arr.select{|k| k.include?("rule_condition")}) || modelClass.new
334
- data_arr.each do |k|
335
- if k.include?("rule_condition")
336
- @activeObject.rule_condition = k
337
- else
338
- @activeObject.rule_description=k
339
- end
340
- end
341
- else
342
- if data_arr.class!=String
343
- findId = data_arr[:_id] || data_arr["_id"]
344
- @activeObject = modelClass.find_by(_id: findId) || modelClass.new
345
- data_arr.each do |k,v|
346
- val=nil
347
- if v.class==Array
348
- val = v.to_json
349
- elsif v.class==Hash
350
- val = v.to_json
338
+ else
339
+ data.each do |data_arr|
340
+
341
+ if data_arr
342
+
343
+ if data_arr.first.class==String && data_arr.class==Array
344
+ @activeObject = modelClass.find_by(rule_condition: data_arr.select{|k| k.include?("rule_condition")}) || modelClass.new
345
+ data_arr.each do |k|
346
+
347
+ if k.include?("rule_condition")
348
+ @activeObject.rule_condition = k
351
349
  else
352
- val=v
350
+ @activeObject.rule_description=k
353
351
  end
352
+ end
354
353
 
355
- #check if the database has the particular field to store
356
- if @activeObject.has_attribute?(k)
357
- @activeObject.send("#{k}=",val)
358
- end
359
354
 
355
+ else
356
+ if data_arr.class!=String
357
+ findId = data_arr[:_id] || data_arr["_id"]
358
+ @activeObject = modelClass.find_by(_id: findId) || modelClass.new
359
+ data_arr.each do |k,v|
360
+
361
+ val=nil
362
+
363
+ if v.class==Array
364
+ val = v.to_json
365
+ elsif v.class==Hash
366
+ val = v.to_json
367
+ else
368
+ val=v
369
+ end
370
+ #check if the database has the particular field to store
371
+ if @activeObject.has_attribute?(k)
372
+ @activeObject.send("#{k}=",val)
373
+ end
374
+ end
360
375
  end
361
376
  end
362
377
  end
378
+ @activeObject.send("auth_token=",auth_token)
379
+ @activeObject.save
363
380
  end
364
- @activeObject.send("auth_token=",auth_token)
365
- @activeObject.save
366
381
  end
367
- end
368
- end
369
- end
370
382
 
371
- end
383
+ end #if ActiveRecord ends
372
384
 
385
+ end #if db_connected ends
386
+ end #if !TABLE_NAME_BLACKLIST.include? ends
387
+ end
373
388
 
374
389
 
375
390
  def get_from_cache(url, body_hash,auth_token)
376
391
  result =Array.new
377
- if db_connected?
378
-
379
- id=nil
380
- splitted = url.split('/')
381
- klass = splitted[-1]
382
-
383
-
384
- if /[0-9]/.match(klass)
385
- id=splitted[-1]
386
-
387
- if splitted[-3]=="rule"
388
- klass="rule"
389
- else
390
- klass = splitted[-2]
391
- end
392
-
393
- end
394
-
395
- klass = klass.capitalize+"Cache"
396
-
397
- modelClass = Object.const_set klass, Class.new(ActiveRecord::Base)
398
- modelClass.inheritance_column = :_type_disabled
399
-
400
- data_exist=false
401
- if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
402
- resp= nil
403
- if id
404
- resp = modelClass.find_by(_id: id, auth_token: auth_token)
405
- data_exist=true if resp
406
- else
407
- # pagination = body_hash.select{|k| k==:per_page || k==:page} if body_hash
408
- resp = modelClass.where(parse_query_string(body_hash,auth_token))
409
- data_exist = true if resp.exists?
410
- end
411
-
412
- if data_exist
413
- if(klass.include?("rule_condition"))
414
- resp.each do |rTemp|
415
- temp = Array.new
416
- temp.push rTemp["rule_description"]
417
- temp.push rTemp["rule_condition"]
418
- result << temp
419
- end
420
- result = result.to_json(:except=>[:id]) if result
392
+ id,klass= Util.get_klass_from_url(url)
393
+ if !TABLE_NAME_BLACKLIST.include?(klass)
394
+ if Util.db_connected?
395
+
396
+ klass = klass.capitalize+"Cache"
397
+
398
+ modelClass = Object.const_set klass, Class.new(ActiveRecord::Base)
399
+ modelClass.inheritance_column = :_type_disabled
400
+
401
+ data_exist=false
402
+ if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
403
+ puts "db:table #{klass.pluralize} exists"
404
+ resp= nil
405
+ if id
406
+ resp = modelClass.find_by(_id: id, auth_token: auth_token)
407
+ data_exist=true if resp
421
408
  else
422
- result = resp.to_json(:except=>[:id]) if resp
409
+ # pagination = body_hash.select{|k| k==:per_page || k==:page} if body_hash
410
+ resp = modelClass.where(parse_query_string(body_hash,auth_token))
411
+ data_exist = true if resp.exists?
423
412
  end
424
413
 
414
+ if data_exist
415
+ if(klass.include?("rule_condition"))
416
+ resp.each do |rTemp|
417
+ temp = Array.new
418
+ temp.push rTemp["rule_description"]
419
+ temp.push rTemp["rule_condition"]
420
+ result << temp
421
+ end
422
+ result = result.to_json(:except=>[:id]) if result
423
+ else
424
+ result = resp.to_json(:except=>[:id]) if resp
425
+ end
426
+
427
+ end
425
428
  end
426
429
  end
427
430
  end
428
431
  return result
429
432
  end
430
433
 
434
+ TABLE_NAME_BLACKLIST = ["user","routes"]
431
435
  QUERY_STRING_BLACKLIST = [
432
436
  'page',
433
437
  'per_page'
@@ -4,7 +4,7 @@ module MachineShop
4
4
  module Create
5
5
  module ClassMethods
6
6
  def create(params={}, auth_token=nil)
7
- response = MachineShop.post(self.url, auth_token, params)
7
+ response = MachineShop.gem_post(self.url, auth_token, params)
8
8
  Util.convert_to_machineshop_object(response, auth_token, self.class_name)
9
9
  end
10
10
  end
@@ -2,7 +2,7 @@ module MachineShop
2
2
  module APIOperations
3
3
  module Delete
4
4
  def delete
5
- response = MachineShop.delete(url, @auth_token,{})
5
+ response = MachineShop.gem_delete(url, @auth_token,{})
6
6
  refresh_from(response, @auth_token)
7
7
  self
8
8
  end
@@ -3,7 +3,7 @@ module MachineShop
3
3
  module List
4
4
  module ClassMethods
5
5
  def all(filters={}, auth_token=nil)
6
- response = MachineShop.get(url, auth_token, filters)
6
+ response = MachineShop.gem_get(url, auth_token, filters)
7
7
  Util.convert_to_machineshop_object(response, auth_token, self.class_name)
8
8
  end
9
9
  end