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.
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
@@ -0,0 +1,628 @@
1
+ require 'spec_helper'
2
+
3
+ #MachineShop.api_base_url= 'http://machineshop.dev:3000/api/v0'
4
+ MachineShop.api_base_url= 'http://stage.services.machineshop.io/api/v0'
5
+
6
+ #publisher_username = 'publisher@machineshop.com'
7
+ publisher_username = 'publisher@csr.com'
8
+ publisher_password = 'password'
9
+
10
+
11
+
12
+ =begin
13
+ describe "#expiry_time" do
14
+ it "default value is 6" do
15
+ MachineShop::Configuration.new.expiry_time =23
16
+ # puts "original value is #{MachineShop::Configuration.expiry_time}"
17
+ end
18
+ end
19
+
20
+
21
+ describe "#expiry_time=" do
22
+ it "can set value" do
23
+
24
+ MachineShop.configure do |config|
25
+ config.expiry_time = 10
26
+ config.enable_caching = false
27
+ config.db_username="root"
28
+ config.db_password="root"
29
+ config.db_name="machineshop"
30
+ end
31
+ config = MachineShop.configuration
32
+
33
+
34
+ config.expiry_time = 7
35
+ expect(config.expiry_time).to eq(7)
36
+ puts "config.expiry_time #{config.expiry_time}"
37
+ end
38
+
39
+
40
+ auth_token, user = MachineShop::User.authenticate(
41
+ :email => publisher_username,
42
+ :password => publisher_password
43
+ )
44
+
45
+ db = MachineShop::Database.new
46
+ it "should get all devices for the user" do
47
+ element_data = MachineShop::Device.all(
48
+ {:page => 1,
49
+ :per_page => 10},
50
+ auth_token)
51
+ device = element_data[0]
52
+ #puts "Devices: #{element_data}"
53
+ device.should_not be_nil
54
+ device.should be_kind_of MachineShop::Device
55
+ end
56
+
57
+
58
+
59
+
60
+ it "stores into database" do
61
+ # Database.new
62
+ db = MachineShop::Database.new
63
+ puts "after db =========="
64
+ puts "db_connected ? #{db.db_connected}"
65
+
66
+ if db.db_connected
67
+
68
+ MachineShop::Database.insert('2343','endpoints',"/user/devices/id23")
69
+
70
+ end
71
+
72
+
73
+ end
74
+ end
75
+ =end
76
+
77
+ # describe "Rubysession" do
78
+ # it "should store session " do
79
+ # session[:user_id] = "hello"
80
+ # end
81
+ # end
82
+
83
+ describe MachineShop::User do
84
+ auth_token = nil
85
+ user = nil
86
+
87
+ it "should allow a user to authenticate" do
88
+ auth_token.should be_nil
89
+ auth_token, user = MachineShop::User.authenticate(
90
+ :email => publisher_username,
91
+ :password => publisher_password
92
+ )
93
+
94
+ puts "User Data: #{user}"
95
+ auth_token.should_not be_nil
96
+ user.should_not be_nil
97
+ user.should be_kind_of MachineShop::User
98
+ end
99
+
100
+ it "should get all roles from a static instance" do
101
+ element_data = MachineShop::User.all_roles(auth_token)
102
+
103
+ puts "all_roles: #{element_data}"
104
+ element_data.should_not be_nil
105
+ end
106
+
107
+ it "should get all roles from a user instance" do
108
+
109
+ puts " here user is : #{user}"
110
+ element_data = user.all_roles
111
+
112
+ #puts "all_roles: #{element_data}"
113
+ element_data.should_not be_nil
114
+ end
115
+
116
+ it "should get a user for the user by id" do
117
+ element_data = MachineShop::User.retrieve(user.id, auth_token)
118
+
119
+ #puts "user: #{element_data}"
120
+ element_data.should_not be_nil
121
+ element_data.should be_kind_of MachineShop::User
122
+ end
123
+
124
+ end
125
+
126
+ describe MachineShop::Device do
127
+
128
+ auth_token, user = MachineShop::User.authenticate(
129
+ :email => publisher_username,
130
+ :password => publisher_password
131
+ )
132
+
133
+ device = nil
134
+
135
+ it "should create devices for the user" do
136
+ element_data = MachineShop::Device.create(
137
+ {
138
+ :name => "my_device",
139
+ :type => "Test",
140
+ :manufacturer => "a company",
141
+ :model => "D-vice 1000",
142
+ :active => "yes",
143
+ :init_cmd => "my_init_cmd",
144
+ :init_params => "{'init':'go'}",
145
+ :exe_path => "/etc/foo",
146
+ :unit_price => "$199.99",
147
+ :sample_data => "some arbitrary sample data",
148
+ :long_description => "This device tracks position and NCAA football conference.",
149
+ :image_url => "http://someurl.com/your_image.png",
150
+ :manual_url => "http://someurl.com/manual.pdf"
151
+ },
152
+ auth_token)
153
+
154
+ puts "Element Data: #{element_data}"
155
+ puts "element_data.class: #{element_data.class}"
156
+
157
+ element_data.should_not be_nil
158
+ element_data.should be_kind_of MachineShop::Device
159
+ end
160
+
161
+ it "should get all devices for the user" do
162
+ element_data = MachineShop::Device.all(
163
+ {:page => 1,
164
+ :per_page => 10},
165
+ auth_token)
166
+ device = element_data[0]
167
+ #puts "Devices: #{element_data}"
168
+ device.should_not be_nil
169
+ device.should be_kind_of MachineShop::Device
170
+ end
171
+
172
+ it "should get a device for the user by id" do
173
+ element_data = MachineShop::Device.retrieve(device.id, auth_token)
174
+
175
+ #puts "Devices: #{element_data}"
176
+ element_data.should_not be_nil
177
+ element_data.should be_kind_of MachineShop::Device
178
+ end
179
+
180
+ it "should get a device for the user by name" do
181
+ element_data = MachineShop::Device.all(
182
+ {
183
+ :name => device.name
184
+ },
185
+ auth_token)
186
+
187
+ #puts "Devices: #{element_data}"
188
+ element_data.should_not be_nil
189
+ element_data.should_not be_empty
190
+ end
191
+
192
+ # it "should get a device's payload_fields" do
193
+ # element_data = device.payload_fields
194
+ # element_data.class
195
+ # puts "payload_fields: #{element_data}"
196
+ # puts "element_data.class: #{element_data.class}"
197
+ # element_data.should_not be_nil
198
+ #
199
+ # end
200
+
201
+ end
202
+
203
+ describe MachineShop::DeviceInstance do
204
+
205
+ auth_token, user = MachineShop::User.authenticate(
206
+ :email => publisher_username,
207
+ :password => publisher_password
208
+ )
209
+
210
+ device = nil
211
+ device_instance = nil
212
+
213
+ it "should create a device instance for the user" do
214
+ # First create a device to use
215
+ device = MachineShop::Device.create(
216
+ {
217
+ :name => "my_device",
218
+ :type => "Test",
219
+ :manufacturer => "a company",
220
+ :model => "D-vice 1000",
221
+ :active => "yes",
222
+ :init_cmd => "my_init_cmd",
223
+ :init_params => "{'init':'go'}",
224
+ :exe_path => "/etc/foo",
225
+ :unit_price => "$199.99",
226
+ :sample_data => "some arbitrary sample data",
227
+ :long_description => "This device tracks position and NCAA football conference.",
228
+ :image_url => "http://someurl.com/your_image.png",
229
+ :manual_url => "http://someurl.com/manual.pdf"
230
+ },
231
+ auth_token)
232
+
233
+ puts "device: #{device.inspect}"
234
+
235
+ # Now create an instance
236
+ device_instance = device.create_instance(
237
+ {
238
+ :name => "My little instance",
239
+ :active => "yes"
240
+ }
241
+ )
242
+
243
+ #puts "Device Instance: #{device_instance.inspect}"
244
+ device_instance.should_not be_nil
245
+ device_instance.should be_kind_of MachineShop::DeviceInstance
246
+ end
247
+
248
+
249
+ it "should get device instances" do
250
+ element_data = MachineShop::DeviceInstance.all({}, auth_token)
251
+
252
+ puts "Device Instances: #{element_data}"
253
+
254
+ device_instance = element_data[0]
255
+ element_data.should_not be_nil
256
+ element_data.should_not be_empty
257
+
258
+ device_instance.should be_kind_of MachineShop::DeviceInstance
259
+ end
260
+
261
+ it "should get a device instance by id" do
262
+ element_data = MachineShop::DeviceInstance.retrieve(device_instance.id, auth_token)
263
+
264
+ puts "Device Instance by id: #{element_data}"
265
+
266
+ element_data.should_not be_nil
267
+ element_data.should be_kind_of MachineShop::DeviceInstance
268
+ end
269
+
270
+ it "should get a device instance by name" do
271
+ element_data = MachineShop::DeviceInstance.all({:name => device_instance.name}, auth_token)
272
+
273
+ puts "Device Instance by name: #{element_data}"
274
+
275
+ element_data.should_not be_nil
276
+ element_data.should_not be_empty
277
+ end
278
+
279
+ it "should get all devices via a user" do
280
+ element_data = user.device_instances
281
+
282
+ #puts "Device Instance: #{element_data}"
283
+
284
+ element_data.should_not be_nil
285
+ element_data.should_not be_empty
286
+ end
287
+
288
+ it "should get all devices via a user with a filter" do
289
+ element_data = user.device_instances({:name => device_instance.name})
290
+
291
+ #puts "Device Instance: #{element_data}"
292
+
293
+ element_data.should_not be_nil
294
+ element_data.should_not be_empty
295
+ end
296
+
297
+ end
298
+
299
+ describe MachineShop::Mapping do
300
+
301
+ auth_token, user = MachineShop::User.authenticate(
302
+ :email => publisher_username,
303
+ :password => publisher_password
304
+ )
305
+
306
+ it "should get a geocoded address" do
307
+ element_data = MachineShop::Mapping.geocode(
308
+ {
309
+ :address => "1600 Amphitheatre Parkway, Mountain View, CA",
310
+ :sensor => "false"
311
+ },
312
+ auth_token)
313
+
314
+ #puts "GEO: #{element_data}"
315
+
316
+ element_data.should_not be_nil
317
+ element_data.should_not be_empty
318
+ end
319
+
320
+ it "should get directions" do
321
+ element_data = MachineShop::Mapping.directions(
322
+ {
323
+ :origin => "Denver",
324
+ :destination => "Boston",
325
+ :sensor => "false"
326
+ },
327
+ auth_token)
328
+
329
+ #puts "GEO: #{element_data}"
330
+
331
+ element_data.should_not be_nil
332
+ element_data.should_not be_empty
333
+ end
334
+
335
+ it "should get distance" do
336
+ element_data = MachineShop::Mapping.distance(
337
+ {
338
+ :origins => "Vancouver BC",
339
+ :destinations => "San Francisco",
340
+ :mode => "bicycling",
341
+ :language => "fr-FR",
342
+ :sensor => "false"
343
+ },
344
+ auth_token)
345
+
346
+ #puts "GEO: #{element_data}"
347
+
348
+ element_data.should_not be_nil
349
+ element_data.should_not be_empty
350
+ end
351
+
352
+ end
353
+
354
+ describe MachineShop::Meter do
355
+
356
+ auth_token, user = MachineShop::User.authenticate(
357
+ :email => publisher_username,
358
+ :password => publisher_password
359
+ )
360
+
361
+ it "should get all meter data" do
362
+ element_data = MachineShop::Meter.all({}, auth_token)
363
+
364
+ puts "element_data from all: #{element_data}"
365
+
366
+ element_data.should_not be_nil
367
+ element_data.should_not be_empty
368
+ end
369
+
370
+ it "should get meter by id " do
371
+
372
+ element_data = MachineShop::Meter.retrieve('527ac622ff73462550000001', auth_token)
373
+ puts "meter by id : #{element_data}"
374
+ element_data.should_not be_nil
375
+ end
376
+
377
+ it "should get meters via a user" do
378
+ element_data = user.meters
379
+
380
+ puts "meters via user: #{element_data}"
381
+ element_data.should_not be_nil
382
+ element_data.should_not be_empty
383
+ end
384
+
385
+ end
386
+
387
+ describe MachineShop::Report do
388
+
389
+ auth_token, user = MachineShop::User.authenticate(
390
+ :email => publisher_username,
391
+ :password => publisher_password
392
+ )
393
+
394
+ it "should get all report data" do
395
+ element_data = MachineShop::Report.all({}, auth_token)
396
+
397
+ puts "element_data: #{element_data}"
398
+
399
+ element_data.should_not be_nil
400
+ element_data.should_not be_empty
401
+ end
402
+
403
+
404
+
405
+ it "should get report of specific device" do
406
+ element_data = MachineShop::Report.all(
407
+ ({:device_instance_id => '531f00e5981800ad58000006',
408
+ :per_page=>'1000',
409
+ #:created_at_between=>'2013-11-04T00:00:00_2014-03-19T17:02:00'
410
+ }), auth_token)
411
+
412
+ puts "element data of f00e5981800ad58000006 #{element_data} "
413
+
414
+ element_data.should_not be_nil
415
+ end
416
+
417
+
418
+ end
419
+
420
+ describe MachineShop::Rule do
421
+
422
+ rules=nil
423
+ auth_token, user = MachineShop::User.authenticate(
424
+ :email => publisher_username,
425
+ :password => publisher_password
426
+ )
427
+ it "should get all the rules " do
428
+
429
+ #rules = MachineShop::Rule.new.get_rules(auth_token)
430
+ rules = MachineShop::Rule.all({},auth_token)
431
+ #get_rule
432
+ puts "rules haru : #{rules}"
433
+ rules.should_not be_nil
434
+
435
+
436
+ end
437
+
438
+ create_hash = {
439
+ :devices=>"52585e1d981800bab2000479",
440
+ :device_instances=>{},
441
+ :rule=>{
442
+ :active=>true,
443
+ :description=>"bajratest",
444
+ :condition=>{
445
+ :type=>"and_rule_condition",
446
+ :rule_conditions=>{
447
+
448
+ :property=>"var",
449
+ :value=>"30",
450
+ :type=>"equal_rule_condition"
451
+
452
+ }
453
+ },
454
+ :then_actions=>{
455
+ :priority=>"1",
456
+ :send_to=>"abc@me.com",
457
+ :type=>"email_rule_action"
458
+ }
459
+ }
460
+ }
461
+
462
+ it "should create rule" do
463
+
464
+ ruleById = MachineShop::Rule.retrieve(rules[0].id,auth_token)
465
+ puts "rule by id : #{ruleById}"
466
+ ruleById.should_not be_nil
467
+
468
+ end
469
+
470
+
471
+
472
+ it "should get rule by id" do
473
+ ruleById = MachineShop::Rule.retrieve(rules[0].id,auth_token)
474
+ puts "rule by id : #{ruleById}"
475
+ ruleById.should_not be_nil
476
+
477
+ end
478
+
479
+
480
+
481
+ it "should get get join rule conditions" do
482
+ test_data = MachineShop::Rule.get_join_rule_conditions(auth_token)
483
+ puts "rule comparison : #{test_data.inspect}"
484
+ test_data.should_not be_nil
485
+
486
+ end
487
+
488
+
489
+ it "should get comparison rule_conditions" do
490
+ test_data = MachineShop::Rule.new.get_comparison_rule_conditions(auth_token)
491
+ puts "comparison rule condition : #{test_data.inspect}"
492
+ test_data.should_not be_nil
493
+
494
+ end
495
+
496
+
497
+ it "should get rules deleted" do
498
+ test_data = MachineShop::Rule.get_by_device_instance(auth_token,'52585e1d981800bab2000478')
499
+ puts "rule by_device_instance : #{test_data.inspect}"
500
+ test_data.should_not be_nil
501
+
502
+ end
503
+
504
+ it "should get deleted rule" do
505
+ test_data = MachineShop::Rule.get_deleted(auth_token)
506
+ puts "deleted rule : #{test_data.inspect}"
507
+ test_data.should_not be_nil
508
+
509
+ end
510
+ it "should get create rule" do
511
+ test_data = MachineShop::Rule.create({},auth_token)
512
+ puts "deleted rule : #{test_data.inspect}"
513
+ test_data.should_not be_nil
514
+
515
+ end
516
+
517
+
518
+ end
519
+
520
+ describe MachineShop::Util do
521
+
522
+ auth_token, user = MachineShop::User.authenticate(
523
+ :email => publisher_username,
524
+ :password => publisher_password
525
+ )
526
+
527
+ it "should send an email" do
528
+ element_data = MachineShop::Utility.email(
529
+ {
530
+ :subject => "hello there From the machineshop",
531
+ :body => "The body of an email goes here.\nEscaped chars should work.",
532
+ :to => "niroj@bajratechnologies.com"
533
+ },
534
+ auth_token)
535
+
536
+ puts "element_data: #{element_data}"
537
+
538
+ #element_data[:http_code].should be(200)
539
+ end
540
+
541
+ it "should send an sms" do
542
+ element_data = MachineShop::Utility.sms(
543
+ {
544
+ :message => "This is a text from the platform",
545
+ :to => "13035551212"
546
+ },
547
+ auth_token)
548
+
549
+ #puts "element_data: #{element_data}"
550
+
551
+ element_data[:http_code].should be(200)
552
+ end
553
+
554
+ end
555
+
556
+ describe MachineShop::Customer do
557
+
558
+ #update = MachineShop::Customer.update
559
+
560
+
561
+
562
+
563
+ auth_token , user = MachineShop::User.authenticate(
564
+ :email => publisher_username,
565
+ :password => publisher_password
566
+ )
567
+
568
+ customers = nil
569
+
570
+ it "should get all the customers " do
571
+ customers = MachineShop::Customer.all({}, auth_token)
572
+
573
+ puts "customers are #{customers}"
574
+
575
+ #puts "first customer is : #{customers[0][:id]}"
576
+
577
+ customers.should_not be_nil
578
+ end
579
+
580
+
581
+ it "should create customer " do
582
+
583
+ customer = MachineShop::Customer.create({:email=>"gvhfbs@bajratechnologies.com",
584
+ :password=>'password',
585
+ :notification_method=>'sms',
586
+ :first_name=>'niroj',:last_name=>'sapkota',
587
+ :phone_number=>'98989898989',
588
+ :company_name=>'technology co'
589
+
590
+ },auth_token)
591
+ puts "created customer is #{customer}"
592
+ customer.should_not be_nil
593
+ end
594
+
595
+
596
+ specificCustomer = nil
597
+ it "should get customer by customer id " do
598
+
599
+ specificCustomer = MachineShop::Customer.retrieve(customers[0].id, auth_token)
600
+ puts "customer id- #{customers[0].id} is #{specificCustomer}"
601
+ specificCustomer.should_not be_nil
602
+ end
603
+
604
+
605
+
606
+ #success test
607
+
608
+ it "should delete customer with id " do
609
+
610
+ #puts
611
+ puts "deleting customer with id : #{customers[0].id}"
612
+
613
+ delete = specificCustomer.delete
614
+ #delete = MachineShop::Customer.delete(customers[0].id,auth_token)
615
+ puts "delete #{delete}"
616
+ delete.http_code.should eq 200
617
+ end
618
+
619
+
620
+
621
+ it "should update the customer with id " do
622
+
623
+ puts "updating customer with id : #{customers[0].id}"
624
+ update = MachineShop::Customer.update(customers[0].id,auth_token,{:notification_method => 'email'})
625
+ puts "update #{update}"
626
+ end
627
+ end
628
+