Class: WebifyRuby::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/webify_ruby/convert.rb

Overview

Public: Core class of the model that converts and distributes if necessary.

Examples

WebifyRuby::Convert.new('../public/fonts/example.ttf', :css => true)
# => #<WebifyRuby::Convert:0x007f92ca0c12c8>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, dir: nil, css: nil, link_to: nil, unique_id: nil, html: nil, link_to_css: nil) ⇒ Convert

Public: Initialize a Convertion of font-file.

file - A String containing relative or full path of file to convert. :dir - A String indicating to the desired to save converted files (optional). :css - A String or Boolean value indicating a desired CSS behavior.

If present, it can be either directory path in String or Boolean.
If value is set to true, then a stylesheet file won't be created,
but code will become accessible as :styles attribute (optional).

:link_to - A String notation indicating how to prefix a font url in CSS (optional). :unique_id - A custom identifier which will separate different fonts (optional). :html - If present, it will create an HTML file in the given directory.

Note: CSS has to be set to generate a file too. (optional).

:link_to_css - Css file to link to. (optional).

Returns nothing. Raises Errno::ENOENT if the inputted file cannot be found. Raises Error if the inputted font file is not withing valid extensions. Raises Error::ENOENT if the directory of inputted file is not accessible.

Raises:

  • (Errno::ENOENT)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webify_ruby/convert.rb', line 61

def initialize(file, dir: nil, css: nil, link_to: nil, unique_id: nil, html: nil, link_to_css: nil)
  [file, dir, css, link_to, unique_id, html, link_to_css]

  @desired_dir = dir
  @css = css
  @link_to = link_to
  @unique_id = unique_id
  @html = html
  @link_to_css = link_to_css

  raise Errno::ENOENT, "The font file '#{file}' does not exist" unless File.exists?(file)
  @original_file = file

  raise Error, "The font file '#{file}' is not valid" unless WebifyRuby::EXT.include? File.extname(@original_file)

  @original_dir = File.dirname(@original_file)
  raise Errno::ENOENT, "Can't find directory '#{@original_dir}'" unless File.directory? @original_dir

  @result_dir = Dir.mktmpdir(nil, destination_dir)
  
  unless @unique_id.nil?
    custom_dir = File.join(File.expand_path('..', @result_dir), @unique_id)
    FileUtils.mv @result_dir, custom_dir
    FileUtils.rm_rf File.join(custom_dir, File.basename(@result_dir))
    @result_dir = custom_dir
  end

  FileUtils.cp(@original_file, @result_dir)

  @file = File.join(@result_dir, File.basename(@original_file))

  process

  
  if affected_files.to_a.length == 0
    # :nocov:
    WebifyRuby.logger.info "Host did not create any files\n@command\n#{@command}\n@output\n#{@output}\n"
    # :nocov:
  end

  generate_css unless @css.nil?
  generate_html unless @css_file.nil? or @html.nil?
end

Instance Attribute Details

#commandObject (readonly)

Public: Returns the String command that was executed.



19
20
21
# File 'lib/webify_ruby/convert.rb', line 19

def command
  @command
end

#cssObject (readonly)

Internal: Returns the String or Mixed value of desired CSS behavior.



31
32
33
# File 'lib/webify_ruby/convert.rb', line 31

def css
  @css
end

#css_fileObject (readonly)

Public: Returns the filepath of CSS file created if applicable.



37
38
39
# File 'lib/webify_ruby/convert.rb', line 37

def css_file
  @css_file
end

#desired_dirObject (readonly)

Internal: Returns the String directory path of the custom directory.



29
30
31
# File 'lib/webify_ruby/convert.rb', line 29

def desired_dir
  @desired_dir
end

#fileObject (readonly)

Public: Returns the String new file path of the inputted file.



15
16
17
# File 'lib/webify_ruby/convert.rb', line 15

def file
  @file
end

#generatedObject (readonly)

Public: Returns the Array of created files.



23
24
25
# File 'lib/webify_ruby/convert.rb', line 23

def generated
  @generated
end

#html_fileObject (readonly)

Public: Returns the html_file that bas been createde



39
40
41
# File 'lib/webify_ruby/convert.rb', line 39

def html_file
  @html_file
end

Internal: Returns the String path of CSS url prefix.



33
34
35
# File 'lib/webify_ruby/convert.rb', line 33

def link_to
  @link_to
end

Public: Returns the value of custom css file link



41
42
43
# File 'lib/webify_ruby/convert.rb', line 41

def link_to_css
  @link_to_css
end

#original_dirObject (readonly)

Internal: Returns the String directory path of file inputted.



25
26
27
# File 'lib/webify_ruby/convert.rb', line 25

def original_dir
  @original_dir
end

#original_fileObject (readonly)

Internal: Returns the String inputted file.



17
18
19
# File 'lib/webify_ruby/convert.rb', line 17

def original_file
  @original_file
end

#outputObject (readonly)

Public: Returns the String STDOUT from the binary.



21
22
23
# File 'lib/webify_ruby/convert.rb', line 21

def output
  @output
end

#result_dirObject (readonly)

Public: Returns the String directory path of the destination.



27
28
29
# File 'lib/webify_ruby/convert.rb', line 27

def result_dir
  @result_dir
end

#stylesObject (readonly)

Public: Returns the String CSS stylesheet code if possible.



35
36
37
# File 'lib/webify_ruby/convert.rb', line 35

def styles
  @styles
end

Instance Method Details

#affected_filesObject

Internal: Know files that have been touched by running binary command.

Returns the Array of affected files.



108
109
110
# File 'lib/webify_ruby/convert.rb', line 108

def affected_files
  Dir[@result_dir + '/*.{ttf,eot,woff,svg}'].reject { |f| f[@file] }
end

#generate_cssObject

Internal: Communicate with Css class and take care of stylesheet creation, code generation and distribution. Method generates Css if attribute is present and writes to a file if attribute possibly is a directory.

Returns the CSS filepath, code written or nothing.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/webify_ruby/convert.rb', line 127

def generate_css
  needs = affected_files.map { |m| File.extname(m)[1..-1].to_sym }
  
  original_ext = File.extname(@original_file)[1..-1]
  needs << original_ext.to_sym unless needs.include? original_ext

  WebifyRuby::Css.link_to = @link_to

  if should_write_css?
    WebifyRuby::Css.relative_from = @link_to ? nil : @css
  end
    
  css = WebifyRuby::Css.new(File.basename(@file, ".*"), @file, *needs)
  @styles = css.result

  @css_file = css.write @css if should_write_css?
end

#generate_htmlObject

Internal: Work with Html class and make a HTML5 Doc. Create a file under given directory. This method should not execute if there is no css file.

Returns the HTML filepath or nothing.



150
151
152
153
154
155
156
157
# File 'lib/webify_ruby/convert.rb', line 150

def generate_html
  return false if @css_file.nil? or @html.nil?
  
  css_link = @link_to_css.nil? ? @css_file : @link_to_css
  
  html = WebifyRuby::Html.new(css_link, @html)
  @html_file = html.html_file
end

#is_valid?Boolean

Internal: Try check if running a command resulted in a positive or negative output about the file you want to convert.

Returns the Boolean saying if file was valid to convert or not.

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/webify_ruby/convert.rb', line 116

def is_valid?
  false if not @output.include? 'Generating' or @output.include? 'Failed'
  true
end