Class: RubyCleanCSS::Exports::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-clean-css/exports.rb

Overview

:nodoc:

Defined Under Namespace

Classes: HttpGetResult, ServerResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttp

Returns a new instance of Http.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ruby-clean-css/exports.rb', line 174

def initialize
  @get = lambda { |global, options, callback|
    err = nil
    uri_hash = {}
    uri_hash[:host] = options['hostname'] || options['host']
    path = options['path'] || options['pathname'] || ''
    # We do this because node expects path and query to be combined:
    path_components = path.split('?', 2)
    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
    # We check this way because of node's http.get:
    uri_hash[:scheme] = uri_hash[:port] == Net::HTTP.https_default_port ? 'https' : 'http'
    case uri_hash[:scheme]
    when 'http'
      uri = URI::HTTP.build(uri_hash)
    when 'https'
      uri = URI::HTTPS.build(uri_hash)
    else
      raise(Exception, 'import only supports http and https')
    end
    http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == 'https'
      # Hurrah for insecurity
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.use_ssl = true
    end
    response = nil
    http.start { |req| response = req.get(uri.to_s) }
    begin
      callback.call(
        ServerResponse.new(response.read_body, response.code.to_i)
      )
    rescue => e
      err = e
    end
    HttpGetResult.new(err);
  }
end

Instance Attribute Details

#getObject (readonly)

Returns the value of attribute get.



172
173
174
# File 'lib/ruby-clean-css/exports.rb', line 172

def get
  @get
end