Class: FetchIt

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_it.rb

Class Method Summary collapse

Instance Method Summary collapse

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

#getObject



41
42
43
44
# File 'lib/fetch_it.rb', line 41

def get
  make_directory_if_needed
  open_file_for_writing
end

#http_bodyObject



26
27
28
# File 'lib/fetch_it.rb', line 26

def http_body
  http_get.body
end

#http_getObject



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_neededObject



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_writingObject



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_urlObject



14
15
16
# File 'lib/fetch_it.rb', line 14

def parsed_url
  URI.parse(@url)
end