Class: Happo::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/happo/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
31
# File 'lib/happo/utils.rb', line 8

def self.config
  @@config ||= {
    'snapshots_folder' => './snapshots',
    'source_files' => [],
    'stylesheets' => [],
    'public_directories' => [],
    '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



33
34
35
36
37
38
39
40
# File 'lib/happo/utils.rb', line 33

def self.config_from_file
  config_file_name = ENV['HAPPO_CONFIG_FILE'] || '.happo.yaml'
  if File.exist?(config_file_name)
    YAML.load(ERB.new(File.read(config_file_name)).result)
  else
    {}
  end
end

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



55
56
57
58
59
60
61
62
# File 'lib/happo/utils.rb', line 55

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

.css_stylesObject



89
90
91
# File 'lib/happo/utils.rb', line 89

def self.css_styles
  File.read(File.expand_path('../public/happo-styles.css', __FILE__))
end

.favicon_as_base64Object



84
85
86
87
# File 'lib/happo/utils.rb', line 84

def self.favicon_as_base64
  favicon = File.expand_path('../public/favicon.ico', __FILE__)
  "data:image/ico;base64,#{Base64.encode64(File.binread(favicon))}"
end

.jsx_codeObject



93
94
95
# File 'lib/happo/utils.rb', line 93

def self.jsx_code
  File.read(File.expand_path('../public/HappoApp.bundle.js', __FILE__))
end

.last_result_summaryObject



97
98
99
100
# File 'lib/happo/utils.rb', line 97

def self.last_result_summary
  YAML.load(File.read(File.join(
    self.config['snapshots_folder'], 'result_summary.yaml')))
end

.normalize_description(description) ⇒ Object



42
43
44
# File 'lib/happo/utils.rb', line 42

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

.page_title(diff_images, new_images) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/happo/utils.rb', line 72

def self.page_title(diff_images, new_images)
  title = []

  unless diff_images.count == 0
    title << pluralize(diff_images.count, 'diff', 'diffs')
  end

  title << "#{new_images.count} new" unless new_images.count == 0

  "#{title.join(', ')} ยท Happo"
end

.path_to(description, viewport_name, file_name) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/happo/utils.rb', line 46

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

.pluralize(count, singular, plural) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/happo/utils.rb', line 64

def self.pluralize(count, singular, plural)
  if count == 1
    "#{count} #{singular}"
  else
    "#{count} #{plural}"
  end
end