Class: ScaleEngineAPI::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "", query = {}) ⇒ Request

Returns a new instance of Request.



8
9
10
11
# File 'lib/scaleengine/request.rb', line 8

def initialize(path = "", query = {})
  @path = path
  @query = query
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/scaleengine/request.rb', line 19

def method_missing(method, *args)
  query_new = query
  query_new[:command] += ".#{method}"
  
  params = args[0].is_a?(Hash) ? args[0] : {}
  params.delete(:command)
  params.keys.each do |x|
    query_new[x] = params[x]
  end
  
  Request.new(path, query_new)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/scaleengine/request.rb', line 6

def path
  @path
end

#queryObject

Returns the value of attribute query.



6
7
8
# File 'lib/scaleengine/request.rb', line 6

def query
  @query
end

Instance Method Details

#prepare_requestObject



32
33
34
35
36
# File 'lib/scaleengine/request.rb', line 32

def prepare_request
  query[:timestamp] = Time.now.to_i
  query[:signature] = Base64.strict_encode64(request_signature)
  body = {:json => JSON.generate(query), :multipart => true}
end

#request_signatureObject



38
39
40
41
42
43
44
45
# File 'lib/scaleengine/request.rb', line 38

def request_signature
  params = query
  json_params = JSON.generate(params)

  # Compute new SHA256 HMAC
  digest = OpenSSL::Digest::SHA256.new
  OpenSSL::HMAC.digest(digest, Configuration.secret_key, json_params)
end

#sendObject



13
14
15
16
17
# File 'lib/scaleengine/request.rb', line 13

def send
  body = prepare_request
  response = RestClient.post path, body
  Response.new(response)
end