Module: Rai::Helpers
- Defined in:
- lib/rai/helpers.rb
Instance Method Summary collapse
- #get_resolution ⇒ Object
- #get_resolution_from_cookie ⇒ Object
- #get_resolution_from_user_agent ⇒ Object
- #is_mobile ⇒ Object
- #update_image ⇒ Object
Instance Method Details
#get_resolution ⇒ Object
3 4 5 6 |
# File 'lib/rai/helpers.rb', line 3 def get_resolution settings.resolutions.sort!{|x,y| y <=> x } || get_resolution_from_user_agent end |
#get_resolution_from_cookie ⇒ Object
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 return false unless = request.[settings.] if !~ /^[0-9]+[,]*[0-9\.]+$/ response. settings. else = .split(',') total_width = client_width = [0].to_i # the device's pixel density factor (physical pixels per CSS pixel) pixel_density = [1] ? [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_agent ⇒ Object
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_mobile ⇒ Object
39 40 41 |
# File 'lib/rai/helpers.rb', line 39 def is_mobile request.user_agent.downcase.match(/mobi|android|touch|mini/) end |
#update_image ⇒ Object
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. 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 |