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
- .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
125 126 127 128 129 130 131 132 |
# File 'lib/telemetry/api.rb', line 125 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
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/telemetry/api.rb', line 134 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
93 94 95 96 |
# File 'lib/telemetry/api.rb', line 93 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
98 99 100 101 |
# File 'lib/telemetry/api.rb', line 98 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
116 117 118 119 120 121 122 123 |
# File 'lib/telemetry/api.rb', line 116 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
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/telemetry/api.rb', line 103 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
77 78 79 |
# File 'lib/telemetry/api.rb', line 77 def self.delete(path) Telemetry::Api.send(:delete, path) end |
.delete_flow_data(id) ⇒ Object
89 90 91 |
# File 'lib/telemetry/api.rb', line 89 def self.delete_flow_data(id) Telemetry::Api.send(:delete, "/flows/#{id}/data") end |
.flow_update(flow) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/telemetry/api.rb', line 147 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
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/telemetry/api.rb', line 156 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
65 66 67 |
# File 'lib/telemetry/api.rb', line 65 def self.get(path) Telemetry::Api.send(:get, path) end |
.get_flow(id) ⇒ Object
81 82 83 |
# File 'lib/telemetry/api.rb', line 81 def self.get_flow(id) Telemetry::Api.send(:get, "/flows/#{id}") end |
.get_flow_data(id) ⇒ Object
85 86 87 |
# File 'lib/telemetry/api.rb', line 85 def self.get_flow_data(id) Telemetry::Api.send(:get, "/flows/#{id}/data") end |
.parse_response(response) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/telemetry/api.rb', line 285 def self.parse_response(response) begin resp_hash = MultiJson.load(response.body) return nil unless resp_hash && resp_hash.is_a?(Hash) if resp_hash["errors"] && resp_hash["errors"].is_a?(Array) && resp_hash["errors"].count > 0 Telemetry::logger.error "Errors: #{resp_hash['errors'].join(', ')}" end if resp_hash["updated"] && resp_hash["updated"].is_a?(Array) && resp_hash["updated"].count > 0 Telemetry::logger.debug "Updated: #{resp_hash['updated'].join(', ')}" end if resp_hash["skipped"] && resp_hash["skipped"].is_a?(Array) && resp_hash["skipped"].count > 0 Telemetry::logger.error "Skipped: #{resp_hash['skipped'].join(', ')}" end return resp_hash rescue Exception => e return nil end end |
.patch(path, body) ⇒ Object
73 74 75 |
# File 'lib/telemetry/api.rb', line 73 def self.patch(path, body) Telemetry::Api.send(:patch, path, body) end |
.post(path, body) ⇒ Object
69 70 71 |
# File 'lib/telemetry/api.rb', line 69 def self.post(path, body) Telemetry::Api.send(:post, path, body) end |
.send(method, endpoint, data = nil) ⇒ Object
168 169 170 171 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 |
# File 'lib/telemetry/api.rb', line 168 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 raise Telemetry::ConnectionError, error rescue Errno::ECONNREFUSED => e error = "ERROR #{e}" Telemetry::logger.error error raise Telemetry::ConnectionError, error rescue Exception => e raise e #ensure #http.shutdown end end |