Class: Kronk::Player::Download
- Inherits:
-
Kronk::Player
- Object
- QueueRunner
- Kronk::Player
- Kronk::Player::Download
- Defined in:
- lib/kronk/player/download.rb
Overview
Instance Attribute Summary collapse
-
#dir ⇒ Object
Directory to write the files to.
Attributes inherited from Kronk::Player
#concurrency, #input, #mutex, #qps
Attributes inherited from QueueRunner
#count, #number, #queue, #reader_thread, #threads
Instance Method Summary collapse
- #ext_for(resp) ⇒ Object
-
#initialize(opts = {}) ⇒ Download
constructor
A new instance of Download.
- #make_name(uri) ⇒ Object
- #result(kronk) ⇒ Object
- #will_parse(resp) ⇒ Object
Methods inherited from Kronk::Player
#compare, #from_io, new_type, #request, #run, #trigger_result
Methods inherited from QueueRunner
#concurrently, #finish, #finished?, #kill, #on, #periodically, #start_input!, #stop_input!, #trigger, #until_finished, #yield_queue_item
Constructor Details
#initialize(opts = {}) ⇒ Download
Returns a new instance of Download.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/kronk/player/download.rb', line 15 def initialize opts={} super @counter = 0 @counter_mutex = Mutex.new require 'fileutils' default_dir = File.join Dir.pwd, "kronk-#{Time.now.to_i}" @dir = File.(opts[:dir] || default_dir) FileUtils.mkdir_p @dir end |
Instance Attribute Details
#dir ⇒ Object
Directory to write the files to.
13 14 15 |
# File 'lib/kronk/player/download.rb', line 13 def dir @dir end |
Instance Method Details
#ext_for(resp) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kronk/player/download.rb', line 67 def ext_for resp if will_parse(resp) Kronk.config[:render_lang].to_s == "ruby" ? "rb" : "json" elsif resp.stringify_opts[:show_headers] "http" else resp.ext end end |
#make_name(uri) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/kronk/player/download.rb', line 59 def make_name uri return unless uri parts = uri.path.to_s.sub(%r{^/}, "").split("/") parts = parts[-2..-1] || parts parts.join("-").sub(%r{[?.].*$}, "") end |
#result(kronk) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/kronk/player/download.rb', line 30 def result kronk output, name, ext = if kronk.diff name = make_name kronk.responses[0].uri name = "#{name}-#{make_name kronk.responses[1].uri}" [kronk.diff.formatted, name, "diff"] elsif kronk.response name = make_name kronk.response.uri [kronk.response.stringify, name, ext_for(kronk.response)] end return unless output && !output.empty? filename = nil @counter_mutex.synchronize do @counter += 1 filename = File.join(@dir, "#{@counter}-#{name}.#{ext}") end File.open(filename, "w"){|file| file.write output} @mutex.synchronize do $stdout.puts filename end end |
#will_parse(resp) ⇒ Object
80 81 82 83 84 |
# File 'lib/kronk/player/download.rb', line 80 def will_parse resp (resp.parser || resp.stringify_opts[:parser] || (resp.stringify_opts[:no_body] && resp.stringify_opts[:show_headers])) && !resp.stringify_opts[:raw] end |