Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ You can add optional interpolation modifiers using the `%{variable:modifiers}` s
- `h`: humanize (for instance, `user_name` becomes `User name`)
- `p`: format phone (for instance, `48111222333` becomes `+48 111 222 333`)

### URL Defaults

Textris uses `ActionController::Renderer` behind the scenes. Add or modify `config/initializers/application_controller_renderer.rb` in your Rails to change the default settings:

```
# Be sure to restart your server when you modify this file.

ActiveSupport::Reloader.to_prepare do
ApplicationController.renderer.defaults.merge!(
http_host: ENV['CANONICAL_HOST'], # or ActionMailer::Base.default_url_options[:host] to use the same host as ActionMailer
https: Rails.env.production?
)
end
```

## Example project

[Here](https://github.com/visualitypl/textris/tree/master/example/rails-4.2) you can find a simple example project that demonstrates **textris** usage with Rails 4.2. In order to see how it works or experiment with it, just go to project's directory and invoke:
Expand Down
29 changes: 12 additions & 17 deletions lib/textris/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
require 'render_anywhere'

module Textris
class Base
class RenderingController < RenderAnywhere::RenderingController
layout false

def default_url_options
ActionMailer::Base.default_url_options || {}
end
end

include RenderAnywhere
extend Textris::Delay::Sidekiq

class << self
Expand Down Expand Up @@ -53,9 +42,15 @@ def call_action
end

def render_content
set_instance_variables_for_rendering

render(:template => template_name, :formats => ['text'], :locale => @locale)
renderer = ::ActionController::Base.renderer.new

renderer.render(
template: template_name,
layout: false,
formats: [:text],
locale: @locale,
assigns: set_instance_variables_for_rendering
)
end

protected
Expand Down Expand Up @@ -84,9 +79,9 @@ def template_name
end

def set_instance_variables_for_rendering
instance_variables.each do |var|
set_instance_variable(var.to_s.sub('@', ''), instance_variable_get(var))
end
instance_variables.map do |var|
[var.to_s.sub('@', ''), instance_variable_get(var)]
end.to_h
end
end
end
2 changes: 1 addition & 1 deletion lib/textris/delivery/twilio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def deliver(to)
options[:media_url] = message.media_urls
end

client.messages.create(options)
client.messages.create(**options)
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/textris/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def parse_to(to)
def parse_content(content)
content = content.to_s
content = content.rstrip
content = CGI.unescapeHTML(content)

content
end
Expand Down
23 changes: 1 addition & 22 deletions spec/textris/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def set_instance_variable(key, value)
it 'defers template rendering when :body not provided' do
render_options = {}

expect_any_instance_of(MyTexter).not_to receive(:render)
expect_any_instance_of(MyTexter).not_to receive(:render_content)

MyTexter.action_with_template
end
Expand Down Expand Up @@ -151,25 +151,4 @@ def my_action(p)
expect(MyTexter.respond_to?(:fake_action)).to eq false
end
end

describe Textris::Base::RenderingController do
before do
class Textris::Base::RenderingController
def initialize(*args)
end
end

class ActionMailer::Base
def self.default_url_options
'x'
end
end
end

it 'maps default_url_options to ActionMailer configuration' do
rendering_controller = Textris::Base::RenderingController.new

expect(rendering_controller.default_url_options).to eq 'x'
end
end
end
11 changes: 10 additions & 1 deletion spec/textris/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@

expect(message.content).to eq(" a b. c")
end

it 'unescapes html entities' do
message = Textris::Message.new(
:content => "a&#39;b&#39;c&#39;d",
:from => 'X',
:to => '+48 111 222 333')

expect(message.content).to eq("a'b'c'd")
end
end

it 'raises if :to not provided' do
Expand Down Expand Up @@ -186,7 +195,7 @@ class SomeSampleTexter; end

describe '#content' do
before do
class Textris::Base::RenderingController
class ApplicationController < ActionController::Base
def initialize(*args)
end
end
Expand Down
4 changes: 1 addition & 3 deletions textris.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ Gem::Specification.new do |spec|
spec.description = "Implement texter classes for sending SMS messages in similar way to how e-mails are sent with ActionMailer-based mailers. Take advantage of e-mail proxying and enhanced phone number parsing, among others."

spec.files = Dir["lib/**/*.rb"]
spec.has_rdoc = false
spec.extra_rdoc_files = ["README.md"]
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.1'
Expand All @@ -36,5 +35,4 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'activejob', '>= 4.2'
spec.add_runtime_dependency 'activesupport', '>= 4.2'
spec.add_runtime_dependency 'phony', '~> 2.8'
spec.add_runtime_dependency 'render_anywhere', '~> 0.0'
end