Module: Rai::Helpers

Defined in:
lib/rai/helpers.rb

Instance Method Summary collapse

Instance Method Details

#get_resolutionObject



3
4
5
6
# File 'lib/rai/helpers.rb', line 3

def get_resolution
  settings.resolutions.sort!{|x,y| y <=> x }
  get_resolution_from_cookie || get_resolution_from_user_agent
end


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

def get_resolution_from_cookie
  return false unless res_cookie = request.cookies[settings.cookie_name]

  if res_cookie !~ /^[0-9]+[,]*[0-9\.]+$/
    response.delete_cookie settings.cookie_name
  else
    cookie_data = res_cookie.split(',')
    total_width = client_width = cookie_data[0].to_i
    # the device's pixel density factor (physical pixels per CSS pixel)
    pixel_density = cookie_data[1] ? cookie_data[1].to_i : 1
    # by default use the largest supported break-point
    resolution = settings.resolutions.first

    total_width = client_width * pixel_density

    resolution =
      settings.resolutions.select {|res| total_width <= res}.last
    resolution ||= settings.resolutions.first

    if total_width > settings.resolutions.first
      resolution *= pixel_density
    end

    return resolution
  end
end

#get_resolution_from_user_agentObject



35
36
37
# File 'lib/rai/helpers.rb', line 35

def get_resolution_from_user_agent
  is_mobile ? settings.resolutions.min : settings.resolutions.max
end

#is_mobileObject



39
40
41
# File 'lib/rai/helpers.rb', line 39

def is_mobile
  request.user_agent.downcase.match(/mobi|android|touch|mini/)
end

#update_imageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/rai/helpers.rb', line 43

def update_image
  # Refresh the image if the cached version is too old
  if File.exists? @cached_image
    return unless settings.watch_cache
    return if File.mtime(@cached_image) >= File.mtime(@requested_image)
    File.delete @cached_image
  end

  begin
    image = MiniMagick::Image.open(@requested_image)
  rescue Exception => e
    halt(500, "Error loading image: #{e}")
  end

  # Do we need to downscale the image?
  if image[:width] <= @resolution
    @cached_image = @requested_image
    return
  end

  cache_dir = File.dirname(@cached_image)

  begin
    FileUtils.mkdir_p(cache_dir, :mode => 0755) unless File.directory?(cache_dir)
  rescue SystemCallError => e
    halt(500, "Unable to create caching directory. #{e}")
  rescue => e
    halt(500, "Unable to create caching directory. #{e}")
  end

  ratio      = image["%[fx:w/h]"].to_f
  new_width  = @resolution
  new_height = (new_width*ratio).ceil

  if params[:image_type] == 'jpg'
    image.combine_options do |c|
      c.interlace 'plane'
      c.quality settings.jpg_quality
    end
  end

  image.resize "#{new_width}x#{new_height}"

  if settings.sharpen
    radius    = 0.5
    sigma     = 0.5
    amount    = 1.0
    threshold = 0.02
    image.unsharp "#{radius}x#{sigma}+#{amount}+#{threshold}"
  end

  begin
    image.write @cached_image
  rescue Exception => e
    halt(500, "Error writing cache file: #{e}")
  end
end