test_machine_shop 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) 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 +9 -0
  16. data/LICENSE +22 -0
  17. data/README.md +977 -0
  18. data/Rakefile +8 -0
  19. data/doc.txt +50 -0
  20. data/lib/machineshop.rb +475 -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 +11 -0
  25. data/lib/machineshop/api_resource.rb +35 -0
  26. data/lib/machineshop/configuration.rb +14 -0
  27. data/lib/machineshop/customer.rb +15 -0
  28. data/lib/machineshop/data_source_types.rb +28 -0
  29. data/lib/machineshop/data_sources.rb +35 -0
  30. data/lib/machineshop/database.rb +59 -0
  31. data/lib/machineshop/device.rb +30 -0
  32. data/lib/machineshop/device_instance.rb +30 -0
  33. data/lib/machineshop/end_points.rb +30 -0
  34. data/lib/machineshop/errors/api_connection_error.rb +4 -0
  35. data/lib/machineshop/errors/api_error.rb +4 -0
  36. data/lib/machineshop/errors/authentication_error.rb +4 -0
  37. data/lib/machineshop/errors/database_error.rb +5 -0
  38. data/lib/machineshop/errors/invalid_request_error.rb +10 -0
  39. data/lib/machineshop/errors/machineshop_error.rb +20 -0
  40. data/lib/machineshop/errors/schema_error.rb +5 -0
  41. data/lib/machineshop/json.rb +21 -0
  42. data/lib/machineshop/machineshop_cache.rb +49 -0
  43. data/lib/machineshop/machineshop_object.rb +165 -0
  44. data/lib/machineshop/mapping.rb +34 -0
  45. data/lib/machineshop/meter.rb +12 -0
  46. data/lib/machineshop/models/api_endpoint.rb +8 -0
  47. data/lib/machineshop/models/api_request.rb +28 -0
  48. data/lib/machineshop/models/schema.rb +184 -0
  49. data/lib/machineshop/report.rb +11 -0
  50. data/lib/machineshop/rule.rb +42 -0
  51. data/lib/machineshop/user.rb +43 -0
  52. data/lib/machineshop/users.rb +43 -0
  53. data/lib/machineshop/util.rb +193 -0
  54. data/lib/machineshop/utility.rb +30 -0
  55. data/lib/machineshop/version.rb +3 -0
  56. data/machineshop.gemspec +35 -0
  57. data/spec/lib/api_calls_spec.rb +628 -0
  58. data/spec/lib/custom_endpoint_test.rb +78 -0
  59. data/spec/lib/customer_spec.rb +87 -0
  60. data/spec/lib/data_source.rb +138 -0
  61. data/spec/lib/data_source_type_spec.rb +77 -0
  62. data/spec/lib/database_spec.rb +45 -0
  63. data/spec/lib/device_instances.rb +73 -0
  64. data/spec/lib/device_spec.rb +172 -0
  65. data/spec/lib/endpoint_spec.rb +37 -0
  66. data/spec/lib/geocode_spec +45 -0
  67. data/spec/lib/mapping_spec.rb +68 -0
  68. data/spec/lib/meter_spec.rb +49 -0
  69. data/spec/lib/report_spec.rb +52 -0
  70. data/spec/lib/rule_spec.rb +130 -0
  71. data/spec/lib/user_spec.rb +58 -0
  72. data/spec/spec_helper.rb +12 -0
  73. metadata +235 -0
