Class: CS::Client

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

Overview

Main entry class of the library.

The login functionality always comes in two pair. The Bang (!) method will raise an exception when there is an error and the normal (without !) will return nil when it fails.

The response can be viewed by looking at the Session

client.session # will return the session object

Authentication with User And Password

client = CS::Client.new
client.('username', 'password')

Authentication using OAuth

client = CS::Client.new
client.oauth('CONSUMER_KEY', 'CONSUMER_SECRET', 'ACCESS_TOKEN', 'ACCESS_TOKEN_SECRET')

Using different API server

client = CS::Client.new(base_uri: 'https://api.dev.sense-os.nl')
# or
client.base_uri = 'https://api.dev.sense-os.nl'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



56
57
58
59
60
61
# File 'lib/cs.rb', line 56

def initialize(opts={})
  options = {
    base_uri: 'https://api.sense-os.nl',
  }.merge(opts)
  @base_uri = options[:base_uri]
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



54
55
56
# File 'lib/cs.rb', line 54

def logger
  @logger
end

#sessionObject

Returns the value of attribute session.



53
54
55
# File 'lib/cs.rb', line 53

def session
  @session
end

Instance Method Details

#api_key=(api_key) ⇒ Object

Create new session by specifying api_key

client = CS::Client.new
client.session_id = '12345'


117
118
119
120
121
# File 'lib/cs.rb', line 117

def api_key=(api_key)
  @session = Session.new(base_uri: @base_uri)
  @session.logger = logger
  @session.api_key = api_key
end

#base_uri=(uri) ⇒ Object



63
64
65
66
# File 'lib/cs.rb', line 63

def base_uri=(uri)
  @base_uri = uri
  session.base_uri = uri
end

#current_groupsObject



164
165
166
167
168
# File 'lib/cs.rb', line 164

def current_groups
  group = EndPoint::Group.new
  group.session = @session
  group.current_groups
end

#current_userObject

Retrun logged in user



124
125
126
127
128
# File 'lib/cs.rb', line 124

def current_user
  user = EndPoint::User.new
  user.session = @session
  user.current_user
end

#errorsObject

return errors got from session



172
173
174
# File 'lib/cs.rb', line 172

def errors
  return @session.errors if @session
end

#groupsObject



152
153
154
# File 'lib/cs.rb', line 152

def groups
  Relation::GroupRelation.new(@session)
end

#login(user, password, digest = true) ⇒ Object

Create a new session to CommonSense using username and plain text password with ‘login` it will return nil if it not successful

client = CS::Client.new
client.('username', 'password')


89
90
91
# File 'lib/cs.rb', line 89

def (user, password, digest=true)
  login!(user, password, digest) rescue false
end

#login!(user, password, digest = true) ⇒ Object

Create a new session to CommonSense using username and plain text password with ‘login!` it will throw exception if there is an error

client = CS::Client.new
client.login!('username', 'password')


78
79
80
81
82
# File 'lib/cs.rb', line 78

def login!(user, password, digest=true)
  @session = Session.new(base_uri: @base_uri)
  @session.logger = logger
  @session.(user, password, digest)
end

#new_user(hash = {}) ⇒ Object

Create a new user

client = CS::Client.new
client.new_user(username: 'Ahmy')
client.email = '[email protected]'
...
client.save!


137
138
139
140
141
# File 'lib/cs.rb', line 137

def new_user(hash={})
 user = EndPoint::User.new(hash)
 user.session = Session.new(base_uri: @base_uri, authentication: false)
 user
end

#notificationsObject



160
161
162
# File 'lib/cs.rb', line 160

def notifications
  Relation::NotificationRelation.new(@session)
end

#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object

Create a new session to CommonSense using OAuth credentials

client = CS::Client.new
client.('username', 'password')


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

def oauth(consumer_key, consumer_secret, access_token, access_token_secret)
  @session = Session.new(base_uri: @base_uri)
  @session.logger = logger
  @session.oauth(consumer_key, consumer_secret, access_token, access_token_secret)
end

#sensorsObject



148
149
150
# File 'lib/cs.rb', line 148

def sensors
  Relation::SensorRelation.new(@session)
end

#session_id=(session_id) ⇒ Object

Create new session by manually specifiying ‘session_id` parameter

client = CS::Client.new
client.session_id = '12345'


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

def session_id=(session_id)
  @session = Session.new(base_uri: @base_uri)
  @session.logger = logger
  @session.session_id = session_id
end

#triggersObject



156
157
158
# File 'lib/cs.rb', line 156

def triggers
  Relation::TriggerRelation.new(@session)
end

#usersObject



143
144
145
# File 'lib/cs.rb', line 143

def users
  Relation::UserRelation.new(@session)
end