Class: Beebotte::Connector
- Inherits:
-
Object
- Object
- Beebotte::Connector
- Defined in:
- lib/beebotte.rb
Constant Summary collapse
- ATTRIBUTE_TYPE_LABELS =
[ 'any', 'number', 'string', 'boolean', 'object', 'function', 'array', 'alphabetic', 'alphanumeric', 'decimal', 'rate', 'percentage', 'email', 'gps', 'cpu', 'memory', 'netif', 'disk', 'temperature', 'humidity', 'body_temp', ]
Instance Method Summary collapse
- #add_channel(channel, &block) ⇒ Object
-
#add_resource(channel, resource, &block) ⇒ Object
{name, description, type, vtype, ispublic}.
- #del_channel(channel, &block) ⇒ Object
- #del_resource(channel, resource, &block) ⇒ Object
- #get_channel(channel, &block) ⇒ Object
-
#get_channels(channel = nil, &block) ⇒ Object
get_channels { |response| puts response.body }.
- #get_conection(user, &block) ⇒ Object
- #get_connections(user = nil, &block) ⇒ Object
- #get_resource(channel, resource, &block) ⇒ Object
- #get_resources(channel, resource = '*', &block) ⇒ Object
-
#initialize(apiKey, secretKey, hostname = 'api.beebotte.com', port = 80, protocol = nil, headers = nil) ⇒ Connector
constructor
A new instance of Connector.
-
#publish(channel, resource, data, &block) ⇒ Object
publish transient information.
-
#read(params, &block) ⇒ Object
Read persisted messages from the specified channel and resource.
-
#write(channel, resource, data, &block) ⇒ Object
write persistent information.
Constructor Details
#initialize(apiKey, secretKey, hostname = 'api.beebotte.com', port = 80, protocol = nil, headers = nil) ⇒ Connector
Returns a new instance of Connector.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/beebotte.rb', line 37 def initialize(apiKey, secretKey, hostname='api.beebotte.com', port=80, protocol=nil, headers=nil) @apiKey = apiKey @secretKey = secretKey @hostname = hostname @port = port @protocol = protocol.is_a?(String) ? protocol.downcase : ((@port == 443) ? 'https' : 'http') @headers = headers || { "Content-type" => 'application/json', "Content-MD5" => '', "User-Agent" => get_useragent_string } @resource_schema = { name: CH::G.string_length(2..30), label: [:optional, CH::G.string_length(0..30) ], description: [:optional, String ], vtype: Set.new(ATTRIBUTE_TYPE_LABELS), sendOnSubscribe: [:optional, TrueClass] } @channel_schema = { name: CH::G.string_length(2..30), label: [:optional, CH::G.string_length(0..30) ], description: [:optional, String ], resources: [ [ @resource_schema ] ], ispublic: [:optional, TrueClass] } @read_params_schema = { channel: CH::G.string_length(2..30), resource: CH::G.string_length(2..30), limit: [:optional, 1..2000 ], 'time-range' => [:optional, String], 'start-time' => [:optional, String], 'end-time' => [:optional, String], filter: [:optional, String], 'sample-rate' => [:optional, 1..10000] } end |
Instance Method Details
#add_channel(channel, &block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/beebotte.rb', line 167 def add_channel(channel, &block) ClassyHash.validate(channel, @channel_schema, strict: true) # validate that no resource descriptions are the same as the channel name raise ArgumentError, 'Must have at least one resource' if channel[:resources].length < 1 channel[:resources].each do |r| raise ArgumentError, 'Resource :name must not equal Channel :name' if r[:name] == channel[:name] end response = post_data("/v1/channels", channel.to_json) if response.code >= 200 && response.code < 300 get_channel(channel[:name], &block) else if block block.call({}, response.code) else return false end end end |
#add_resource(channel, resource, &block) ⇒ Object
{name, description, type, vtype, ispublic}
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/beebotte.rb', line 220 def add_resource(channel, resource, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) ClassyHash.validate(resource, @resource_schema, strict: true) # validate that no resource descriptions are the same as the channel name raise ArgumentError, 'Resource :name must not equal Channel :name' if resource[:name] == channel response = post_data("/v1/channels/#{channel}/resources", resource.to_json) if response.code >= 200 && response.code < 300 get_resource(channel, resource[:name], &block) else if block block.call({}, response.code) else return false end end end |
#del_channel(channel, &block) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/beebotte.rb', line 186 def del_channel(channel, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) response = del_data("/v1/channels/#{channel}") if block block.call(response.body, response.code) else return (response.code >= 200 && response.code < 300) ? true : false end end |
#del_resource(channel, resource, &block) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/beebotte.rb', line 238 def del_resource(channel, resource, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) response = del_data("/v1/channels/#{channel}/resources/#{resource}") if block block.call(response.body, response.code) else return (response.code >= 200 && response.code < 300) ? true : false end end |
#get_channel(channel, &block) ⇒ Object
162 163 164 165 |
# File 'lib/beebotte.rb', line 162 def get_channel(channel, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) get_channels(channel, &block) end |
#get_channels(channel = nil, &block) ⇒ Object
get_channels { |response| puts response.body }
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/beebotte.rb', line 150 def get_channels(channel=nil, &block) rtn = {} uri = "/v1/channels" + (channel.is_a?(String) ? "/#{channel}" : '') response = get_data(uri) rtn = JSON.parse(response.body) if response.code >= 200 && response.code < 300 if block block.call(rtn, response.code) else (response.code >= 200 && response.code < 300) ? rtn : nil end end |
#get_conection(user, &block) ⇒ Object
143 144 145 146 |
# File 'lib/beebotte.rb', line 143 def get_conection(user, &block) raise ArgumentError, 'User name must be a string' unless user.is_a?(String) get_connections(user, &block) end |
#get_connections(user = nil, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/beebotte.rb', line 131 def get_connections(user=nil, &block) resource = user.nil? ? [] : {} uri = "/v1/connections" + (resource.is_a?(String) ? "/#{resource}" : '') response = get_data(uri) resource = JSON.parse(response.body) if response.code >= 200 && response.code < 300 if block block.call(resource, response.code) else (response.code >= 200 && response.code < 300) ? resource : nil end end |
#get_resource(channel, resource, &block) ⇒ Object
211 212 213 214 215 |
# File 'lib/beebotte.rb', line 211 def get_resource(channel, resource, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) get_resources(channel, resource, &block) end |
#get_resources(channel, resource = '*', &block) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/beebotte.rb', line 196 def get_resources(channel, resource='*', &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) rtn = {} params = { resource: resource } response = get_data("/v1/channels/#{channel}/resources", params) rtn = JSON.parse(response.body) if response.code >= 200 && response.code < 300 if block block.call(rtn, response.code) else return (response.code >= 200 && response.code < 300) ? rtn : nil end end |
#publish(channel, resource, data, &block) ⇒ Object
publish transient information
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/beebotte.rb', line 94 def publish(channel, resource, data, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) raise ArgumentError, 'Data must be a hash object' unless data.is_a?(Hash) body = {data: data} response = post_data("/v1/data/publish/#{channel}/#{resource}", body.to_json) if block block.call(response.body, response.code) else (response.code >= 200 && response.code < 300) ? true : false end end |
#read(params, &block) ⇒ Object
Read persisted messages from the specified channel and resource
Attributes
-
channel
- String: the channel name -
resource
- String: the resource name -
params
- Hash: the query parameters: ‘time-range’, ‘start-time’, ‘end-time’, ‘limit’, ‘filter’, ‘sample-rate’
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/beebotte.rb', line 115 def read(params, &block) ClassyHash.validate(params, @read_params_schema, strict: true) params[:limit] ||= 750 rtn = {} uri = "/v1/data/read/#{params[:channel]}/#{params[:resource]}" [:channel, :resource].each {|k| params.delete(k) } response = get_data(uri, params) rtn = JSON.parse(response.body) if response.code >= 200 && response.code < 300 if block block.call(rtn, response.code) else (response.code >= 200 && response.code < 300) ? rtn : nil end end |
#write(channel, resource, data, &block) ⇒ Object
write persistent information
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/beebotte.rb', line 79 def write(channel, resource, data, &block) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) raise ArgumentError, 'Data must be a hash object' unless data.is_a?(Hash) body = {data: data} response = post_data("/v1/data/write/#{channel}/#{resource}", body.to_json) if block block.call(response.body, response.code) else (response.code >= 200 && response.code < 300) ? true : false end end |