Class: Openplacos::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openplacos/libclient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_, name_, scope_, connection_type_, id_ = "0", opt_ = {}) ⇒ Client

Initialize a connection to server with OAuth2 in a automatic way Please provide url server, application name, permission needed for application Set connection_type to auth_code to use with oauth2 flow You can access to proxyfied objects with .objects attribute Please give:

  • opos url

  • an application name that identify the client oath2 talking

  • a scope, typically [“read”, “write”, “user”]

  • a connection_type, set it to “auth_code” to use oauth2 with classic flow (recommanded)

or with “password” to use with password flow. Set to “inception” to pass a connection object through opt:connection

  • an optionnal id, to manage several clients

  • an optionnal option hash, in which you can specify openplacos port { :port => 5454 }

  • You can also pass a token object through opt that is an oauth2 object.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/openplacos/libclient.rb', line 279

def initialize(url_, name_, scope_, connection_type_, id_ = "0", opt_={})

  @objects = Hash.new
  @connection_type = connection_type_
  if opt_[:token].nil?
    case @connection_type
    when "auth_code" then
      @connection =  Connection_auth_code.new(url_, name_, scope_, id_, opt_[:port] || 2000)
    when "password" then
      @connection = Connection_password.new(url_, name_, scope_, id_, opt_[:port] || 2000, opt_[:username], opt_[:password])
    else
      raise "unknow grant type"
    end
  else
    @connection = Connection_from_token.new(opt_[:token])
  end
  introspect
  extend_objects
end

Instance Attribute Details

#actuatorsObject

Returns the value of attribute actuators.



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

def actuators
  @actuators
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#initial_roomObject

Returns the value of attribute initial_room.



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

def initial_room
  @initial_room
end

#objectsObject

Returns the value of attribute objects.



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

def objects
  @objects
end

#regulsObject

Returns the value of attribute reguls.



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

def reguls
  @reguls
end

#roomsObject

Returns the value of attribute rooms.



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

def rooms
  @rooms
end

#sensorsObject

Returns the value of attribute sensors.



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

def sensors
  @sensors
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#construct_module_name(iface_name_) ⇒ Object

transform iface_name to a module name that obj will inherit



337
338
339
340
341
342
343
# File 'lib/openplacos/libclient.rb', line 337

def construct_module_name(iface_name_)
  iface_heritage = iface_name_.sub(/org.openplacos./, '').split('.')
  iface_heritage.each { |s|
    s.capitalize!
  }
  iface_heritage.join('::')
end

#extend_iface(iface_name_, obj_) ⇒ Object

Extend an object to a ruby module according to iface_name_ if iface_name_ is “org.openplacos.analog.order”

> object will inherit Openplacos::Analog::Order



330
331
332
333
334
# File 'lib/openplacos/libclient.rb', line 330

def extend_iface(iface_name_,obj_ )
  mod = "Openplacos::"+ construct_module_name(iface_name_)
  mod.extend(Openplacos::String)
  obj_.extend(mod.get_max_const)
end

#extend_objectsObject



317
318
319
320
321
322
323
324
325
# File 'lib/openplacos/libclient.rb', line 317

def extend_objects
  @objects.each_pair{ |key, obj|
    if (key != "/informations")
      obj.interfaces.each { |iface|
        extend_iface(iface, obj[iface])
      }
    end
  }
end

#get_iface_type(obj, det_) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/openplacos/libclient.rb', line 307

def get_iface_type(obj, det_)
  a = Array.new
  obj.interfaces.each { |iface|
    if(iface.include?(det_))
      a << iface
    end
  }
  a
end

#introspectObject

Intropect the distant server



300
301
302
303
304
305
# File 'lib/openplacos/libclient.rb', line 300

def introspect
  @introspect = JSON.parse( @connection.token.get('/ressources').body)
  @introspect.each { |obj|
    @objects[obj["name"]] = ProxyObject.new(@connection, obj) 
  }
end

#meObject

return the user name



346
347
348
# File 'lib/openplacos/libclient.rb', line 346

def me
  JSON.parse(@connection.token.get("/me").body)["username"]
end