Class: QC::API::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, extra_params = {}) ⇒ Request

Returns a new instance of Request.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/qc.rb', line 211

def initialize action, extra_params = {}
  @response = :not_requested

  @params = []
  @params << ['action', action]
  @params << ['access_key_id', QC::AccessKeyId]
  @params << ['signature_method', 'HmacSHA256']
  @params << ['signature_version', 1]
  zone_set = false
  extra_params.each_pair do |k,v|
    zone_set = true if k == 'zone'
    next if v.nil?
    @params << [k.to_s, v]
  end
  @params << ['zone', QC::Zone] unless zone_set
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



199
200
201
# File 'lib/qc.rb', line 199

def response
  @response
end

Class Method Details

.execute!(a, p = {}) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/qc.rb', line 201

def Request.execute! a, p = {}
  req = QC::API::Request.new a, p
  @result = req.execute!(QC::Key)
  if @result['ret_code'] == 0
    @result
  else
    raise ArgumentError.new("#{@result['ret_code']}: #{@result['message']}")
  end
end

Instance Method Details

#execute!(key) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/qc.rb', line 228

def execute!(key)
  _p = @params.dup
  _p << ['time_stamp', Time.now.utc.strftime("%FT%TZ")]
  @uri = URI.parse(API.json2url(key, _p.to_json))

  # Establish a SSL connection
  Net::HTTP.start(@uri.host, 443,
                  :use_ssl => true,
                  :verify_mode  => OpenSSL::SSL::VERIFY_PEER) do |https|

    # Verify additional the host name in the certificate to avoid MITM
    unless OpenSSL::SSL.verify_certificate_identity(https.peer_cert, 'qingcloud.com')
      raise 'Hostname in certifcate does NOT match! (MITM?)' 
    end

    # Verify the individual certificate
    unless https.peer_cert.to_s == QC::CERT_FILE
      raise "Certificate is NOT trustworthy!"
    end

    ####################################################################
    # Starting from this point I consider the SSL connection as safe!

    @response = https.request(Net::HTTP::Get.new(@uri.request_uri))

    # After this point we close the SSL connection.
    ####################################################################
  end

  JSON.parse(@response.body)
end