Class: Npmfed::Download

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Download

get url to local file, return local file name



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/npmfed/download.rb', line 10

def initialize url
#      puts "Download #{url.inspect}"
  @uri = case url
           when URI then url
           else
             URI url
         end
  http = Net::HTTP.new(@uri.host, @uri.port)
  if @uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    store = OpenSSL::X509::Store.new
    store.set_default_paths # Optional method that will auto-include the system CAs.
    crtpath = File.expand_path(File.join(File.dirname(__FILE__), "..", "registry.npmjs.org.crt"))
    store.add_file(crtpath)
    http.cert_store = store
  end
  response = http.request(Net::HTTP::Get.new(@uri.request_uri))
  case response.code.to_i
    when 200
    when 404
      abort "No such NPM module #{url}"
    else
      abort "HTTP error #{response.code.inspect}"
  end
  @content = response.body
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/npmfed/download.rb', line 8

def content
  @content
end

Instance Method Details

#filenameObject



42
43
44
# File 'lib/npmfed/download.rb', line 42

def filename
  @filename ||= File.basename(@uri.path)
end

#saveObject



38
39
40
41
# File 'lib/npmfed/download.rb', line 38

def save
  File.open(self.filename, "w+") { |f| f.write @content }
  self
end