Module: Ustyle

Defined in:
lib/ustyle.rb,
lib/ustyle/icons.rb,
lib/ustyle/utils.rb,
lib/ustyle/deploy.rb,
lib/ustyle/engine.rb,
lib/ustyle/version.rb,
lib/ustyle/helpers/icon_helper.rb,
lib/ustyle/middleware/icon_middleware.rb

Defined Under Namespace

Modules: Helpers, Icons Classes: Engine, IconMiddleware, Railtie

Constant Summary collapse

REGION =
'eu-west-1'
BUCKET =
'uswitch-assets-eu'
CLOUDFRONT_DISTRIBUTION =
'E3F1XI0HIG20E0'
ONE_YEAR_IN_S =
1 * 365 * 24 * 60 * 60
ONE_YEAR_FROM_NOW =
Time.now + ONE_YEAR_IN_S
VERSION =
version

Class Method Summary collapse

Class Method Details

.asset_digest(path) ⇒ Object



17
18
19
# File 'lib/ustyle/utils.rb', line 17

def self.asset_digest path
  path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end

.asset_directoriesObject



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

def asset_directories
  %w(stylesheets javascripts images fonts)
end

.assets_pathObject



38
39
40
# File 'lib/ustyle.rb', line 38

def assets_path
  @assets_path ||= File.join gem_path, "vendor", "assets"
end

.autoprefixer_configObject



50
51
52
53
54
# File 'lib/ustyle.rb', line 50

def autoprefixer_config
  file   = File.join Ustyle.gem_path, 'config/autoprefixer.yml'
  params = YAML.load_file(file).symbolize_keys
  params
end

.cloudfront_url(path, type, versioned = true, digest = true) ⇒ Object



10
11
12
13
14
15
# File 'lib/ustyle/utils.rb', line 10

def self.cloudfront_url path, type, versioned = true, digest = true
  File.join "https://assets0.uswitch.com/s3/uswitch-assets-eu/ustyle/",
            (versioned ? Ustyle::VERSION : ""),
            folder_by_type(type),
            (digest ? asset_digest(path) : path)
end

.digestObject



29
30
31
# File 'lib/ustyle/utils.rb', line 29

def self.digest
  Digest::SHA1.hexdigest Ustyle::VERSION
end

.folder_by_type(type) ⇒ Object



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

def self.folder_by_type type
  case type
  when :image
    "images"
  when :font
    "fonts"
  else ""
  end
end

.gem_pathObject



34
35
36
# File 'lib/ustyle.rb', line 34

def gem_path
  @gem_path ||= File.expand_path "..", File.dirname(__FILE__)
end

.invalidate(files) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ustyle/deploy.rb', line 24

def self.invalidate files
  cloudfront = Aws::CloudFront::Client.new(region: REGION)
  cloudfront.create_invalidation(
    distribution_id: CLOUDFRONT_DISTRIBUTION,
    invalidation_batch: {
      paths: {
        quantity: files.length,
        items: files
      },
      caller_reference: "ustyle invalidation at #{Time.now.to_s}"
    }
  )
end

.load!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ustyle.rb', line 11

def load!
  require "ustyle/sass_functions"
  require "ustyle/icons"
  require "ustyle/helpers/icon_helper"

  if rails?
    require "ustyle/engine"
  elsif sprockets?
    require "ustyle/sprockets"
    require "ustyle/sinatra"
  end

  ::Sass.load_paths << File.join(assets_path, "stylesheets")
end

.mime_type_for(asset) ⇒ Object



21
22
23
# File 'lib/ustyle/utils.rb', line 21

def self.mime_type_for asset
  MIME::Types.type_for( asset ).first.content_type
end

.production?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/ustyle/utils.rb', line 6

def self.production?
  ENV['RACK_ENV'] == 'production' || ENV['RAILS_ENV'] == 'production' || ENV['NODE_ENV'] == 'production' || false
end

.rails?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ustyle.rb', line 30

def rails?
  defined?(::Rails)
end

.s3Object



38
39
40
# File 'lib/ustyle/deploy.rb', line 38

def self.s3
  @conn ||= Aws::S3::Resource.new(region: REGION)
end

.s3_upload(to, from, content_type, bucket = BUCKET) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ustyle/deploy.rb', line 12

def self.s3_upload to, from, content_type, bucket = BUCKET
  bucket = s3.bucket(bucket)
  object = bucket.object(to)
  object.put(
    body: open(from),
    content_type: content_type,
    acl: 'public-read',
    cache_control: "max-age=#{ONE_YEAR_IN_S}, public",
    expires: ONE_YEAR_FROM_NOW.httpdate
  )
end

.sprockets?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ustyle.rb', line 26

def sprockets?
  defined?(::Sprockets)
end

.sprockets_envObject



46
47
48
# File 'lib/ustyle.rb', line 46

def sprockets_env
  @sprockets_env ||= ::Sprockets::Environment.new
end

.versioned_path(file) ⇒ Object



25
26
27
# File 'lib/ustyle/utils.rb', line 25

def self.versioned_path file
  File.join "ustyle", Ustyle::VERSION, file
end