Class: Loggr::LogClient

Inherits:
Object
  • Object
show all
Defined in:
lib/loggr-rb/logclient.rb

Instance Method Summary collapse

Instance Method Details

#call_remote(host, path, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/loggr-rb/logclient.rb', line 31

def call_remote(host, path, params)
  http = Net::HTTP.new(host)
  req = Net::HTTP::Get.new(path)
  data = params.collect { |k, v| "#{k}=#{v}&" }.join
  http.start
  begin
    http.request_async(req, data)
		res = http.read_response(req)
  rescue Exception => e
    Loggr.logger.error('Problem notifying Loggr about the event')
    Loggr.logger.error(e)
  ensure
    http.finish
  end
  res.value # raise if error
end

#call_remote_async(host, path, params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/loggr-rb/logclient.rb', line 48

def call_remote_async(host, path, params)
  http = Net::HTTP.new(host)
  req = Net::HTTP::Get.new(path)
  data = params.collect { |k, v| "#{k}=#{v}&" }.join
  http.start
  begin
    http.request_async(req, data)
  rescue Exception => e
    Loggr.logger.error('Problem notifying Loggr about the event')
    Loggr.logger.error(e)
  ensure
    http.finish
  end
end

#create_params(e) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/loggr-rb/logclient.rb', line 15

def create_params(e)
     apikey = ::Loggr::Config.api_key
  params = {"apikey" => apikey, "text" => e.text}
  params = params.merge({"link" => e.link}) if !e.link.nil?
  params = params.merge({"tags" => e.tags}) if !e.tags.nil?
  params = params.merge({"source" => e.source}) if !e.source.nil?
  params = params.merge({"geo" => e.geo}) if !e.geo.nil?
  params = params.merge({"value" => e.value}) if !e.value.nil?
  if e.datatype == DataType::HTML
    params = params.merge({"data" => sprintf("@html\r\n%s", e.data)}) if !e.data.nil?
  else
    params = params.merge({"data" => e.data}) if !e.data.nil?
  end
  return params
end

#post(e, async = true) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/loggr-rb/logclient.rb', line 6

def post(e, async=true)
  logkey = ::Loggr::Config.log_key
	  if async == true
 call_remote_async("post.loggr.net", "/1/logs/#{logkey}/events", create_params(e))
	  else
 call_remote("post.loggr.net", "/1/logs/#{logkey}/events", create_params(e))
  end
end