Class: Lcms::Engine::SVGSocialThumbnail

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
app/services/lcms/engine/svg_social_thumbnail.rb

Constant Summary collapse

SIZE_MAP =
{
  all: { width: 600, height: 600 },
  facebook: { width: 1200, height: 627 },
  pinterest: { width: 600, height: 800 },
  twitter: { width: 440, height: 220 }
}.with_indifferent_access
COLOR_CODES =
{
  ela_base: '#f75b28',
  ela_pk: '#fda43a',
  ela_k: '#fc9837',
  ela_1: '#fb8d32',
  ela_2: '#f7802c',
  ela_3: '#f97529',
  ela_4: '#f96924',
  ela_5: '#f85e20',
  ela_6: '#f0501a',
  ela_7: '#e94d1a',
  ela_8: '#e14b19',
  ela_9: '#da4818',
  ela_10: '#d34617',
  ela_11: '#cc4417',
  ela_12: '#c54116',

  math_base: '#00a699',
  math_pk: '#69d59d',
  math_k: '#5bcf9d',
  math_1: '#4cc89c',
  math_2: '#3cc19b',
  math_3: '#2ebb9b',
  math_4: '#1eb49a',
  math_5: '#0fad9a',
  math_6: '#009d90',
  math_7: '#009488',
  math_8: '#008b7f',
  math_9: '#008277',
  math_10: '#00796e',
  math_11: '#007066',
  math_12: '#00675d'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, media: nil) ⇒ SVGSocialThumbnail

Returns a new instance of SVGSocialThumbnail.



51
52
53
54
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 51

def initialize(model, media: nil)
  @resource = model
  @media = media || :all
end

Instance Attribute Details

#mediaObject (readonly)

Returns the value of attribute media.



49
50
51
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 49

def media
  @media
end

#resourceObject (readonly)

Returns the value of attribute resource.



49
50
51
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 49

def resource
  @resource
end

Class Method Details

.base64_cacheObject



75
76
77
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 75

def self.base64_cache
  @base64_cache ||= {}
end

.templateObject



60
61
62
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 60

def self.template
  @template ||= File.read Rails.root.join('app', 'views', 'shared', 'social_thumbnail.svg.erb')
end

Instance Method Details

#asset_path(asset) ⇒ Object

Helper methods



71
72
73
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 71

def asset_path(asset)
  ApplicationController.helpers.asset_path(asset)
end

#base64_cacheObject



79
80
81
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 79

def base64_cache
  self.class.base64_cache
end

#base64_encoded_asset(asset) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 83

def base64_encoded_asset(asset)
  base64_cache.fetch asset do
    encoded = ApplicationController.helpers.base64_encoded_asset(asset)
    if asset.match?(/\.ttf$/)
      encoded.gsub!('data:application/x-font-ttf;base64,', 'data:font/truetype;charset=utf-8;base64,')
    end
    base64_cache[asset] = encoded
  end
end

#color_codeObject



217
218
219
220
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 217

def color_code
  grade = resource.grades.average rescue :base
  COLOR_CODES[:"#{resource.subject}_#{grade}"]
end

#content_typeObject



186
187
188
189
190
191
192
193
194
195
196
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 186

def content_type
  if resource.generic? || resource.media?
    I18n.t("resource_types.#{resource.resource_type}").upcase
  elsif resurce.unit? && resource.short_title.match(/topic/i)
    'TOPIC'
  elsif resource.lesson?
    'LESSON PLAN'
  else
    resource.curriculum_type.try(:upcase)
  end
end

#emObject



111
112
113
114
115
116
117
118
119
120
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 111

def em
  case media
  when :twitter then
    12
  when :facebook then
    26
  else
    22
  end
end


102
103
104
105
106
107
108
109
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 102

def footer_size
  case media
  when :twitter then
    50
  else
    100
  end
end

#header_with_two_lines?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 198

def header_with_two_lines?
  media == :pinterest && resource.try(:quick_reference_guide?)
end

#logo_sizeObject



97
98
99
100
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 97

def logo_size
  proportion = footer_size / 100.0
  { width: 220 * proportion, height: 30 * proportion }
end

#number_of_linesObject



152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 152

def number_of_lines
  case media
  when :facebook then
    4
  when :pinterest then
    (header_with_two_lines? ? 6 : 7)
  when :twitter then
    3
  else
    6
  end
end

#renderObject



56
57
58
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 56

def render
  @render ||= ERB.new(template).result(binding)
end

#resource_typeObject



182
183
184
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 182

def resource_type
  resource.class.name.underscore
end

#sizeObject



93
94
95
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 93

def size
  SIZE_MAP[media]
end

#styleObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 122

def style
  @style ||= {
    padding: 2 * em,
    font_size: em,
    font_size_big: (
      case media
      when :facebook then
        3 * em
      when :pinterest then
        3.25 * em
      else
        2.5 * em
      end
    )
  }.with_indifferent_access
end

#subject_and_gradeObject



169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 169

def subject_and_grade
  grades = resource.grades.to_str
  subject = resource.subject == 'ela' ? 'ELA' : 'Math'
  case grades
  when 'Grade PK' then
    "#{subject}  Prekindergarten"
  when 'Grade K' then
    "#{subject}  Kindergarten"
  else
    "#{subject}  #{grades}"
  end
end

#templateObject



64
65
66
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 64

def template
  self.class.template
end

#title_sentencesObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 202

def title_sentences
  buffer = ''
  sentences = []
  resource.title.gsub(/(\w)-(\w)/, '\1- \2').split.each do |word|
    if (buffer + word).size < title_width_threshold
      buffer = [buffer, word].select(&:present?).join(' ')
      buffer = buffer.gsub(/(\w)- (\w)/, '\1-\2')
    else
      sentences << buffer
      buffer = word
    end
  end
  sentences << buffer unless buffer.empty?
end

#title_top_marginObject



165
166
167
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 165

def title_top_margin
  header_with_two_lines? ? 3 * style[:padding] : 2 * style[:padding]
end

#title_width_thresholdObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/lcms/engine/svg_social_thumbnail.rb', line 139

def title_width_threshold
  case media
  when :twitter then
    31
  when :facebook then
    32
  when :pinterest then
    16
  else
    21
  end
end