Commit 29630b75 authored by sergio1990's avatar sergio1990

Added logging classes and its specs

Ref #13
parent 62e550be
......@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake", ">= 10.3.0"
spec.add_development_dependency "activesupport", ">= 3.1.0"
spec.add_development_dependency "rspec", "~> 3.0.0.beta"
spec.add_development_dependency "rspec-its"
spec.add_development_dependency "pry-nav"
spec.add_development_dependency "rspec", "~> 3.0.0.beta"
spec.add_development_dependency "rspec-its", "1.0.1"
spec.add_development_dependency "pry-nav", "0.2.3"
end
\ No newline at end of file
......@@ -19,6 +19,13 @@ module BetfairApiNgRails
autoload :Connection, 'betfair_api_ng_rails/api/connection'
autoload :Hashalator, 'betfair_api_ng_rails/api/hashalator'
module Logs
autoload :FileLogger, 'betfair_api_ng_rails/api/logs/file_logger'
autoload :ConsoleLogger, 'betfair_api_ng_rails/api/logs/console_logger'
end
module Formatters
autoload :JsTreeFormatter, 'betfair_api_ng_rails/api/formatters/js_tree_formatter'
......@@ -133,6 +140,10 @@ module BetfairApiNgRails
include Api::Data
extend Api::RequestMethods
class << self
attr_accessor :log
end
def self.config(&block)
@_config ||= BetfairApiNgRails::Api::Config
block_given? ? yield(@_config) : @_config
......
module BetfairApiNgRails
module Api
module Logs
class ConsoleLogger
def write(content)
puts content
end
end
end
end
end
\ No newline at end of file
module BetfairApiNgRails
module Api
module Logs
class FileLogger
attr_reader :root, :env
def initialize(root, env)
@root, @env = root, env
end
def write(content)
File.open(file_path, 'w') { |file| file.write(content) }
end
private
def file_path
@_log_file_path ||= "#{root}/log/betfair_api_ng_rails.#{env}.log"
end
end
end
end
end
\ No newline at end of file
......@@ -2,6 +2,12 @@ module BetfairApiNgRails
class Railtie < Rails::Railtie
config.after_initialize do
BetfairApiNgRails.connection = BetfairApiNgRails::Api::Connection.new
BetfairApiNgRails.log = BetfairApiNgRails::Api::Logs::FileLogger.new Rails.root, Rails.env
end
console do
BetfairApiNgRails.log = BetfairApiNgRails::Api::Logs::ConsoleLogger.new
end
end
end
\ No newline at end of file
require 'spec_helper'
describe BetfairApiNgRails::Api::Logs::ConsoleLogger do
subject(:object) { described_class.new }
describe "#write" do
let(:string) { "some content" }
it "puts content to console" do
expect(object).to receive(:puts).with string
object.write string
end
end
end
\ No newline at end of file
require 'spec_helper'
describe BetfairApiNgRails::Api::Logs::FileLogger do
let(:root) { 'root_path_of_rails_app' }
let(:env) { 'some_env' }
subject(:object) { described_class.new(root, env) }
describe "#write" do
let(:file) { double(:file) }
let(:string) { "some text" }
it "writes passed string to log file" do
expect(File).to receive(:open).and_yield file
expect(file).to receive(:write).with(string)
object.write string
end
end
describe "private method" do
describe "#file_path" do
subject { object.send(:file_path) }
it { is_expected.to include root }
it { is_expected.to include env }
it { is_expected.to eq "#{root}/log/betfair_api_ng_rails.#{env}.log" }
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment