Class: Npmfed::Download
- Inherits:
-
Object
- Object
- Npmfed::Download
- Defined in:
- lib/npmfed/download.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Instance Method Summary collapse
- #filename ⇒ Object
-
#initialize(url) ⇒ Download
constructor
get url to local file, return local file name.
- #save ⇒ Object
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.(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
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/npmfed/download.rb', line 8 def content @content end |
Instance Method Details
#filename ⇒ Object
42 43 44 |
# File 'lib/npmfed/download.rb', line 42 def filename @filename ||= File.basename(@uri.path) end |
#save ⇒ Object
38 39 40 41 |
# File 'lib/npmfed/download.rb', line 38 def save File.open(self.filename, "w+") { |f| f.write @content } self end |