Commit 0dc04b8f authored by sergio1990's avatar sergio1990

Merge branch 'logging'

parents 62e550be 8755b864
require "rubygems"
# require 'rspec/core/rake_task'
# require 'rubygems/specification'
require 'rspec/core/rake_task'
require 'rubygems/specification'
# task default: :spec
# desc "Run specs"
# RSpec::Core::RakeTask.new do |t|
# t.pattern = FileList['spec/**/*_spec.rb']
# t.rspec_opts = %w(--color)
# end
task default: :spec
desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = FileList['spec/**/*_spec.rb']
t.rspec_opts = %w(--color)
end
......@@ -18,10 +18,12 @@ 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_dependency "colorize", "0.7.2"
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'
......@@ -125,6 +132,7 @@ module BetfairApiNgRails
autoload :ErrorHandling, 'betfair_api_ng_rails/api/connection_ext/error_handling'
autoload :Parsing, 'betfair_api_ng_rails/api/connection_ext/parsing'
autoload :Formatting, 'betfair_api_ng_rails/api/connection_ext/formatting'
autoload :Logging, 'betfair_api_ng_rails/api/connection_ext/logging'
end
......@@ -133,6 +141,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
......
......@@ -14,6 +14,7 @@ module BetfairApiNgRails
provider.fetch method: method, params: hashing(params)
end
include Api::ConnectionExt::Logging
include Api::ConnectionExt::ErrorHandling
include Api::ConnectionExt::Parsing
include Api::ConnectionExt::Formatting
......
require 'active_support/inflector'
module BetfairApiNgRails
module Api
module ConnectionExt
module Logging
include BetfairApiNgRails::Api::Constants
def self.included(base)
base.send :class_eval, <<-CODE
def request_with_logging(method, params = {})
write_income_request method, params
request_without_logging(method, params).tap { |res| write_result(res) }
end
alias_method :request_without_logging, :request
alias_method :request, :request_with_logging
CODE
end
private
def write_income_request(method, params)
BetfairApiNgRails.log.write("--->Income request for #{method} with parameters #{hashing(params)}")
end
def write_result(res)
BetfairApiNgRails.log.write(" Result of request: #{res.result}")
end
end
end
end
end
\ No newline at end of file
require 'colorize'
module BetfairApiNgRails
module Api
module Logs
class ConsoleLogger
def write(content)
puts content.green
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
......@@ -17,18 +17,21 @@ describe BetfairApiNgRails::Api::Connection do
let(:provider) { double(:provider) }
let(:parser) { double(:parser, process: true) }
let(:response) { double(:response) }
let(:response) { double(:response, result: "") }
let(:logger) { double(:logger) }
before(:each) do
expect_any_instance_of(described_class).to receive(:request_ssoid)
expect(subject).to receive(:init_parser).and_return parser
BetfairApiNgRails.log = logger
allow(logger).to receive(:write)
end
it "calls provider's fetch method" do
expect(subject).to receive(:provider).and_return provider
expect(provider).to receive(:fetch).with(method: 'method', params: {}).and_return response
expect(response).to receive(:has_error?).and_return false
expect(subject).to receive(:hashing).with({}).and_return({})
allow(subject).to receive(:hashing).with({}).and_return({})
subject.request 'method'
end
......
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
shared_examples 'simple list filtering request' do |method|
let(:parameters) { {filter: filter} }
let(:logger) { double(:logger) }
before(:each) { BetfairApiNgRails.config.formatter = nil }
before(:each) do
BetfairApiNgRails.config.formatter = nil
BetfairApiNgRails.log = logger
allow(logger).to receive(:write)
end
context 'when no error occured' do
......
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