Class: Gemgento::AssetFile

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gemgento/asset_file.rb

Overview

Author:

  • Gemgento LLC

Class Method Summary collapse

Class Method Details

.from_url(url) ⇒ TempFile

Get file from Magento.

Parameters:

  • url (String)

Returns:

  • (TempFile)


44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/gemgento/asset_file.rb', line 44

def self.from_url(url)
  if Gemgento::Config[:magento][:auth_username].blank?
    open(url)
  else
    open(url, http_basic_authentication: [
                Gemgento::Config[:magento][:auth_username],
                Gemgento::Config[:magento][:auth_password]
            ]
    )
  end
end

.valid_url(url) ⇒ Boolean

Check that a url is valid. Assumes url is pointing to Magento installation.

Parameters:

  • url (String)

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/gemgento/asset_file.rb', line 22

def self.valid_url(url)
  uri = URI.parse(url)
  req = Net::HTTP::Get.new(uri)

  unless Gemgento::Config[:magento][:auth_username].blank?
    req.basic_auth Gemgento::Config[:magento][:auth_username], Gemgento::Config[:magento][:auth_password]
  end

  http = Net::HTTP.new(uri.hostname, uri.port)
  http.use_ssl = uri.port == 443
  res = http.start  do |http|
    http.use_ssl = uri.port == 443
    http.request(req)
  end

  return res.code == '200'
end