Class: StatsMix
- Inherits:
-
Object
- Object
- StatsMix
- Defined in:
- lib/statsmix.rb
Constant Summary collapse
- HOST =
'api.statsmix.com'
- BASE_URI =
"http://#{HOST}/api/v2/"
- UDP_PORT =
8125
- GEM_VERSION =
File.exist?('VERSION') ? File.read('VERSION') : ""
Class Method Summary collapse
- .api_from_env ⇒ Object
-
.api_key ⇒ Object
Returns: string.
- .api_key=(string) ⇒ Object
-
.create_metric(name, params = {}) ⇒ Object
Create metric.
-
.create_stat(metric_id, value = nil, params = {}) ⇒ Object
Create stat.
-
.create_user(params = {}) ⇒ Object
Create user.
-
.debugging ⇒ Object
Returns: boolean.
-
.debugging=(value) ⇒ Object
Expects: boolean.
-
.delete_metric(metric_id) ⇒ Object
Delete metric.
-
.delete_stat(stat_id) ⇒ Object
Delete stat.
-
.delete_user(id_or_api_key) ⇒ Object
Delete user.
-
.error ⇒ Object
Returns: string or boolean false.
-
.format ⇒ Object
Returns: string.
- .format=(string) ⇒ Object
-
.get_metric(metric_id) ⇒ Object
Get metric.
-
.get_stat(stat_id) ⇒ Object
Get stat.
-
.get_user(id_or_api_key) ⇒ Object
Get user.
-
.ignore ⇒ Object
Returns: boolean.
- .ignore=(boolean) ⇒ Object
-
.list_metrics(profile_id = nil, limit = nil) ⇒ Object
List metrics.
-
.list_stats(metric_id, limit = nil, options = {}) ⇒ Object
List stats (index).
-
.list_users ⇒ Object
list users.
-
.params ⇒ Object
Returns: hash.
-
.request_uri ⇒ Object
Returns: string.
-
.response ⇒ Object
Returns: Net::HTTP object.
-
.test_metric_name ⇒ Object
Returns: string or nil.
- .test_metric_name=(name) ⇒ Object
-
.track(name, value = nil, options = {}) ⇒ Object
Track an event.
-
.update_metric(metric_id, params = {}) ⇒ Object
Update metric.
-
.update_stat(stat_id, value = nil, params = {}) ⇒ Object
Update stat.
-
.update_user(id_or_api_key, params = {}) ⇒ Object
Update user.
-
.use_udp ⇒ Object
Returns: boolean.
-
.use_udp=(value) ⇒ Object
Expects: boolean.
-
.user_api_key ⇒ Object
Return: string.
Class Method Details
.api_from_env ⇒ Object
350 351 352 353 354 355 |
# File 'lib/statsmix.rb', line 350 def self.api_from_env return nil if ENV['STATSMIX_URL'].nil? url = ENV['STATSMIX_URL'] pieces = url.gsub('http://','').gsub('https://','').split('/') @api_key = pieces[2] end |
.api_key ⇒ Object
Returns: string
297 298 299 |
# File 'lib/statsmix.rb', line 297 def self.api_key @api_key end |
.api_key=(string) ⇒ Object
292 293 294 |
# File 'lib/statsmix.rb', line 292 def self.api_key=(string) @api_key = string end |
.create_metric(name, params = {}) ⇒ Object
Create metric
Required: name Optional: params[:profile_id, :sharing, :include_in_email] Returns: Net::HTTP object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/statsmix.rb', line 159 def self.create_metric(name, params = {}) if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('metrics') @params.merge!(params) @params[:name] = name @request_uri = @url.path + '.' + @format @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.create_stat(metric_id, value = nil, params = {}) ⇒ Object
Create stat
Required: metric_id Optional: value, params[:generated_at, :meta] Returns: Net::HTTP object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/statsmix.rb', line 80 def self.create_stat(metric_id, value = nil, params = {}) if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('stats') @request_uri = @url.path + '.' + @format @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) @params[:metric_id] = metric_id @params[:value] = value if value @params.merge!(params) self. return do_request end |
.create_user(params = {}) ⇒ Object
Create user
Required: => ‘[email protected]’ - if the email already exists in our system, we will return the existing user Optional: :email,:name,:plan,:company,:url, :metrics[] Returns: Net::HTTP object
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/statsmix.rb', line 232 def self.create_user(params = {}) connect('partners/users') @request_uri = @url.path + '.' + @format @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) @params.merge!(params) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end return result end |
.debugging ⇒ Object
Returns: boolean
336 337 338 |
# File 'lib/statsmix.rb', line 336 def self.debugging @debugging end |
.debugging=(value) ⇒ Object
Expects: boolean
331 332 333 |
# File 'lib/statsmix.rb', line 331 def self.debugging=(value) @debugging = value end |
.delete_metric(metric_id) ⇒ Object
Delete metric
Required: metric_id Optional: none Returns: Net::HTTP object
194 195 196 197 198 199 |
# File 'lib/statsmix.rb', line 194 def self.delete_metric(metric_id) connect('metrics') @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.delete_stat(stat_id) ⇒ Object
Delete stat
Required: stat_id Optional: none Returns: Net::HTTP object
117 118 119 120 121 122 |
# File 'lib/statsmix.rb', line 117 def self.delete_stat(stat_id) connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.delete_user(id_or_api_key) ⇒ Object
Delete user
Required: id_or_api_key - id or api key of the user to get Optional: none Returns: Net::HTTP object
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/statsmix.rb', line 266 def self.delete_user(id_or_api_key) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end return result end |
.error ⇒ Object
Returns: string or boolean false
283 284 285 |
# File 'lib/statsmix.rb', line 283 def self.error @error end |
.format ⇒ Object
Returns: string
366 367 368 |
# File 'lib/statsmix.rb', line 366 def self.format @format end |
.format=(string) ⇒ Object
357 358 359 360 361 362 363 |
# File 'lib/statsmix.rb', line 357 def self.format=(string) string.downcase! if string != 'json' && string != 'xml' raise "format MUST be either xml or json" end @format = string end |
.get_metric(metric_id) ⇒ Object
Get metric
Required: metric_id Optional: none Returns: Net::HTTP object
147 148 149 150 151 152 |
# File 'lib/statsmix.rb', line 147 def self.get_metric(metric_id) connect('metrics') @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.get_stat(stat_id) ⇒ Object
Get stat
Required: stat_id Optional: none Returns: Net::HTTP object
68 69 70 71 72 73 |
# File 'lib/statsmix.rb', line 68 def self.get_stat(stat_id) connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.get_user(id_or_api_key) ⇒ Object
Get user
Required: id_or_api_key - id or api key of the user to get Optional: none Returns: Net::HTTP object
220 221 222 223 224 225 |
# File 'lib/statsmix.rb', line 220 def self.get_user(id_or_api_key) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.ignore ⇒ Object
Returns: boolean
306 307 308 |
# File 'lib/statsmix.rb', line 306 def self.ignore @ignore end |
.ignore=(boolean) ⇒ Object
301 302 303 |
# File 'lib/statsmix.rb', line 301 def self.ignore=(boolean) @ignore = boolean ? true : false end |
.list_metrics(profile_id = nil, limit = nil) ⇒ Object
List metrics
Required: none Optional: profile_id, limit Returns: Net::HTTP object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/statsmix.rb', line 131 def self.list_metrics(profile_id = nil, limit = nil) connect('metrics') @request_uri = @url.path + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:profile_id] = profile_id if profile_id != nil @params[:limit] = limit if limit != nil return do_request end |
.list_stats(metric_id, limit = nil, options = {}) ⇒ Object
List stats (index)
Required: metric_id Optional: limit, => nil, :end_date => nil Use start_date and end_date as has keys in the third param to scope the date range of stats based on the generated_at timestamp of a stat Returns: Net::HTTP object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/statsmix.rb', line 44 def self.list_stats(metric_id, limit = nil, = {}) if .respond_to?('with_indifferent_access') = .with_indifferent_access end self.connect('stats') @request_uri = @url.path + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:metric_id] = metric_id @params.merge!() if @params[:start_date] && @params[:start_date].is_a?(ActiveSupport::TimeWithZone) @params[:start_date] = @params[:start_date].utc.strftime('%Y-%m-%d %H:%M:%S') end if @params[:end_date] && @params[:end_date].is_a?(ActiveSupport::TimeWithZone) @params[:end_date] = @params[:end_date].utc.strftime('%Y-%m-%d %H:%M:%S') end return do_request end |
.list_users ⇒ Object
list users
Optional: none Returns: Net::HTTP object
208 209 210 211 212 213 |
# File 'lib/statsmix.rb', line 208 def self.list_users connect('partners/users') @request_uri = @url.path @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.params ⇒ Object
Returns: hash
288 289 290 |
# File 'lib/statsmix.rb', line 288 def self.params @params end |
.request_uri ⇒ Object
Returns: string
371 372 373 |
# File 'lib/statsmix.rb', line 371 def self.request_uri @request_uri end |
.response ⇒ Object
Returns: Net::HTTP object
278 279 280 |
# File 'lib/statsmix.rb', line 278 def self.response @response end |
.test_metric_name ⇒ Object
Returns: string or nil
320 321 322 |
# File 'lib/statsmix.rb', line 320 def self.test_metric_name @test_metric_name end |
.test_metric_name=(name) ⇒ Object
315 316 317 |
# File 'lib/statsmix.rb', line 315 def self.test_metric_name=(name) @test_metric_name = name end |
.track(name, value = nil, options = {}) ⇒ Object
Track an event
Required: name of metric Optional: value, options :generated_at Returns: Net::HTTP object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/statsmix.rb', line 19 def self.track(name, value = nil, = {}) if .respond_to?('with_indifferent_access') = .with_indifferent_access end self.connect('track') @request_uri = @url.path + '.' + @format @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:name] = name if @test_metric_name @params[:name] = @test_metric_name end @params[:value] = Float(value) unless value.nil? @params.merge!() self. return do_request end |
.update_metric(metric_id, params = {}) ⇒ Object
Update metric
Required: metric_id Optional: params[:profile_id, :sharing, :include_in_email] Returns: Net::HTTP object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/statsmix.rb', line 176 def self.update_metric(metric_id, params = {}) if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('metrics') @params = [] if @params.nil? @params.merge!(params) @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end |
.update_stat(stat_id, value = nil, params = {}) ⇒ Object
Update stat
Required: stat_id Optional: value, params[:generated_at, :meta] Returns: Net::HTTP object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/statsmix.rb', line 99 def self.update_stat(stat_id, value = nil, params = {}) if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) @params[:value] = value if value @params.merge!(params) self. return do_request end |
.update_user(id_or_api_key, params = {}) ⇒ Object
Update user
Required: id_or_api_key - id or api key of the user to get Optional: params with keys :email,:name,:plan,:company,:url Returns: Net::HTTP object
249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/statsmix.rb', line 249 def self.update_user(id_or_api_key, params = {}) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) @params.merge!(params) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end return result end |
.use_udp ⇒ Object
Returns: boolean
346 347 348 |
# File 'lib/statsmix.rb', line 346 def self.use_udp @use_udp end |
.use_udp=(value) ⇒ Object
Expects: boolean
341 342 343 |
# File 'lib/statsmix.rb', line 341 def self.use_udp=(value) @use_udp = value end |
.user_api_key ⇒ Object
Return: string
311 312 313 |
# File 'lib/statsmix.rb', line 311 def self.user_api_key @user_api_key end |