@@ -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,42 @@
1
+ module MachineShop
2
+ class Rule < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+
7
+ def self.platform_url
8
+ '/platform'
9
+ end
10
+
11
+ def self.get_join_rule_conditions(auth_token)
12
+ url = platform_url + "/rule/join_rule_conditions"
13
+ MachineShop.gem_get(url, auth_token)
14
+ end
15
+
16
+ def self.get_comparison_rule_conditions(auth_token)
17
+ url = platform_url + "/rule/comparison_rule_conditions"
18
+ MachineShop.gem_get(url, auth_token)
19
+ end
20
+
21
+ def self.get_deleted(auth_token)
22
+ url = platform_url + "/rules/deleted"
23
+ MachineShop.gem_get(url, auth_token)
24
+ end
25
+
26
+ def self.get_by_device_instance(auth_token,id)
27
+ url = platform_url + "/rule/device_instance/#{id}"
28
+ MachineShop.gem_get(url, auth_token)
29
+ end
30
+
31
+ def post_rule(auth_token, rule_hash)
32
+ url = platform_url + "/rule"
33
+ MachineShop.gem_post(url, auth_token, rule_hash)
34
+ end
35
+
36
+ def delete_rule(auth_token, id)
37
+ url = platform_url + "/rule/#{id}"
38
+ MachineShop.gem_delete(url, auth_token)
39
+ end
40
+
41
+ end
42
+ 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.gem_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.gem_get(self.role_url, auth_token)
16
+ end
17
+
18
+ def all_roles
19
+ MachineShop.gem_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,43 @@
1
+ module MachineShop
2
+ class Users < APIResource
3
+
4
+ # Specific API calls
5
+ def self.authenticate(user_hash)
6
+ #user_hash is => { email: email, password: password }
7
+ response = MachineShop.gem_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.gem_get(self.role_url, auth_token)
16
+ end
17
+
18
+ def all_roles
19
+ MachineShop.gem_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,193 @@
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
+ 'Users' => Users,
28
+ 'Utility' => Utility,
29
+ 'Customer'=> Customer
30
+ }
31
+ case resp
32
+ when Array
33
+ resp.map { |i| convert_to_machineshop_object(i, auth_token, type) }
34
+ when Hash
35
+ # Try converting to a known object class. If none available, fall back to generic APIResource
36
+ if klass_name = type
37
+ klass = types[klass_name]
38
+ end
39
+ klass ||= MachineShopObject
40
+ klass.construct_from(resp, auth_token)
41
+ else
42
+ resp
43
+ end
44
+ end
45
+
46
+ def self.file_readable(file)
47
+ begin
48
+ File.open(file) { |f| }
49
+ rescue
50
+ false
51
+ else
52
+ true
53
+ end
54
+ end
55
+
56
+ def self.symbolize_names(object)
57
+ case object
58
+ when Hash
59
+ new = {}
60
+ object.each do |key, value|
61
+ key = (key.to_sym rescue key) || key
62
+ new[key] = symbolize_names(value)
63
+ end
64
+ new
65
+ when Array
66
+ object.map { |value| symbolize_names(value) }
67
+ else
68
+ object
69
+ end
70
+ end
71
+
72
+ def self.url_encode(key)
73
+ URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
74
+ end
75
+
76
+ def self.flatten_params(params, parent_key=nil)
77
+ result = []
78
+ params.each do |key, value|
79
+ calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
80
+ if value.is_a?(Hash)
81
+ result += flatten_params(value, calculated_key)
82
+ elsif value.is_a?(Array)
83
+ result += flatten_params_array(value, calculated_key)
84
+ else
85
+ result << [calculated_key, value]
86
+ end
87
+ end
88
+ result
89
+ end
90
+
91
+ def self.flatten_params_array(value, calculated_key)
92
+ result = []
93
+ value.each do |elem|
94
+ if elem.is_a?(Hash)
95
+ result += flatten_params(elem, calculated_key)
96
+ elsif elem.is_a?(Array)
97
+ result += flatten_params_array(elem, calculated_key)
98
+ else
99
+ result << ["#{calculated_key}[]", elem]
100
+ end
101
+ end
102
+ result
103
+ end
104
+
105
+ def self.get_klass_from_url(url)
106
+ id=nil
107
+ klass=nil
108
+ splitted = url.split('/')
109
+ klass = splitted[-1]
110
+ if /[0-9]/.match(klass)
111
+ id=splitted[-1]
112
+
113
+ if splitted[-3]=="rule"
114
+ klass="rule"
115
+ else
116
+ klass = splitted[-2]
117
+ end
118
+ end
119
+ return id,klass
120
+ end
121
+
122
+ #Check if db_connected
123
+ def self.db_connected?
124
+ db_connected = true
125
+ begin
126
+ MachineShop::Database.new
127
+ rescue DatabaseError =>e
128
+ # puts e.message
129
+ db_connected= false
130
+ rescue SchemaError =>e
131
+ # puts e.message
132
+ # db_connected=true
133
+ end
134
+
135
+ db_connected
136
+ end
137
+
138
+
139
+ def self.valid_endpoint(name,auth_token,verb,params)
140
+ if Util.db_connected?
141
+ endpoints_upto_date=false
142
+
143
+ ApiRequest.cache("/gem/routes/#{MachineShop.configuration.base_version}", auth_token, MachineShop.configuration.custom_endpoints_cache_time) do
144
+ #time not expired, cached routes are oke
145
+ endpoints_upto_date = true
146
+ end
147
+
148
+ if !endpoints_upto_date
149
+ #request the endpoints
150
+ MachineShop::EndPoints.all(MachineShop.configuration.base_version,auth_token)
151
+
152
+ end
153
+
154
+ endpoint = ApiEndpoint.where(:verb=>verb).where(:auth_token=>auth_token).where("api_endpoints.endpoint LIKE :endpoint", {:endpoint => "/#{name}%"}).take(1)
155
+ if !endpoint.empty?
156
+ splitEndpoints = (endpoint[0].endpoint).split("/").reject{|val| val=="" }
157
+ if splitEndpoints[0]==name
158
+ url="/"
159
+ key=0
160
+ splitEndpoints.each do |v|
161
+ if v.start_with? ":" #parameter
162
+ if params[key]
163
+ url+=params[key]
164
+ key+=1
165
+ else
166
+ raise APIError.new("Invalid parameters, Please provide value for #{v}")
167
+ end
168
+ else
169
+ url+="#{v}/"
170
+ end
171
+ end
172
+ end
173
+ return url
174
+ else
175
+ raise APIError.new("Invalid url request")
176
+ return false
177
+ end
178
+ end
179
+ end
180
+
181
+ end
182
+ end
183
+
184
+ class String
185
+ def underscore
186
+ self.gsub(/::/, '/').
187
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
188
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
189
+ tr("-", "_").
190
+ downcase
191
+ end
192
+ end
193
+
@@ -0,0 +1,30 @@
1
+ module MachineShop
2
+ class Utility < APIResource
3
+ def self.email(params={}, auth_token)
4
+ MachineShop.gem_post(email_url, auth_token, params)
5
+ end
6
+
7
+ def self.sms(params={}, auth_token)
8
+ MachineShop.gem_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.4"
3
+ end
@@ -0,0 +1,35 @@
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 = "test_machine_shop"
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_development_dependency 'mysql'
22
+
23
+ # gem.add_runtime_dependency 'mysql'
24
+ gem.add_dependency 'mysql'
25
+
26
+ gem.add_dependency 'addressable'
27
+ gem.add_dependency 'rest-client'#, '~> 1.6.7'
28
+ gem.add_dependency('multi_json', '>= 1.0.4', '< 2')
29
+ # gem.add_dependency('activerecord', '>= 4.1.1')
30
+ gem.add_dependency('activerecord', '>= 4.0.0')
31
+ gem.required_ruby_version = '>= 1.9.3'
32
+ gem.post_install_message = "Thanks for installing Machineshop gem, Enjoy!!"
33
+ gem.requirements << 'mysql, activerecord'
34
+
35
+ 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
+