Module: MachineShop
- Defined in:
- lib/machineshop/api_operations/create.rb,
lib/machineshop.rb,
lib/machineshop/json.rb,
lib/machineshop/rule.rb,
lib/machineshop/user.rb,
lib/machineshop/util.rb,
lib/machineshop/meter.rb,
lib/machineshop/users.rb,
lib/machineshop/device.rb,
lib/machineshop/report.rb,
lib/machineshop/mapping.rb,
lib/machineshop/utility.rb,
lib/machineshop/version.rb,
lib/machineshop/customer.rb,
lib/machineshop/database.rb,
lib/machineshop/end_points.rb,
lib/machineshop/api_resource.rb,
lib/machineshop/data_sources.rb,
lib/machineshop/configuration.rb,
lib/machineshop/device_instance.rb,
lib/machineshop/errors/api_error.rb,
lib/machineshop/data_source_types.rb,
lib/machineshop/machineshop_cache.rb,
lib/machineshop/machineshop_object.rb,
lib/machineshop/api_operations/list.rb,
lib/machineshop/errors/schema_error.rb,
lib/machineshop/api_operations/delete.rb,
lib/machineshop/api_operations/update.rb,
lib/machineshop/errors/database_error.rb,
lib/machineshop/errors/machineshop_error.rb,
lib/machineshop/errors/api_connection_error.rb,
lib/machineshop/errors/authentication_error.rb,
lib/machineshop/errors/invalid_request_error.rb
Overview
Defining class method inside a module
Defined Under Namespace
Modules: APIOperations, JSON, Util
Classes: APIConnectionError, APIError, APIResource, AuthenticationError, Configuration, Customer, DataSourceTypes, DataSources, Database, DatabaseError, Device, DeviceInstance, EndPoints, InvalidRequestError, MachineShopError, MachineShopObject, MachineshopCache, Mapping, Meter, Report, Rule, SchemaError, User, Users, Utility
Constant Summary
collapse
- TABLE_NAME_BLACKLIST =
["user","routes"]
- QUERY_STRING_BLACKLIST =
[
'page',
'per_page'
]
- VERSION =
"0.0.4"
- @@api_base_url =
'http://stage.services.machineshop.io/api/v0'
Class Attribute Summary collapse
Class Method Summary
collapse
-
.api_base_url ⇒ Object
-
.api_base_url=(api_base_url) ⇒ Object
-
.api_error(error, rcode, rbody, error_obj) ⇒ Object
-
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
-
.class_exists?(class_name) ⇒ Boolean
Check if the class with the variable exists.
-
.configure {|configuration| ... } ⇒ Object
-
.delete(name, auth_token, *params) ⇒ Object
-
.execute_request(opts) ⇒ Object
-
.gem_delete(url, auth_token, body_hash) ⇒ Object
-
.gem_get(url, auth_token, body_hash = nil) ⇒ Object
Call for the predefined request.
-
.gem_post(url, auth_token, body_hash) ⇒ Object
-
.gem_put(url, auth_token, body_hash) ⇒ Object
-
.get(name, auth_token, *params) ⇒ Object
-
.get_from_cache(url, body_hash, auth_token) ⇒ Object
-
.handle_api_error(rcode, rbody) ⇒ Object
-
.handle_restclient_error(e) ⇒ Object
-
.headers(auth_token) ⇒ Object
-
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
-
.parse_query_string(query_params, auth_token) ⇒ Object
-
.platform_request(url, auth_token, body_hash = nil, http_verb = :get) ⇒ Object
-
.post(name, auth_token, body_hash) ⇒ Object
-
.put(name, auth_token, *params, body_hash) ⇒ Object
-
.reset ⇒ Object
-
.save_into_cache(url, data, auth_token) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
67
68
69
|
# File 'lib/machineshop.rb', line 67
def configuration
@configuration ||= Configuration.new
end
|
Class Method Details
.api_base_url ⇒ Object
86
87
88
|
# File 'lib/machineshop.rb', line 86
def api_base_url
@@api_base_url
end
|
.api_base_url=(api_base_url) ⇒ Object
82
83
84
|
# File 'lib/machineshop.rb', line 82
def api_base_url=(api_base_url)
@@api_base_url = api_base_url
end
|
.api_error(error, rcode, rbody, error_obj) ⇒ Object
266
267
268
|
# File 'lib/machineshop.rb', line 266
def api_error(error, rcode, rbody, error_obj)
APIError.new(error, rcode, rbody, error_obj)
end
|
.authentication_error(error, rcode, rbody, error_obj) ⇒ Object
262
263
264
|
# File 'lib/machineshop.rb', line 262
def authentication_error(error, rcode, rbody, error_obj)
AuthenticationError.new(error, rcode, rbody, error_obj)
end
|
.class_exists?(class_name) ⇒ Boolean
Check if the class with the variable exists
288
289
290
291
292
293
|
# File 'lib/machineshop.rb', line 288
def class_exists?(class_name)
klass = class_name.constantize
return klass.is_a?(Class)
rescue NameError =>e
return false
end
|
71
72
73
|
# File 'lib/machineshop.rb', line 71
def configure
yield(configuration)
end
|
.delete(name, auth_token, *params) ⇒ Object
106
107
108
109
|
# File 'lib/machineshop.rb', line 106
def delete(name,auth_token,*params)
url = Util.valid_endpoint(name,auth_token,:delete,params)
platform_request(url, auth_token, nil ,:delete)
end
|
.execute_request(opts) ⇒ Object
233
234
235
|
# File 'lib/machineshop.rb', line 233
def execute_request(opts)
RestClient::Request.execute(opts)
end
|
.gem_delete(url, auth_token, body_hash) ⇒ Object
120
121
122
|
# File 'lib/machineshop.rb', line 120
def gem_delete(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :delete)
end
|
.gem_get(url, auth_token, body_hash = nil) ⇒ Object
Call for the predefined request
112
113
114
|
# File 'lib/machineshop.rb', line 112
def gem_get(url, auth_token, body_hash=nil)
platform_request(url, auth_token, body_hash)
end
|
.gem_post(url, auth_token, body_hash) ⇒ Object
116
117
118
|
# File 'lib/machineshop.rb', line 116
def gem_post(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :post)
end
|
.gem_put(url, auth_token, body_hash) ⇒ Object
124
125
126
|
# File 'lib/machineshop.rb', line 124
def gem_put(url, auth_token, body_hash)
platform_request(url, auth_token, body_hash, :put)
end
|
.get(name, auth_token, *params) ⇒ Object
91
92
93
94
|
# File 'lib/machineshop.rb', line 91
def get(name, auth_token, *params)
url = Util.valid_endpoint(name,auth_token,:get, params)
platform_request(url, auth_token, nil, :get)
end
|
.get_from_cache(url, body_hash, auth_token) ⇒ Object
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
# File 'lib/machineshop.rb', line 390
def get_from_cache(url, body_hash,auth_token)
result =Array.new
id,klass= Util.get_klass_from_url(url)
if !TABLE_NAME_BLACKLIST.include?(klass)
if Util.db_connected?
klass = klass.capitalize+"Cache"
modelClass = Object.const_set klass, Class.new(ActiveRecord::Base)
modelClass.inheritance_column = :_type_disabled
data_exist=false
if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
puts "db:table #{klass.pluralize} exists"
resp= nil
if id
resp = modelClass.find_by(_id: id, auth_token: auth_token)
data_exist=true if resp
else
resp = modelClass.where(parse_query_string(body_hash,auth_token))
data_exist = true if resp.exists?
end
if data_exist
if(klass.include?("rule_condition"))
resp.each do |rTemp|
temp = Array.new
temp.push rTemp["rule_description"]
temp.push rTemp["rule_condition"]
result << temp
end
result = result.to_json(:except=>[:id]) if result
else
result = resp.to_json(:except=>[:id]) if resp
end
end
end
end
end
return result
end
|
.handle_api_error(rcode, rbody) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/machineshop.rb', line 237
def handle_api_error(rcode, rbody)
begin
error_obj = MachineShop::JSON.load(rbody)
error_obj = Util.symbolize_names(error_obj)
error = error_obj[:error] or raise MachineShopError.new
rescue MultiJson::DecodeError, MachineShopError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
case rcode
when 400, 404 then
raise invalid_request_error(error, rcode, rbody, error_obj)
when 401
raise authentication_error(error, rcode, rbody, error_obj)
when 402
else
raise api_error(error, rcode, rbody, error_obj)
end
end
|
.handle_restclient_error(e) ⇒ Object
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/machineshop.rb', line 270
def handle_restclient_error(e)
case e
when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
message = "Could not connect to MachineShop (#{@@api_base_url}). Please check your internet connection and try again. If this problem persists, you should check MachineShop's service status."
when RestClient::SSLCertificateNotVerified
message = "Could not verify MachineShops's SSL certificate. Please make sure that your network is not intercepting certificates."
when SocketError
message = "Unexpected error communicating when trying to connect to MachineShop (#{@@api_base_url}). HINT: You may be seeing this message because your DNS is not working."
else
message = "Unexpected error communicating with MachineShop"
end
message += "\n\n(Network error: #{e.message})"
raise APIConnectionError.new(message)
end
|
128
129
130
131
132
133
|
# File 'lib/machineshop.rb', line 128
def (auth_token)
={:content_type => :json,
:accept => :json}
.merge!({ authorization: "Basic " + Base64.encode64(auth_token + ':X') }) if auth_token
end
|
.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object
258
259
260
|
# File 'lib/machineshop.rb', line 258
def invalid_request_error(error, rcode, rbody, error_obj)
InvalidRequestError.new(error, error, rcode, rbody, error_obj)
end
|
.parse_query_string(query_params, auth_token) ⇒ Object
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
# File 'lib/machineshop.rb', line 440
def parse_query_string(query_params,auth_token)
search_parms = {}
if query_params
params = Hash[query_params.map{ |k, v| [k.to_s, v] }]
operators = ["gt", "gte", "lt", "lte"]
xs = params.reject { |k,_| QUERY_STRING_BLACKLIST.include?(k) }
xs.each do |key,value|
tokens = key.split('_')
if tokens.nil? || tokens.length == 1
search_parms[key] = value
else
token_length = tokens[tokens.length-1].length-1
if operators.include?(tokens[tokens.length-1])
operator = "$" + tokens[tokens.length-1]
new_key = key[0,key.length-token_length-2].to_sym
search_parms[new_key] = { operator => value }
elsif tokens[tokens.length-1] == "between" && value.split("_").length > 1
new_key = key[0,key.length-token_length-2].to_sym
vals = value.split("_")
search_parms[new_key] = {"$gte" => vals[0], "$lte" => vals[1]}
else
search_parms[key] = value
end
end
end
end
search_parms['auth_token']=auth_token
search_parms
end
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/machineshop.rb', line 135
def platform_request(url, auth_token, body_hash=nil, http_verb=:get )
rbody=nil
cachedContent = :true
if http_verb==:get
if Util.db_connected?
xpired=true
ApiRequest.cache(url, auth_token, MachineShop.configuration.expiry_time) do
xpired=false
puts "Not expired , calling from local "
rbody = get_from_cache(url,body_hash,auth_token)
rcode="200"
end
end
end
if (rbody.nil? || rbody.empty?)
cachedContent=:false
opts = nil
api_uri = api_base_url + url
= self.(auth_token)
if http_verb == :get
if (body_hash && !body_hash.empty?)
uri = Addressable::URI.new
uri.query_values = body_hash
api_uri += "?" + uri.query
end
opts = {
:method => :get,
:url => api_uri,
:headers => ,
:open_timeout => 30,
:timeout => 80
}
else
opts = {
:method => http_verb,
:url => api_uri,
:headers => ,
:open_timeout => 30,
:payload => MachineShop::JSON.dump(body_hash),
:timeout => 80
}
end
puts "request params: #{opts} "
begin
response = execute_request(opts)
rescue SocketError => e
self.handle_restclient_error(e)
rescue NoMethodError => e
if e.message =~ /\WRequestFailed\W/
e = APIConnectionError.new('Unexpected HTTP response code')
self.handle_restclient_error(e)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
self.handle_api_error(rcode, rbody)
else
self.handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
self.handle_restclient_error(e)
end
rbody = response.body
rcode = response.code
end
begin
resp = MachineShop::JSON.load(rbody)
resp ||= {}
resp = Util.symbolize_names(resp)
save_into_cache(url,resp,auth_token) if (http_verb == :get && cachedContent==:false)
resp.merge!({:http_code => rcode}) if resp.is_a?(Hash)
return resp
rescue MultiJson::DecodeError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
end
|
.post(name, auth_token, body_hash) ⇒ Object
96
97
98
99
|
# File 'lib/machineshop.rb', line 96
def post(name,auth_token, body_hash)
url = Util.valid_endpoint(name,auth_token,:post,[])
platform_request(url, auth_token,body_hash,:post)
end
|
.put(name, auth_token, *params, body_hash) ⇒ Object
101
102
103
104
|
# File 'lib/machineshop.rb', line 101
def put(name,auth_token,*params,body_hash)
url = Util.valid_endpoint(name,auth_token,:put,params)
platform_request(url, auth_token,body_hash,:put)
end
|
.reset ⇒ Object
76
77
78
|
# File 'lib/machineshop.rb', line 76
def reset
Configuration.new
end
|
.save_into_cache(url, data, auth_token) ⇒ Object
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/machineshop.rb', line 296
def save_into_cache(url, data,auth_token)
id,klass= Util.get_klass_from_url(url)
if !TABLE_NAME_BLACKLIST.include?(klass)
if Util.db_connected?
klass = klass.capitalize+"Cache"
puts "creating dynamic class #{klass}"
modelClass ||= (Object.const_set klass, Class.new(ActiveRecord::Base))
modelClass.inheritance_column = :_type_disabled
if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)
modelClass.delete_all
puts "db table #{klass.pluralize.underscore} exists"
if data.class ==Hash
findId = data[:_id] || data["_id"]
@activeObject = modelClass.new
data.each do |k,v|
val=nil
if v.class==Array
val = v.to_json
elsif v.class==Hash
val = v.to_json
else
val=v
end
if @activeObject.has_attribute?(k)
@activeObject.send("#{k}=",val)
end
@activeObject.save
end
else
data.each do |data_arr|
if data_arr
if data_arr.first.class==String && data_arr.class==Array
@activeObject = modelClass.find_by(rule_condition: data_arr.select{|k| k.include?("rule_condition")}) || modelClass.new
data_arr.each do |k|
if k.include?("rule_condition")
@activeObject.rule_condition = k
else
@activeObject.rule_description=k
end
end
else
if data_arr.class!=String
findId = data_arr[:_id] || data_arr["_id"]
@activeObject = modelClass.find_by(_id: findId) || modelClass.new
data_arr.each do |k,v|
val=nil
if v.class==Array
val = v.to_json
elsif v.class==Hash
val = v.to_json
else
val=v
end
if @activeObject.has_attribute?(k)
@activeObject.send("#{k}=",val)
end
end
end
end
end
@activeObject.send("auth_token=",auth_token)
@activeObject.save
end
end
end
end
end
end
|