machineshop 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/machineshop.rb +146 -142
- data/lib/machineshop/api_operations/create.rb +1 -1
- data/lib/machineshop/api_operations/delete.rb +1 -1
- data/lib/machineshop/api_operations/list.rb +1 -1
- data/lib/machineshop/api_operations/update.rb +1 -1
- data/lib/machineshop/api_resource.rb +1 -1
- data/lib/machineshop/configuration.rb +5 -2
- data/lib/machineshop/data_source_types.rb +1 -1
- data/lib/machineshop/data_sources.rb +2 -2
- data/lib/machineshop/device.rb +1 -1
- data/lib/machineshop/device_instance.rb +1 -1
- data/lib/machineshop/end_points.rb +30 -0
- data/lib/machineshop/mapping.rb +3 -3
- data/lib/machineshop/models/api_endpoint.rb +8 -0
- data/lib/machineshop/models/api_request.rb +1 -0
- data/lib/machineshop/models/schema.rb +15 -0
- data/lib/machineshop/rule.rb +6 -22
- data/lib/machineshop/user.rb +3 -3
- data/lib/machineshop/users.rb +3 -3
- data/lib/machineshop/util.rb +82 -5
- data/lib/machineshop/utility.rb +2 -2
- data/lib/machineshop/version.rb +1 -1
- data/machineshop.gemspec +2 -6
- data/spec/lib/api_calls_spec.rb +628 -0
- data/spec/lib/custom_endpoint_test.rb +78 -0
- data/spec/lib/customer_spec.rb +39 -14
- data/spec/lib/data_source.rb +2 -0
- data/spec/lib/database_spec.rb +3 -1
- data/spec/lib/device_instances.rb +9 -1
- data/spec/lib/device_spec.rb +24 -0
- data/spec/lib/endpoint_spec.rb +37 -0
- data/spec/lib/{test_spec.rb → geocode_spec} +12 -14
- data/spec/lib/mapping_spec.rb +11 -0
- data/spec/lib/meter_spec.rb +8 -0
- data/spec/lib/report_spec.rb +6 -0
- data/spec/lib/rule_spec.rb +50 -26
- data/spec/lib/user_spec.rb +11 -0
- data/spec/spec_helper.rb +10 -1
- metadata +12 -5
- data/lib/machineshop/models/people.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c441ae662e654ac372b9769d7b30dd03acaa18c
|
4
|
+
data.tar.gz: 2447a88093b40b35e9fbc01795406260622507d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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/
|
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
|
-
|
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
|
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
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
272
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
307
|
-
|
313
|
+
modelClass.delete_all
|
314
|
+
puts "db table #{klass.pluralize.underscore} exists"
|
315
|
+
if data.class ==Hash
|
308
316
|
|
309
|
-
|
310
|
-
|
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
|
-
|
322
|
+
val=nil
|
313
323
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
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
|
-
|
330
|
-
|
331
|
-
|
332
|
-
if data_arr
|
333
|
-
|
334
|
-
data_arr.
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
if
|
388
|
-
klass
|
389
|
-
|
390
|
-
|
391
|
-
|
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
|
-
|
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.
|
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
|
@@ -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.
|
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
|