Class: FetchIt
- Inherits:
-
Object
- Object
- FetchIt
- Defined in:
- lib/fetch_it.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get ⇒ Object
- #http_body ⇒ Object
- #http_get ⇒ Object
- #http_start(&block) ⇒ Object
-
#initialize(url, target) ⇒ FetchIt
constructor
A new instance of FetchIt.
- #make_directory_if_needed ⇒ Object
- #open_file_for_writing ⇒ Object
- #parsed_url ⇒ Object
Constructor Details
#initialize(url, target) ⇒ FetchIt
Returns a new instance of FetchIt.
10 11 12 |
# File 'lib/fetch_it.rb', line 10 def initialize(url, target) @url, @target = url, target end |
Class Method Details
.go(url, target) ⇒ Object
6 7 8 |
# File 'lib/fetch_it.rb', line 6 def self.go(url, target) new(url, target).get end |
Instance Method Details
#get ⇒ Object
41 42 43 44 |
# File 'lib/fetch_it.rb', line 41 def get make_directory_if_needed open_file_for_writing end |
#http_body ⇒ Object
26 27 28 |
# File 'lib/fetch_it.rb', line 26 def http_body http_get.body end |
#http_get ⇒ Object
22 23 24 |
# File 'lib/fetch_it.rb', line 22 def http_get http_start {|http| http.get(parsed_url.path)} end |
#http_start(&block) ⇒ Object
18 19 20 |
# File 'lib/fetch_it.rb', line 18 def http_start(&block) Net::HTTP.start(parsed_url.host, &block) end |
#make_directory_if_needed ⇒ Object
34 35 36 37 38 39 |
# File 'lib/fetch_it.rb', line 34 def make_directory_if_needed dir = File.dirname(@target) unless Kernel.test(?d, dir) FileUtils.mkdir_p(dir) end end |
#open_file_for_writing ⇒ Object
30 31 32 |
# File 'lib/fetch_it.rb', line 30 def open_file_for_writing File.open(@target, "w+") {|f| f << http_body} end |
#parsed_url ⇒ Object
14 15 16 |
# File 'lib/fetch_it.rb', line 14 def parsed_url URI.parse(@url) end |