Class: DogapiDemo::V1::EventService

Inherits:
APIService show all
Defined in:
lib/dogapi-demo/v1/event.rb

Overview

Event-specific client affording more granular control than the simple DogapiDemo::Client

Constant Summary collapse

API_VERSION =
"v1"
MAX_BODY_LENGTH =
4000
MAX_TITLE_LENGTH =
100

Instance Method Summary collapse

Methods inherited from APIService

#connect, #initialize, #request, #suppress_error_if_silent

Constructor Details

This class inherits a constructor from DogapiDemo::APIService

Instance Method Details

#get(id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dogapi-demo/v1/event.rb', line 41

def get(id)
  begin
    params = {
      :api_key => @api_key,
      :application_key => @application_key
    }

    request(Net::HTTP::Get, '/api/' + API_VERSION + '/events/' + id.to_s, params, nil, false)
  rescue Exception => e
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end

#post(event, scope = nil) ⇒ Object

Records an Event with no duration



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dogapi-demo/v1/event.rb', line 14

def post(event, scope=nil)
  begin
    scope = scope || DogapiDemo::Scope.new()
    params = {
      :api_key => @api_key
    }

    body = event.to_hash.merge({
      :title => event.msg_title[0..MAX_TITLE_LENGTH - 1],
      :text => event.msg_text[0..MAX_BODY_LENGTH - 1],
      :date_happened => event.date_happened.to_i,
      :host => scope.host,
      :device => scope.device,
      :aggregation_key => event.aggregation_key.to_s
    })

    request(Net::HTTP::Post, '/api/v1/events', params, body, true)
  rescue Exception => e
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end

#stream(start, stop, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dogapi-demo/v1/event.rb', line 59

def stream(start, stop, options = {})
  begin
    defaults = {
      :priority => nil,
      :sources => nil,
      :tags => nil
    }
    options = defaults.merge(options)

    params = {
      :api_key => @api_key,
      :application_key => @application_key,

      :start => start.to_i,
      :end => stop.to_i
    }

    if options[:priority]
      params[:priority] = options[:priority]
    end
    if options[:sources]
      params[:sources] = options[:sources]
    end
    if options[:tags]
      tags = options[:tags]
      params[:tags] = tags.kind_of?(Array) ? tags.join(",") : tags
    end

    request(Net::HTTP::Get, '/api/' + API_VERSION + '/events', params, nil, false)
  rescue Exception => e
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end