machineshop 0.0.1

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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/.name +1 -0
  3. data/.idea/.rakeTasks +7 -0
  4. data/.idea/compiler.xml +23 -0
  5. data/.idea/copyright/profiles_settings.xml +5 -0
  6. data/.idea/encodings.xml +5 -0
  7. data/.idea/inspectionProfiles/Project_Default.xml +7 -0
  8. data/.idea/inspectionProfiles/profiles_settings.xml +7 -0
  9. data/.idea/machineshop.iml +194 -0
  10. data/.idea/misc.xml +5 -0
  11. data/.idea/modules.xml +9 -0
  12. data/.idea/scopes/scope_settings.xml +5 -0
  13. data/.idea/vcs.xml +7 -0
  14. data/.idea/workspace.xml +722 -0
  15. data/Gemfile +8 -0
  16. data/LICENSE +22 -0
  17. data/README.md +29 -0
  18. data/Rakefile +8 -0
  19. data/doc.txt +50 -0
  20. data/lib/machineshop.rb +234 -0
  21. data/lib/machineshop/api_operations/create.rb +17 -0
  22. data/lib/machineshop/api_operations/delete.rb +11 -0
  23. data/lib/machineshop/api_operations/list.rb +16 -0
  24. data/lib/machineshop/api_operations/update.rb +17 -0
  25. data/lib/machineshop/api_resource.rb +35 -0
  26. data/lib/machineshop/configuration.rb +11 -0
  27. data/lib/machineshop/customer.rb +9 -0
  28. data/lib/machineshop/database.rb +112 -0
  29. data/lib/machineshop/device.rb +30 -0
  30. data/lib/machineshop/device_instance.rb +30 -0
  31. data/lib/machineshop/errors/api_error.rb +4 -0
  32. data/lib/machineshop/errors/authentication_error.rb +4 -0
  33. data/lib/machineshop/errors/database_error.rb +5 -0
  34. data/lib/machineshop/errors/invalid_request_error.rb +10 -0
  35. data/lib/machineshop/errors/machineshop_error.rb +20 -0
  36. data/lib/machineshop/json.rb +21 -0
  37. data/lib/machineshop/machineshop_cache.rb +49 -0
  38. data/lib/machineshop/machineshop_object.rb +164 -0
  39. data/lib/machineshop/mapping.rb +34 -0
  40. data/lib/machineshop/meter.rb +12 -0
  41. data/lib/machineshop/models/people.rb +11 -0
  42. data/lib/machineshop/report.rb +11 -0
  43. data/lib/machineshop/rule.rb +58 -0
  44. data/lib/machineshop/user.rb +43 -0
  45. data/lib/machineshop/util.rb +115 -0
  46. data/lib/machineshop/utility.rb +30 -0
  47. data/lib/machineshop/version.rb +3 -0
  48. data/machineshop.gemspec +24 -0
  49. data/spec/lib/api_calls_spec.rb +628 -0
  50. data/spec/lib/customer_spec.rb +76 -0
  51. data/spec/lib/device_spec.rb +179 -0
  52. data/spec/lib/mapping_spec.rb +64 -0
  53. data/spec/lib/meter_spec.rb +46 -0
  54. data/spec/lib/report_spec.rb +52 -0
  55. data/spec/lib/rule_spec.rb +117 -0
  56. data/spec/lib/user_spec.rb +53 -0
  57. data/spec/spec_helper.rb +3 -0
  58. metadata +184 -0
