GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#27)
by
unknown
02:43
created

Mail.from_format()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1 1
module Textris
2 1
  module Delivery
3 1
    class Mail < Textris::Delivery::Base
4 1
      class Mailer < ActionMailer::Base
5 1
        def notify(from, to, subject, body)
6 1
          mail :from => from, :to => to, :subject => subject, :body => body
7
        end
8
      end
9
10 1
      def deliver(to)
11 14
        template_vars = { :to_phone => to }
12
13 14
        from    = apply_template from_template,    template_vars
14 14
        to      = apply_template to_template,      template_vars
15 14
        subject = apply_template subject_template, template_vars
16 14
        body    = apply_template body_template,    template_vars
17
18
        ::Textris::Delivery::Mail::Mailer.notify(
19 14
          from, to, subject, body).deliver
20
      end
21
22 1
      private
23
24 1
      def from_template
25
        Rails.application.config.try(:textris_mail_from_template) ||
26 14
          "#{from_format}@%{env:d}.%{app:d}.com"
27
      end
28
29 1
      def from_format
30 12
        if message.twilio_messaging_service_sid
31 2
          '%{twilio_messaging_service_sid}'
32
        else
33 10
          '%{from_name:d}-%{from_phone}'
34
        end
35
      end
36
37 1
      def to_template
38
        Rails.application.config.try(:textris_mail_to_template) ||
39 14
          "%{app:d}-%{env:d}-%{to_phone}[email protected]"
40
      end
41
42 1
      def subject_template
43
        Rails.application.config.try(:textris_mail_subject_template) ||
44 14
          "%{texter:dh} texter: %{action:h}"
45
      end
46
47 1
      def body_template
48
        Rails.application.config.try(:textris_mail_body_template) ||
49 14
          "%{content}"
50
      end
51
52 1
      def apply_template(template, variables)
53 56
        template.gsub(/\%\{[a-z_:]+\}/) do |match|
54 142
          directive = match.gsub(/[%{}]/, '')
55 142
          key       = directive.split(':').first
56 142
          modifiers = directive.split(':')[1] || ''
57
58 142
          content = get_template_interpolation(key, variables)
59 142
          content = apply_template_modifiers(content, modifiers.chars)
60 142
          content = 'unknown' unless content.present?
61
62 142
          content
63
        end
64
      end
65
66 1
      def get_template_interpolation(key, variables)
67
        case key
68
        when 'app', 'env'
69 46
          get_rails_variable(key)
70
        when 'texter', 'action', 'from_name', 'from_phone', 'content', 'twilio_messaging_service_sid'
71 84
          message.send(key)
72
        when 'media_urls'
73 2
          message.media_urls.join(', ')
74
        else
75 10
          variables[key.to_sym]
76 142
        end.to_s.strip
77
      end
78
79 1
      def get_rails_variable(var)
80 46
        case var
81
        when 'app'
82 24
          Rails.application.class.parent_name
83
        when 'env'
84 22
          Rails.env
85
        end
86
      end
87
88 1
      def apply_template_modifiers(content, modifiers)
89 142
        modifiers.each do |modifier|
90 94
          case modifier
91
          when 'd'
92 62
            content = content.underscore.dasherize
93
          when 'h'
94 28
            content = content.humanize.gsub(/[-_]/, ' ')
95
          when 'p'
96 2
            content = Phony.format(content) rescue content
97
          end
98
        end
99
100 142
        content
101
      end
102
    end
103
  end
104
end
105