Class: CS::Client
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.login('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
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#api_key=(api_key) ⇒ Object
Create new session by specifying api_key.
- #base_uri=(uri) ⇒ Object
- #current_groups ⇒ Object
-
#current_user ⇒ Object
Retrun logged in user.
-
#errors ⇒ Object
return errors got from session.
- #groups ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
-
#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.
-
#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.
-
#new_user(hash = {}) ⇒ Object
Create a new user.
- #notifications ⇒ Object
-
#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object
Create a new session to CommonSense using OAuth credentials.
- #sensors ⇒ Object
-
#session_id=(session_id) ⇒ Object
Create new session by manually specifiying ‘session_id` parameter.
- #triggers ⇒ Object
- #users ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
61 62 63 64 65 66 |
# File 'lib/cs.rb', line 61 def initialize(opts={}) = { base_uri: 'https://api.sense-os.nl', }.merge(opts) @base_uri = [:base_uri] end |
Instance Attribute Details
#session ⇒ Object
Returns the value of attribute session.
55 56 57 |
# File 'lib/cs.rb', line 55 def session @session end |
Instance Method Details
#api_key=(api_key) ⇒ Object
130 131 132 133 134 |
# File 'lib/cs.rb', line 130 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
68 69 70 71 72 73 74 75 |
# File 'lib/cs.rb', line 68 def base_uri=(uri) @base_uri = uri unless session @session = Session.new() end session.base_uri = uri end |
#current_groups ⇒ Object
177 178 179 180 181 |
# File 'lib/cs.rb', line 177 def current_groups group = EndPoint::Group.new group.session = @session group.current_groups end |
#current_user ⇒ Object
Retrun logged in user
137 138 139 140 141 |
# File 'lib/cs.rb', line 137 def current_user user = EndPoint::User.new user.session = @session user.current_user end |
#errors ⇒ Object
return errors got from session
185 186 187 |
# File 'lib/cs.rb', line 185 def errors return @session.errors if @session end |
#groups ⇒ Object
165 166 167 |
# File 'lib/cs.rb', line 165 def groups Relation::GroupRelation.new(@session) end |
#logger ⇒ Object
82 83 84 |
# File 'lib/cs.rb', line 82 def logger @logger end |
#logger=(logger) ⇒ Object
77 78 79 80 |
# File 'lib/cs.rb', line 77 def logger=(logger) @logger = logger @session.logger = logger if @session end |
#login(user, password, digest = true) ⇒ Object
102 103 104 |
# File 'lib/cs.rb', line 102 def login(user, password, digest=true) login!(user, password, digest) rescue false end |
#login!(user, password, digest = true) ⇒ Object
91 92 93 94 95 |
# File 'lib/cs.rb', line 91 def login!(user, password, digest=true) @session = Session.new(base_uri: @base_uri) @session.logger = @logger @session.login(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!
150 151 152 153 154 |
# File 'lib/cs.rb', line 150 def new_user(hash={}) user = EndPoint::User.new(hash) user.session = Session.new(base_uri: @base_uri, authentication: false) user end |
#notifications ⇒ Object
173 174 175 |
# File 'lib/cs.rb', line 173 def notifications Relation::NotificationRelation.new(@session) end |
#oauth(consumer_key, consumer_secret, access_token, access_token_secret) ⇒ Object
110 111 112 113 114 |
# File 'lib/cs.rb', line 110 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 |
#sensors ⇒ Object
161 162 163 |
# File 'lib/cs.rb', line 161 def sensors Relation::SensorRelation.new(@session) end |
#session_id=(session_id) ⇒ Object
120 121 122 123 124 |
# File 'lib/cs.rb', line 120 def session_id=(session_id) @session = Session.new(base_uri: @base_uri) @session.logger = @logger @session.session_id = session_id end |
#triggers ⇒ Object
169 170 171 |
# File 'lib/cs.rb', line 169 def triggers Relation::TriggerRelation.new(@session) end |
#users ⇒ Object
156 157 158 |
# File 'lib/cs.rb', line 156 def users Relation::UserRelation.new(@session) end |