Class: Npm2Rpm::Download

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Download

get url to local file, return local file name



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

def initialize url
  puts "Download #{url.inspect}"
  @uri = URI url
  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.



13
14
15
# File 'lib/npm2rpm/download.rb', line 13

def content
  @content
end

Instance Method Details

#filenameObject



43
44
45
# File 'lib/npm2rpm/download.rb', line 43

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

#saveObject



39
40
41
42
# File 'lib/npm2rpm/download.rb', line 39

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