class Happo::Utils

Public Class Methods

config() click to toggle source
# 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_file() click to toggle source
# 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 = {}) click to toggle source
# 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_styles() click to toggle source
# File lib/happo/utils.rb, line 89
def self.css_styles
  File.read(File.expand_path('../public/happo-styles.css', __FILE__))
end
favicon_as_base64() click to toggle source
# 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_code() click to toggle source
# File lib/happo/utils.rb, line 93
def self.jsx_code
  File.read(File.expand_path('../public/HappoApp.bundle.js', __FILE__))
end
last_result_summary() click to toggle source
# 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) click to toggle source
# File lib/happo/utils.rb, line 42
def self.normalize_description(description)
  Base64.strict_encode64(description).strip
end
page_title(diff_images, new_images) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/happo/utils.rb, line 64
def self.pluralize(count, singular, plural)
  if count == 1
    "#{count} #{singular}"
  else
    "#{count} #{plural}"
  end
end