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 =

VERSION = “1.0.2”

"0.0.5"
@@api_base_url =

@@api_base_url = ‘api.machineshop.io/api/v0

'http://stage.services.machineshop.io/api/v0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



68
69
70
# File 'lib/machineshop.rb', line 68

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.api_base_urlObject



87
88
89
# File 'lib/machineshop.rb', line 87

def api_base_url
  @@api_base_url
end

.api_base_url=(api_base_url) ⇒ Object

configs ends



83
84
85
# File 'lib/machineshop.rb', line 83

def api_base_url=(api_base_url)
  @@api_base_url = api_base_url
end

.api_error(error, rcode, rbody, error_obj) ⇒ Object



267
268
269
# File 'lib/machineshop.rb', line 267

def api_error(error, rcode, rbody, error_obj)
  APIError.new(error, rcode, rbody, error_obj)
end

.authentication_error(error, rcode, rbody, error_obj) ⇒ Object



263
264
265
# File 'lib/machineshop.rb', line 263

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

Returns:



289
290
291
292
293
294
# File 'lib/machineshop.rb', line 289

def class_exists?(class_name)
  klass = class_name.constantize
  return klass.is_a?(Class)
rescue NameError =>e
  return false
end

.configure {|configuration| ... } ⇒ Object

Yields:



72
73
74
# File 'lib/machineshop.rb', line 72

def configure
  yield(configuration)
end

.delete(name, auth_token, *params) ⇒ Object



107
108
109
110
# File 'lib/machineshop.rb', line 107

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



234
235
236
# File 'lib/machineshop.rb', line 234

def execute_request(opts)
  RestClient::Request.execute(opts)
end

.gem_delete(url, auth_token, body_hash) ⇒ Object



121
122
123
# File 'lib/machineshop.rb', line 121

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



113
114
115
# File 'lib/machineshop.rb', line 113

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



117
118
119
# File 'lib/machineshop.rb', line 117

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



125
126
127
# File 'lib/machineshop.rb', line 125

def gem_put(url, auth_token, body_hash)
  platform_request(url, auth_token, body_hash, :put)
end

.get(name, auth_token, *params) ⇒ Object

For Custom endpoints



92
93
94
95
# File 'lib/machineshop.rb', line 92

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



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
433
# File 'lib/machineshop.rb', line 391

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
          # pagination = body_hash.select{|k| k==:per_page || k==:page} if body_hash
          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



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/machineshop.rb', line 238

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 # escape from parsing
  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
    # TODO Come up with errors
  else
    raise api_error(error, rcode, rbody, error_obj)
  end
end

.handle_restclient_error(e) ⇒ Object

Raises:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/machineshop.rb', line 271

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})"
  # puts "error message string : #{message}"
  raise APIConnectionError.new(message)
end

.headers(auth_token) ⇒ Object



129
130
131
132
133
134
# File 'lib/machineshop.rb', line 129

def headers(auth_token)
  header ={:content_type => :json,
  :accept => :json}
  header.merge!({ authorization: "Basic " + Base64.encode64(auth_token + ':X') }) if auth_token
  header
end

.invalid_request_error(error, rcode, rbody, error_obj) ⇒ Object



259
260
261
# File 'lib/machineshop.rb', line 259

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



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
472
# File 'lib/machineshop.rb', line 441

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
  #append auth_token = auth_token part as well
  search_parms['auth_token']=auth_token
  search_parms
end

.platform_request(url, auth_token, body_hash = nil, http_verb = :get) ⇒ Object



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
231
# File 'lib/machineshop.rb', line 136

def platform_request(url, auth_token, body_hash=nil, http_verb=:get )
  rbody=nil
  cachedContent = :true
  # ApiRequest.cache(url,MachineShop.configuration.expiry_time)
  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
    headers = self.headers(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 => headers,
        :open_timeout => 30,
        :timeout => 80
      }

    else
      opts = {
        :method => http_verb,
        :url => api_uri,
        :headers => 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
      # Work around RestClient bug
      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
    # puts rbody
    rcode = response.code
  end

  begin
    # Would use :symbolize_names => true, but apparently there is
    # some library out there that makes symbolize_names not work.
    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



97
98
99
100
# File 'lib/machineshop.rb', line 97

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



102
103
104
105
# File 'lib/machineshop.rb', line 102

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

.resetObject

reset the config object



77
78
79
# File 'lib/machineshop.rb', line 77

def reset
  Configuration.new
end

.save_into_cache(url, data, auth_token) ⇒ Object



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
388
# File 'lib/machineshop.rb', line 297

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
      #Because 'type' is reserved for storing the class in case of inheritance and our array has "TYPE" key

      if ActiveRecord::Base.connection.table_exists? CGI.escape(klass.pluralize.underscore)

        #delete all the previous records

        modelClass.delete_all
        puts "db table #{klass.pluralize.underscore} exists"
        if data.class ==Hash

          findId = data[:_id] || data["_id"]
          @activeObject = modelClass.new
          # @activeObject = modelClass.find_by(_id: findId) || 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
                    #check if the database has the particular field to store
                    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 #if ActiveRecord ends

    end #if db_connected ends
  end #if !TABLE_NAME_BLACKLIST.include? ends
end