Skip to content

Commit

Permalink
Implement the log delivery
Browse files Browse the repository at this point in the history
* Specs included

* README updated
  • Loading branch information
karolsluszniak committed Dec 19, 2014
1 parent 316b285 commit 7c3231c
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Below you'll find sample settings for any of supported delivery methods along wi
# Send messages via the Twilio REST API
config.textris_delivery_method = :twilio

# Don't send anything, log messages into Rails logger
config.textris_delivery_method = :log

# Don't send anything, access your messages via Textris::Base.deliveries
config.textris_delivery_method = :test

Expand Down Expand Up @@ -153,6 +156,14 @@ Twilio.configure do |config|
end
```

#### Log

**textris** logger has similar logging behavior to ActionMailer. It will log single line to *info* log with production in mind and then a couple details to *debug* log. You can change the log level for the whole output:

```ruby
config.textris_log_level = :info
```

#### Custom delivery methods

Currently, **textris** comes with `twilio`, `mail` and `test` delivery methods built-in, but you can easily implement your own. Place desired delivery class in `app/deliveries/<name>_delivery.rb` (e.g. `app/deliveries/my_provider_delivery.rb`):
Expand Down
1 change: 1 addition & 0 deletions lib/textris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
require 'textris/delivery/base'
require 'textris/delivery/test'
require 'textris/delivery/mail'
require 'textris/delivery/log'
require 'textris/delivery/twilio'
29 changes: 29 additions & 0 deletions lib/textris/delivery/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Textris
module Delivery
class Log < Textris::Delivery::Base
AVAILABLE_LOG_LEVELS = %w{debug info warn error fatal unknown}

def deliver(to)
log :info, "Sent text to #{Phony.format(to)}"
log :debug, "Texter: #{message.texter || 'UnknownTexter'}" + "#" +
"#{message.action || 'unknown_action'}"
log :debug, "Date: #{Time.now}"
log :debug, "From: #{message.from || 'unknown'}"
log :debug, "To: #{message.to.map { |i| Phony.format(to) }.join(', ')}"
log :debug, "Content: #{message.content}"
end

private

def log(level, message)
level = Rails.application.config.try(:textris_log_level) || level

unless AVAILABLE_LOG_LEVELS.include?(level.to_s)
raise(ArgumentError, "Wrong log level: #{level}")
end

Rails.logger.send(level, message)
end
end
end
end
133 changes: 133 additions & 0 deletions spec/models/textris/delivery/log_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
describe Textris::Delivery::Log do
let(:message) do
Textris::Message.new(
:from => 'Mr Jones <+48 555 666 777>',
:to => ['+48 600 700 800', '48100200300'],
:content => 'Some text')
end

let(:delivery) { Textris::Delivery::Log.new(message) }
let(:logger) { FakeLogger.new }

before do
class FakeLogger
def log(kind = :all)
@log[kind.to_s] || ""
end

def method_missing(name, *args)
if Textris::Delivery::Log::AVAILABLE_LOG_LEVELS.include?(name.to_s)
@log ||= {}
@log[name.to_s] ||= ""
@log[name.to_s] += args[0] + "\n"
@log["all"] ||= ""
@log["all"] += args[0] + "\n"
end
end
end

Object.send(:remove_const, :Rails) if defined?(Rails)

Rails = OpenStruct.new(
:logger => logger,
:application => OpenStruct.new(
:config => OpenStruct.new
)
)
end

it 'responds to :deliver_to_all' do
expect(delivery).to respond_to(:deliver_to_all)
end

it 'prints proper delivery information to log' do
delivery.deliver_to_all

expect(logger.log(:info)).to include "Sent text to +48 600 700 800"
expect(logger.log(:info)).to include "Sent text to +48 10 020 03 00"

expect(logger.log(:debug)).to include "Date: "
expect(logger.log(:debug)).to include "To: +48 600 700 800, +48 600 700 800"
expect(logger.log(:debug)).to include "Texter: UnknownTexter#unknown_action"
expect(logger.log(:debug)).to include "From: Mr Jones <+48 55 566 67 77>"
expect(logger.log(:debug)).to include "Content: Some text"
end

it 'applies configured log level' do
Rails.application.config.textris_log_level = :unknown

delivery.deliver_to_all

expect(logger.log(:info)).to be_blank
expect(logger.log(:debug)).to be_blank
expect(logger.log(:unknown)).not_to be_blank
end

it 'throws error if configured log level is wrong' do
Rails.application.config.textris_log_level = :wronglevel

expect do
delivery.deliver_to_all
end.to raise_error(ArgumentError)
end

context "message with from name and no from phone" do
let(:message) do
Textris::Message.new(
:from => 'Mr Jones',
:to => ['+48 600 700 800', '48100200300'],
:content => 'Some text')
end

it 'prints proper delivery information to log' do
delivery.deliver_to_all

expect(logger.log).to include "From: Mr Jones"
end
end

context "message with from phone and no from name" do
let(:message) do
Textris::Message.new(
:from => '+48 55 566 67 77',
:to => ['+48 600 700 800', '48100200300'],
:content => 'Some text')
end

it 'prints proper delivery information to log' do
delivery.deliver_to_all

expect(logger.log).to include "From: +48 55 566 67 77"
end
end

context "message with no from" do
let(:message) do
Textris::Message.new(
:to => ['+48 600 700 800', '48100200300'],
:content => 'Some text')
end

it 'prints proper delivery information to log' do
delivery.deliver_to_all

expect(logger.log).to include "From: unknown"
end
end

context "message with texter and action" do
let(:message) do
Textris::Message.new(
:texter => "MyClass",
:action => "my_action",
:to => ['+48 600 700 800', '48100200300'],
:content => 'Some text')
end

it 'prints proper delivery information to log' do
delivery.deliver_to_all

expect(logger.log).to include "Texter: MyClass#my_action"
end
end
end

0 comments on commit 7c3231c

Please sign in to comment.