class Openplacos::Client

Attributes

actuators[RW]
config[RW]
initial_room[RW]
objects[RW]
reguls[RW]
rooms[RW]
sensors[RW]
service[RW]

Public Class Methods

new(url_, name_, scope_, connection_type_, id_ = "0", opt_={}) click to toggle source

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.

# File lib/openplacos/libclient.rb, line 278
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

Public Instance Methods

construct_module_name(iface_name_) click to toggle source

transform iface_name to a module name that obj will inherit

# File lib/openplacos/libclient.rb, line 336
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_ ) click to toggle source

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

# File lib/openplacos/libclient.rb, line 329
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_objects() click to toggle source
# File lib/openplacos/libclient.rb, line 316
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_) click to toggle source
# File lib/openplacos/libclient.rb, line 306
def get_iface_type(obj, det_)
  a = Array.new
  obj.interfaces.each { |iface|
    if(iface.include?(det_))
      a << iface
    end
  }
  a
end
introspect() click to toggle source

Intropect the distant server

# File lib/openplacos/libclient.rb, line 299
def introspect
  @introspect = JSON.parse( @connection.token.get('/ressources').body)
  @introspect.each { |obj|
    @objects[obj["name"]] = ProxyObject.new(@connection, obj) 
  }
end
me() click to toggle source

return the user name

# File lib/openplacos/libclient.rb, line 345
def me
  JSON.parse(@connection.token.get("/me").body)["username"]
end