diff --git a/README.md b/README.md index 66d37e5..62e125b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/textris/base.rb b/lib/textris/base.rb index 92c0070..00c452c 100644 --- a/lib/textris/base.rb +++ b/lib/textris/base.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/textris/delivery/twilio.rb b/lib/textris/delivery/twilio.rb index c51e0b8..172ad91 100644 --- a/lib/textris/delivery/twilio.rb +++ b/lib/textris/delivery/twilio.rb @@ -17,7 +17,7 @@ def deliver(to) options[:media_url] = message.media_urls end - client.messages.create(options) + client.messages.create(**options) end private diff --git a/lib/textris/message.rb b/lib/textris/message.rb index c636ce9..b898b09 100644 --- a/lib/textris/message.rb +++ b/lib/textris/message.rb @@ -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 diff --git a/spec/textris/base_spec.rb b/spec/textris/base_spec.rb index 651591b..3580200 100644 --- a/spec/textris/base_spec.rb +++ b/spec/textris/base_spec.rb @@ -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 @@ -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 diff --git a/spec/textris/message_spec.rb b/spec/textris/message_spec.rb index b44ef1a..8fc0543 100644 --- a/spec/textris/message_spec.rb +++ b/spec/textris/message_spec.rb @@ -137,6 +137,15 @@ expect(message.content).to eq(" a b. c") end + + it 'unescapes html entities' do + message = Textris::Message.new( + :content => "a'b'c'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 @@ -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 diff --git a/textris.gemspec b/textris.gemspec index 9d34a16..d68b10f 100644 --- a/textris.gemspec +++ b/textris.gemspec @@ -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' @@ -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