Class: DiffuxCI::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/diffux_ci/utils.rb

Class Method Summary collapse

Class Method Details

.configObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/diffux_ci/utils.rb', line 8

def self.config
  @@config ||= {
    'snapshots_folder' => './snapshots',
    'source_files' => [],
    'stylesheets' => [],
    'port' => 4567,
    'driver' => :firefox,
    'viewports' => {
      'large' => {
        'width' => 1024,
        'height' => 768
      },
      'medium' => {
        'width' => 640,
        'height' => 888
      },
      'small' => {
        'width' => 320,
        'height' => 444
      }
    }
  }.merge(config_from_file)
end

.config_from_fileObject



32
33
34
35
# File 'lib/diffux_ci/utils.rb', line 32

def self.config_from_file
  config_file_name = ENV['DIFFUX_CI_CONFIG_FILE'] || '.diffux_ci.yaml'
  YAML.load(ERB.new(File.read(config_file_name)).result)
end

.construct_url(absolute_path, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/diffux_ci/utils.rb', line 50

def self.construct_url(absolute_path, params = {})
  query = URI.encode_www_form(params) unless params.empty?

  URI::HTTP.build(host: 'localhost',
                  port: config['port'],
                  path: absolute_path,
                  query: query).to_s
end

.current_snapshotsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/diffux_ci/utils.rb', line 59

def self.current_snapshots
  prepare_file = lambda do |file|
    viewport_dir = File.expand_path('..', file)
    description_dir = File.expand_path('..', viewport_dir)
    {
      description: Base64.decode64(File.basename(description_dir)),
      viewport: File.basename(viewport_dir).sub('@', ''),
      file: file
    }
  end

  snapshots_folder = DiffuxCI::Utils.config['snapshots_folder']
  diff_files = Dir.glob("#{snapshots_folder}/**/diff.png")
  baselines = Dir.glob("#{snapshots_folder}/**/baseline.png")

  {
    diffs: diff_files.map(&prepare_file),
    baselines: baselines.map(&prepare_file)
  }
end

.normalize_description(description) ⇒ Object



37
38
39
# File 'lib/diffux_ci/utils.rb', line 37

def self.normalize_description(description)
  Base64.encode64(description).strip
end

.path_to(description, viewport_name, file_name) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/diffux_ci/utils.rb', line 41

def self.path_to(description, viewport_name, file_name)
  File.join(
    config['snapshots_folder'],
    normalize_description(description),
    "@#{viewport_name}",
    file_name
  )
end