module Mangdown::Tools
Common helpers
Constants
- TYPHOEUS_OPTIONS
rubocop:disable Metrics/LineLength
Public Class Methods
file_join(safe_path, *unsafe_parts)
click to toggle source
# File lib/mangdown/support/tools.rb, line 42 def file_join(safe_path, *unsafe_parts) now_safe_parts = unsafe_parts.map { |part| part.tr('/', '') } File.join(safe_path, *now_safe_parts) end
get(uri)
click to toggle source
# File lib/mangdown/support/tools.rb, line 25 def get(uri) response = Typhoeus.get(uri) return response.body if response.success? raise Mangdown::Error, "Failed to GET: #{uri}" end
get_doc(uri)
click to toggle source
rubocop:enable Metrics/LineLength
# File lib/mangdown/support/tools.rb, line 20 def get_doc(uri) data = get(uri) @doc = Nokogiri::HTML(data) end
get_root(uri)
click to toggle source
# File lib/mangdown/support/tools.rb, line 33 def get_root(uri) uri = Addressable::URI.parse(uri) @root = "#{uri.scheme}://#{uri.host}" end
hydra_streaming(objects, hydra_opts = {}) { |:before, obj| ... }
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/mangdown/support/tools.rb, line 65 def hydra_streaming(objects, hydra_opts = {}) hydra = Typhoeus::Hydra.new(hydra_opts) requests = objects.map do |obj| next unless yield(:before, obj) request = typhoeus(obj.uri) request.on_headers do |response| status = response.success? ? :succeeded : :failed yield(status, obj, response) end request.on_body do |chunk| yield(:body, obj, chunk) end request.on_complete do |_response| yield(:complete, obj) end hydra.queue(request) request end.compact hydra.run requests end
image_extension(path)
click to toggle source
# File lib/mangdown/support/tools.rb, line 55 def image_extension(path) path = path.to_s return unless File.exist?(path) mime = MimeMagic.by_magic(File.open(path, 'r')) mime_to_extension(mime) end
relative_or_absolute_path(*sub_paths)
click to toggle source
# File lib/mangdown/support/tools.rb, line 38 def relative_or_absolute_path(*sub_paths) Pathname.new(Dir.pwd).join(*sub_paths) end
typhoeus(uri)
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/mangdown/support/tools.rb, line 92 def typhoeus(uri) Typhoeus::Request.new(uri, TYPHOEUS_OPTIONS) end
valid_path_name(name)
click to toggle source
# File lib/mangdown/support/tools.rb, line 48 def valid_path_name(name) name.to_s.sub(/(\d+)(\.\w+)*\Z/) do digits, ext = Regexp.last_match[1..2] digits.to_i.to_s.rjust(5, '0') + ext.to_s end end
Private Class Methods
mime_to_extension(mime)
click to toggle source
# File lib/mangdown/support/tools.rb, line 98 def mime_to_extension(mime) return unless mime.mediatype == 'image' mime.subtype end