@@ -0,0 +1,34 @@
1
+ module MachineShop
2
+ class Mapping
3
+ # Specific API calls
4
+ def self.geocode(params={}, auth_token)
5
+ MachineShop.get(geocode_url, auth_token, params)
6
+ end
7
+
8
+ def self.directions(params={}, auth_token)
9
+ MachineShop.get(directions_url, auth_token, params)
10
+ end
11
+
12
+ def self.distance(params={}, auth_token)
13
+ MachineShop.get(distance_url, auth_token, params)
14
+ end
15
+
16
+ private
17
+
18
+ def self.geocode_url
19
+ url + '/geocode/json'
20
+ end
21
+
22
+ def self.directions_url
23
+ url + '/directions/json'
24
+ end
25
+
26
+ def self.distance_url
27
+ url + '/distance/json'
28
+ end
29
+
30
+ def self.url
31
+ '/platform/google'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module MachineShop
2
+ class Meter < APIResource
3
+ include MachineShop::APIOperations::List
4
+
5
+ def self.url()
6
+ ret = "/platform/data/#{CGI.escape(class_name.underscore)}"
7
+ ret
8
+ end
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,11 @@
1
+ module MachineShop
2
+ # require 'active_record'
3
+
4
+ # require 'machineshop/database'
5
+
6
+ # Database.new
7
+
8
+ class People
9
+ # end
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ module MachineShop
2
+ class Report < APIResource
3
+ include MachineShop::APIOperations::List
4
+
5
+ def self.url()
6
+ ret = "/monitor/#{CGI.escape(class_name.underscore)}"
7
+ ret
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ module MachineShop
2
+ class Rule < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+
7
+ =begin
8
+ # Specific API calls
9
+ def get_rules(auth_token)
10
+ url = platform_url + "/rule"
11
+ MachineShop.get(url,auth_token)
12
+ #get url, auth_token
13
+ end
14
+ =end
15
+
16
+ def self.platform_url
17
+ '/platform'
18
+ end
19
+
20
+ =begin
21
+ def get_rule(auth_token, id)
22
+ url = platform_url + "/rule/#{id}"
23
+ MachineShop.get(url, auth_token)
24
+ end
25
+ =end
26
+
27
+ def self.get_join_rule_conditions(auth_token)
28
+ url = platform_url + "/rule/join_rule_conditions"
29
+ MachineShop.get(url, auth_token)
30
+ end
31
+
32
+ def self.get_comparison_rule_conditions(auth_token)
33
+ url = platform_url + "/rule/comparison_rule_conditions"
34
+ MachineShop.get(url, auth_token)
35
+ end
36
+
37
+ def self.get_deleted(auth_token)
38
+ url = platform_url + "/rules/deleted"
39
+ MachineShop.get(url, auth_token)
40
+ end
41
+
42
+ def self.get_by_device_instance(auth_token,id)
43
+ url = platform_url + "/rule/device_instance/#{id}"
44
+ MachineShop.get(url, auth_token)
45
+ end
46
+
47
+ def post_rule(auth_token, rule_hash)
48
+ url = platform_url + "/rule"
49
+ MachineShop.post(url, auth_token, rule_hash)
50
+ end
51
+
52
+ def delete_rule(auth_token, id)
53
+ url = platform_url + "/rule/#{id}"
54
+ MachineShop.delete(url, auth_token)
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,43 @@
1
+ module MachineShop
2
+ class User < APIResource
3
+
4
+ # Specific API calls
5
+ def self.authenticate(user_hash)
6
+ #user_hash is => { email: email, password: password }
7
+ response = MachineShop.post(authenticate_url, nil, user_hash)
8
+ auth_token = response[:authentication_token]
9
+ id = response[:_id]
10
+
11
+ return auth_token, self.retrieve(id, auth_token)
12
+ end
13
+
14
+ def self.all_roles(auth_token)
15
+ MachineShop.get(self.role_url, auth_token)
16
+ end
17
+
18
+ def all_roles
19
+ MachineShop.get(self.class.role_url, @auth_token)
20
+ end
21
+
22
+ def device_instances(filters={})
23
+ filters.merge!(:user_id => self.id)
24
+ MachineShop::DeviceInstance.all(filters, @auth_token)
25
+ end
26
+
27
+ def meters(filters={})
28
+ filters.merge!(:user_id => self.id)
29
+ MachineShop::Meter.all(filters, @auth_token)
30
+ end
31
+
32
+ private
33
+
34
+ def self.authenticate_url
35
+ '/user_session/user/authenticate'
36
+ end
37
+
38
+ def self.role_url
39
+ '/platform/role'
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,115 @@
1
+ module MachineShop
2
+ module Util
3
+ def self.objects_to_ids(h)
4
+ case h
5
+ when APIResource
6
+ h.id
7
+ when Hash
8
+ res = {}
9
+ h.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
10
+ res
11
+ when Array
12
+ h.map { |v| objects_to_ids(v) }
13
+ else
14
+ h
15
+ end
16
+ end
17
+
18
+ def self.convert_to_machineshop_object(resp, auth_token, type=nil)
19
+ types = {
20
+ 'Device' => Device,
21
+ 'DeviceInstance' => DeviceInstance,
22
+ 'Mapping' => Mapping,
23
+ 'Meter' => Meter,
24
+ 'Report' => Report,
25
+ 'Rule' => Rule,
26
+ 'User' => User,
27
+ 'Utility' => Utility,
28
+ 'Customer'=> Customer
29
+ }
30
+ case resp
31
+ when Array
32
+ resp.map { |i| convert_to_machineshop_object(i, auth_token, type) }
33
+ when Hash
34
+ # Try converting to a known object class. If none available, fall back to generic APIResource
35
+ if klass_name = type
36
+ klass = types[klass_name]
37
+ end
38
+ klass ||= MachineShopObject
39
+ klass.construct_from(resp, auth_token)
40
+ else
41
+ resp
42
+ end
43
+ end
44
+
45
+ def self.file_readable(file)
46
+ begin
47
+ File.open(file) { |f| }
48
+ rescue
49
+ false
50
+ else
51
+ true
52
+ end
53
+ end
54
+
55
+ def self.symbolize_names(object)
56
+ case object
57
+ when Hash
58
+ new = {}
59
+ object.each do |key, value|
60
+ key = (key.to_sym rescue key) || key
61
+ new[key] = symbolize_names(value)
62
+ end
63
+ new
64
+ when Array
65
+ object.map { |value| symbolize_names(value) }
66
+ else
67
+ object
68
+ end
69
+ end
70
+
71
+ def self.url_encode(key)
72
+ URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
73
+ end
74
+
75
+ def self.flatten_params(params, parent_key=nil)
76
+ result = []
77
+ params.each do |key, value|
78
+ calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
79
+ if value.is_a?(Hash)
80
+ result += flatten_params(value, calculated_key)
81
+ elsif value.is_a?(Array)
82
+ result += flatten_params_array(value, calculated_key)
83
+ else
84
+ result << [calculated_key, value]
85
+ end
86
+ end
87
+ result
88
+ end
89
+
90
+ def self.flatten_params_array(value, calculated_key)
91
+ result = []
92
+ value.each do |elem|
93
+ if elem.is_a?(Hash)
94
+ result += flatten_params(elem, calculated_key)
95
+ elsif elem.is_a?(Array)
96
+ result += flatten_params_array(elem, calculated_key)
97
+ else
98
+ result << ["#{calculated_key}[]", elem]
99
+ end
100
+ end
101
+ result
102
+ end
103
+ end
104
+ end
105
+
106
+ class String
107
+ def underscore
108
+ self.gsub(/::/, '/').
109
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
110
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
111
+ tr("-", "_").
112
+ downcase
113
+ end
114
+ end
115
+
@@ -0,0 +1,30 @@
1
+ module MachineShop
2
+ class Utility < APIResource
3
+ def self.email(params={}, auth_token)
4
+ MachineShop.post(email_url, auth_token, params)
5
+ end
6
+
7
+ def self.sms(params={}, auth_token)
8
+ MachineShop.post(sms_url, auth_token, params)
9
+ end
10
+
11
+ private
12
+
13
+ def self.email_url
14
+ url + '/email'
15
+ end
16
+
17
+ def self.sms_url
18
+ url + '/SMS'
19
+ end
20
+
21
+ def refresh
22
+ raise NotImplementedError.new('Utility is a helper api. You should not perform this action on it')
23
+ end
24
+
25
+ def self.retrieve(id, auth_token=nil)
26
+ raise NotImplementedError.new('Utility is a helper api. You should not perform this action on it')
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module MachineShop
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/machineshop/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["machineshop"]
6
+ gem.email = ["john@mach19.com"]
7
+ gem.description = %q{Wraps the machineshop API.}
8
+ gem.summary = %q{A convenient way to call into the machineshop API.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "machineshop"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MachineShop::VERSION
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+
21
+ gem.add_dependency 'addressable'
22
+ gem.add_dependency 'rest-client'#, '~> 1.6.7'
23
+ gem.add_dependency('multi_json', '>= 1.0.4', '< 2')
24
+ end
@@ -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
+