Module: Mangdown::Tools
Instance Method Summary collapse
- #file_type(path) ⇒ Object
- #get(uri) ⇒ Object
- #get_doc(uri) ⇒ Object
- #get_root(uri) ⇒ Object
- #hydra(objects) ⇒ Object
- #hydra_streaming(objects) ⇒ Object
- #relative_or_absolute_path(*sub_paths) ⇒ Object
Instance Method Details
#file_type(path) ⇒ Object
25 26 27 |
# File 'lib/mangdown/tools.rb', line 25 def file_type(path) FileMagic.new.file(path.to_s).slice(/^\w+/).downcase end |
#get(uri) ⇒ Object
12 13 14 |
# File 'lib/mangdown/tools.rb', line 12 def get(uri) Typhoeus.get(uri).body end |
#get_doc(uri) ⇒ Object
8 9 10 |
# File 'lib/mangdown/tools.rb', line 8 def get_doc(uri) @doc = ::Nokogiri::HTML(get(uri)) end |
#get_root(uri) ⇒ Object
16 17 18 |
# File 'lib/mangdown/tools.rb', line 16 def get_root(uri) @root = ::URI::join(uri, "/").to_s[0..-2] end |
#hydra(objects) ⇒ Object
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 |
# File 'lib/mangdown/tools.rb', line 54 def hydra(objects) hydra = Typhoeus::Hydra.hydra requests = objects.map {|obj| request = Typhoeus::Request.new(obj.uri) request.on_complete do |response| if response.success? yield(obj, response.body) if block_given? elsif response.timed_out? STDERR.puts "#{obj.uri}: got a time out" elsif response.code == 0 STDERR.puts "#{obj.uri}: #{response.return_message}" else STDERR.puts "#{obj.uri}: " + "HTTP request failed: #{response.code.to_s} \n" + "But maybe it succeeded..." if response.body yield(obj, response.body) if block_given? end end end hydra.queue(request) request } hydra.run requests end |
#hydra_streaming(objects) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mangdown/tools.rb', line 29 def hydra_streaming(objects) hydra = Typhoeus::Hydra.hydra requests = objects.map {|obj| next unless yield(:before, obj) request = Typhoeus::Request.new(obj.uri) request.on_headers do |response| yield((response.success? ? :succeeded : :failed), obj) end request.on_body do |chunk| yield(:body, obj, chunk) end request.on_complete do |response| yield(:complete, obj) end hydra.queue(request) request }.compact hydra.run requests end |
#relative_or_absolute_path(*sub_paths) ⇒ Object
20 21 22 23 |
# File 'lib/mangdown/tools.rb', line 20 def relative_or_absolute_path(*sub_paths) root = (sub_paths.first.to_s =~ /^\// ? sub_paths.shift : Dir.pwd) Pathname.new(root).join(*sub_paths) end |