Class: Telemetry::Api
- Inherits:
-
Object
- Object
- Telemetry::Api
- Defined in:
- lib/telemetry/api.rb
Class Method Summary collapse
- .affiliate_send(affiliate_identifier, flow) ⇒ Object
- .affiliate_send_batch(affiliate_identifier, flows) ⇒ Object
- .aggregate(bucket, value) ⇒ Object
- .aggregate_set_interval(bucket, interval, values) ⇒ Object
- .channel_send(channel_tag, flow) ⇒ Object
- .channel_send_batch(channel_tag, flows) ⇒ Object
- .delete(path) ⇒ Object
- .delete_flow_data(id) ⇒ Object
- .flow_update(flow) ⇒ Object
- .flow_update_batch(flows) ⇒ Object
- .get(path) ⇒ Object
- .get_flow(id) ⇒ Object
- .get_flow_data(id) ⇒ Object
- .notification(id, body) ⇒ Object
- .parse_response(response) ⇒ Object
- .patch(path, body) ⇒ Object
- .post(path, body) ⇒ Object
- .send(method, endpoint, data = nil) ⇒ Object
Class Method Details
.affiliate_send(affiliate_identifier, flow) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/telemetry/api.rb', line 129 def self.affiliate_send(affiliate_identifier, flow) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token raise RuntimeError, "Must supply flow to send" unless flow raise RuntimeError, "Must supply a unique affiliate identifier" unless affiliate_identifier values = flow.to_hash tag = values.delete('tag') return Telemetry::Api.send(:post, "/affiliates/#{affiliate_identifier}/flows/#{tag}/data", values) end |
.affiliate_send_batch(affiliate_identifier, flows) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/telemetry/api.rb', line 138 def self.affiliate_send_batch(affiliate_identifier, flows) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token raise RuntimeError, "Must supply flows to send" unless flows raise RuntimeError, "Must supply a unique affiliate identifier" unless affiliate_identifier data = {} flows.each do |flow| values = flow.to_hash tag = values.delete('tag') data[tag] = values end return Telemetry::Api.send(:post, "/affiliates/#{affiliate_identifier}/data", {:data => data}) end |
.aggregate(bucket, value) ⇒ Object
97 98 99 100 |
# File 'lib/telemetry/api.rb', line 97 def self.aggregate(bucket, value) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token return Telemetry::Api.send(:post, "/aggregations/#{bucket}", {:value => value}) end |
.aggregate_set_interval(bucket, interval, values) ⇒ Object
102 103 104 105 |
# File 'lib/telemetry/api.rb', line 102 def self.aggregate_set_interval(bucket, interval, values) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token return Telemetry::Api.send(:put, "/aggregations/#{bucket}/interval/#{interval}", {:value => value}) end |
.channel_send(channel_tag, flow) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/telemetry/api.rb', line 120 def self.channel_send(channel_tag, flow) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token raise RuntimeError, "Must supply flow to send" unless flow raise RuntimeError, "Must supply a channel_tag" unless channel_tag values = flow.to_hash tag = values.delete('tag') return Telemetry::Api.send(:post, "/channels/#{channel_tag}/flows/#{tag}/data", values) end |
.channel_send_batch(channel_tag, flows) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/telemetry/api.rb', line 107 def self.channel_send_batch(channel_tag, flows) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token raise RuntimeError, "Must supply flows to send" unless flows raise RuntimeError, "Must supply a channel_tag" unless channel_tag data = {} flows.each do |flow| values = flow.to_hash tag = values.delete('tag') data[tag] = values end return Telemetry::Api.send(:post, "/channels/#{channel_tag}/data", {:data => data}) end |
.delete(path) ⇒ Object
76 77 78 |
# File 'lib/telemetry/api.rb', line 76 def self.delete(path) Telemetry::Api.send(:delete, path) end |
.delete_flow_data(id) ⇒ Object
88 89 90 |
# File 'lib/telemetry/api.rb', line 88 def self.delete_flow_data(id) Telemetry::Api.send(:delete, "/flows/#{id}/data") end |
.flow_update(flow) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/telemetry/api.rb', line 151 def self.flow_update(flow) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token values = flow.to_hash tag = values.delete('tag') result = Telemetry::Api.send(:put, "/flows/#{tag}/data", values) raise ResponseError, "API Response: #{result['errors'].join(', ')}" unless result["updated"].include?(tag) result end |
.flow_update_batch(flows) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/telemetry/api.rb', line 160 def self.flow_update_batch(flows) raise Telemetry::AuthenticationFailed, "Please set your Telemetry.token" unless Telemetry.token raise RuntimeError, "Must supply flows to send" if flows == 0 || flows.count == 0 data = {} flows.each do |flow| values = flow.to_hash tag = values.delete('tag') data[tag] = values end return Telemetry::Api.send(:post, "/data", {:data => data}) end |
.get(path) ⇒ Object
64 65 66 |
# File 'lib/telemetry/api.rb', line 64 def self.get(path) Telemetry::Api.send(:get, path) end |
.get_flow(id) ⇒ Object
80 81 82 |
# File 'lib/telemetry/api.rb', line 80 def self.get_flow(id) Telemetry::Api.send(:get, "/flows/#{id}") end |
.get_flow_data(id) ⇒ Object
84 85 86 |
# File 'lib/telemetry/api.rb', line 84 def self.get_flow_data(id) Telemetry::Api.send(:get, "/flows/#{id}/data") end |
.notification(id, body) ⇒ Object
92 93 94 95 |
# File 'lib/telemetry/api.rb', line 92 def self.notification(id, body) path = "/channels/#{id}/notifications" Telemetry::Api.send(:post, path, body) end |
.parse_response(response) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/telemetry/api.rb', line 289 def self.parse_response(response) begin resp_json = MultiJson.load(response.body) return nil unless resp_json && (resp_json.is_a?(Hash) || resp_json.is_a?(Array)) if resp_json.is_a?(Hash) if resp_json["errors"] && resp_json["errors"].is_a?(Array) && resp_json["errors"].count > 0 Telemetry::logger.error "Errors: #{resp_json['errors'].join(', ')}" end if resp_json["updated"] && resp_json["updated"].is_a?(Array) && resp_json["updated"].count > 0 Telemetry::logger.debug "Updated: #{resp_json['updated'].join(', ')}" end if resp_json["skipped"] && resp_json["skipped"].is_a?(Array) && resp_json["skipped"].count > 0 Telemetry::logger.error "Skipped: #{resp_json['skipped'].join(', ')}" end end return resp_json rescue Exception => e return nil end end |
.patch(path, body) ⇒ Object
72 73 74 |
# File 'lib/telemetry/api.rb', line 72 def self.patch(path, body) Telemetry::Api.send(:patch, path, body) end |
.post(path, body) ⇒ Object
68 69 70 |
# File 'lib/telemetry/api.rb', line 68 def self.post(path, body) Telemetry::Api.send(:post, path, body) end |
.send(method, endpoint, data = nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/telemetry/api.rb', line 172 def self.send(method, endpoint, data = nil) http = Net::HTTP::Persistent.new 'telemetry_api' http.idle_timeout = 15 http.max_requests = 100 uri = URI("#{Telemetry.api_host}#{endpoint}") Telemetry::logger.debug "REQ #{uri} - #{MultiJson.dump(data)}" if method == :post request = Net::HTTP::Post.new(uri.path) request.body = MultiJson.dump(data) elsif method == :put request = Net::HTTP::Put.new(uri.path) request.body = MultiJson.dump(data) elsif method == :patch request = Net::HTTP::Patch.new(uri.path) request.body = MultiJson.dump(data) elsif method == :get request = Net::HTTP::Get.new(uri.path) elsif method == :delete request = Net::HTTP::Delete.new(uri.path) end request.basic_auth(Telemetry.token, "") if Telemetry.token request['Content-Type'] = 'application/json' request['Accept-Version'] = '~ 1' request['User-Agent'] = "Telemetry Ruby Gem (#{Telemetry::TELEMETRY_VERSION})" start_time = Time.now begin ssl = true if Telemetry.api_host.match(/^https/) response = http.request uri, request code = response.code Telemetry::logger.debug "RESP (#{((Time.now-start_time)*1000).to_i}ms): #{response.code}:#{response.body}" result = Telemetry::Api.parse_response(response) case response.code when "200" Telemetry::logger.debug "200 OK" return result when "201" Telemetry::logger.debug "201 OK" return result when "204" Telemetry::logger.debug "204 OK" return "No Body" when "400" error = "HTTP 400: Request error " Telemetry::logger.error error raise Telemetry::FormatError, error when "401" if Telemetry.token == nil error = "HTTP 401: Authentication failed, please set Telemetry.token to your API Token. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::AuthenticationFailed, error else error = "HTTP 401: Authentication failed, please verify your token. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::AuthenticationFailed, error end when "403" error = "HTTP 403: Authorization failed, please check your account access. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::AuthorizationError, error when "404" error = "HTTP 404: Requested object not found. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::FlowNotFound, error when "405" error = "HTTP 405: Method not allowed. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::MethodNotAllowed, error when "429" error = "HTTP 429: Rate limited. Please reduce your update interval. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::RateLimited, error when "500" error = "HTTP 500: API server error. #{method.upcase} #{uri}" Telemetry::logger.error error Telemetry::logger.error response.body raise Telemetry::ServerException, error when "502" error = "HTTP 502: API server is down. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::Unavailable, error when "503" error = "HTTP 503: API server is down. #{method.upcase} #{uri}" Telemetry::logger.error error raise Telemetry::Unavailable, error else error = "ERROR UNK: #{method.upcase} #{uri} #{response.body}." raise Telemetry::UnknownError, error end rescue Errno::ETIMEDOUT => e error = "ERROR #{e}" Telemetry::logger.error error http.shutdown raise Telemetry::ConnectionError, error rescue Errno::ECONNREFUSED => e error = "ERROR #{e}" Telemetry::logger.error error http.shutdown raise Telemetry::ConnectionError, error rescue Exception => e http.shutdown raise e end end |