Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiren Patel committed Oct 10, 2013
1 parent 3acf1b6 commit 43ac7c2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
13 changes: 7 additions & 6 deletions lib/rnotifier/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def init
return unless self.init_env && self.init_api_options

self.init_igonore_options
self.app_env = get_app_env(:complete)
self.app_env = detailed_env
self.valid = true
end

Expand Down Expand Up @@ -75,18 +75,19 @@ def init_api_options
end
end

def get_app_env(type = :complete)
env = {
def basic_env
{
:env => Config.current_env,
:pid => Process.pid,
:host => (Socket.gethostname rescue ''),
:language => 'ruby',
:time_zone => (Time.now.to_s.split.last rescue nil),
:client => CLIENT
}

return env unless type == :complete
env.merge({
end

def detailed_env
basic_env.merge({
:user_name => ENV['USER'] || ENV['USERNAME'],
:program_name => $PROGRAM_NAME,
:app_root => self.app_root,
Expand Down
23 changes: 12 additions & 11 deletions lib/rnotifier/exception_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ def notify
return false if options[:type] == :rack && is_bot?(request.user_agent)

begin
data = options[:type] == :rack ? self.rack_exception_data : {:extra => self.env }
data[:env] = Rnotifier::Config.app_env
data[:occurred_at] = Time.now.to_i
data[:exception] = self.exception_data
data[:context_data] = Thread.current[:rnotifier_context] if Thread.current[:rnotifier_context]
data[:data_from] = options[:type]

return Notifier.send_data(data, Config.exception_path)
return Notifier.send_data(self.data, Config.exception_path)
rescue Exception => e
Rlogger.error("[NOTIFY] #{e.message}")
Rlogger.error("[NOTIFY] #{e.backtrace}")
false
Rlogger.exception(e, 'NOTIFY')
end
end

def data
data = options[:type] == :rack ? self.rack_exception_data : {:extra => self.env }
data[:env] = Rnotifier::Config.app_env
data[:occurred_at] = Time.now.to_i
data[:exception] = self.exception_data
data[:context_data] = Thread.current[:rnotifier_context] if Thread.current[:rnotifier_context]
data[:data_from] = options[:type]
data
end

def rack_exception_data
data = {}
data[:request] = {
Expand Down
8 changes: 3 additions & 5 deletions lib/rnotifier/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ def notify(data, path)
return false unless Config.valid?

begin
Notifier.send_data(data, path)
return Notifier.send_data(data, path)
rescue Exception => e
Rlogger.error("[EVENT NOTIFY] #{e.message}")
Rlogger.error("[EVENT NOTIFY] #{e.backtrace}")
false
Rlogger.exception(e, 'EVENT NOTIFY')
end
end

Expand All @@ -44,7 +42,7 @@ def bulk_notify(messages)
end

def app_env
@app_env ||= Config.get_app_env(:short)
@app_env ||= Config.basic_env
end
end

Expand Down
5 changes: 0 additions & 5 deletions lib/rnotifier/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Rnotifier
class Notifier
class << self

attr_accessor :json_encoder

def connection
@connection ||= Faraday.new(:url => Config.api_host) do |faraday|
faraday.adapter Faraday.default_adapter
Expand All @@ -18,8 +16,6 @@ def send_data(data, path)
req.options[:timeout] = Config[:http_open_timeout]
req.options[:open_timeout] = Config[:http_read_timeout]
req.body = Yajl::Encoder.new.encode(data)
#req.body = MultiJson.dump(data)
#req.body = Rnotifier::Notifier.json_encoder.encode(data)
end

return true if response.status == 200
Expand All @@ -30,6 +26,5 @@ def send_data(data, path)
end
end

self.json_encoder = Yajl::Encoder.new
end
end
6 changes: 6 additions & 0 deletions lib/rnotifier/rlogger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def logger
@logger
end

def exception(e, tag = '')
self.error("[#{tag}] #{e.message}")
self.error("[#{tag}] #{e.backtrace}")
return false
end

end
end
end
2 changes: 1 addition & 1 deletion spec/rnotifier/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
end

it 'app env should have time zone' do
env = Rnotifier::Config.get_app_env
env = Rnotifier::Config.basic_env
expect(env[:time_zone]).not_to be_nil
end

Expand Down

0 comments on commit 43ac7c2

Please sign in to comment.