Class: Less::Loader::Http

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

Instance Method Summary collapse

Instance Method Details

#get(options, callback) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/less/loader.rb', line 94

def get(options, callback)
  err = nil
  begin
    #less always sends options as an object, so no need to check for string
    uri_hash = {}
    uri_hash[:host]     = options['hostname'] ? options['hostname'] : options['host']
    path_components = options['path'] ? options['path'].split('?', 2) : ['']  #have to do this because node expects path and query to be combined
    if path_components.length > 1
      uri_hash[:path]   = path_components[0]
      uri_hash[:query]  = path_components[0]
    else
      uri_hash[:path]   = path_components[0]
    end
    uri_hash[:port]     = options['port'] ? options['port'] : Net::HTTP.http_default_port
    uri_hash[:scheme]   = uri_hash[:port] == Net::HTTP.https_default_port ? 'https' : 'http'  #have to check this way because of node's http.get
    case uri_hash[:scheme]
    when 'http'
      uri = URI::HTTP.build(uri_hash)
    when 'https'
      uri = URI::HTTPS.build(uri_hash)
    else
      raise Exception, 'Less import only supports http and https'
    end
    http = Net::HTTP.new uri.host, uri.port
    if uri.scheme == 'https'
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.use_ssl = true
    end
    response = nil
    http.start do |req|
      response = req.get(uri.to_s)
    end
    sr = ServerResponse.new(response.read_body, response.code.to_i)
    callback.call(sr)
  rescue => e
    err = e.message
  ensure
    ret = HttpNodeObj.new(err)
  end
  ret
end