Module: Less::Loader::Http

Defined in:
lib/less/loader.rb

Overview

:nodoc:

Defined Under Namespace

Classes: HttpGetResult, ServerResponse

Class Method Summary collapse

Class Method Details

.get(options, callback) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/less/loader.rb', line 112

def self.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
    callback.call ServerResponse.new(response.read_body, response.code.to_i)
  rescue => e
    err = e.message
  ensure
    ret = HttpGetResult.new(err)
  end
  ret
end