Class: Ymlbill::HtmlRenderer
- Inherits:
-
Object
- Object
- Ymlbill::HtmlRenderer
- Defined in:
- lib/ymlbill/html_renderer.rb
Instance Method Summary collapse
-
#initialize(template_path:, base_dir: nil) ⇒ HtmlRenderer
constructor
A new instance of HtmlRenderer.
- #logo_path(path) ⇒ Object
- #money(amount, currency = 'EUR') ⇒ Object
- #render(data:) ⇒ Object
Constructor Details
#initialize(template_path:, base_dir: nil) ⇒ HtmlRenderer
Returns a new instance of HtmlRenderer.
7 8 9 10 |
# File 'lib/ymlbill/html_renderer.rb', line 7 def initialize(template_path:, base_dir: nil) @template_path = template_path @base_dir = base_dir end |
Instance Method Details
#logo_path(path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ymlbill/html_renderer.rb', line 29 def logo_path(path) return nil unless path full_path = @base_dir ? File.join(@base_dir, path) : File.(path) full_path = File.(full_path) return nil unless File.exist?(full_path) ext = File.extname(full_path).downcase mime_type = case ext when '.jpg', '.jpeg' then 'image/jpeg' when '.svg' then 'image/svg+xml' when '.gif' then 'image/gif' when '.webp' then 'image/webp' else 'image/png' end content = File.read(full_path, mode: 'rb') base64 = [content].pack('m0') "data:#{mime_type};base64,#{base64}" rescue StandardError => e warn "Warning: Could not load logo from #{path}: #{e.}" nil end |
#money(amount, currency = 'EUR') ⇒ Object
23 24 25 26 27 |
# File 'lib/ymlbill/html_renderer.rb', line 23 def money(amount, currency = 'EUR') Money.new(amount * 100, currency.to_s.upcase).format( symbol: true, decimal_mark: '.', thousands_separator: ' ' ) end |
#render(data:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ymlbill/html_renderer.rb', line 12 def render(data:) template_content = File.read(@template_path) data = JSON.parse(data.to_json, object_class: OpenStruct) erb = ERB.new(template_content, trim_mode: '-') erb.result(binding) rescue SyntaxError => e raise TemplateRenderError, "Template syntax error: #{e.}" end |