Class: SmartsApi::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ Message

Returns a new instance of Message.



14
15
16
# File 'lib/smarts_api/message.rb', line 14

def initialize(logger=nil)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/smarts_api/message.rb', line 10

def logger
  @logger
end

Instance Method Details

#encode_hash(hash) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/smarts_api/message.rb', line 70

def encode_hash(hash)
  new = []
  hash.each do |k,v|
    new << k.to_s+"="+url_encode(v)
  end
  return new.join("&")
end

#hex_string_to_ascii(hex_str) ⇒ Object



59
60
61
62
63
64
# File 'lib/smarts_api/message.rb', line 59

def hex_string_to_ascii hex_str
  #we need to iterate our hex-string and convert each pair to a hex-number.  Then evaluate that as ascii code and replace with the corresponding ascii character.
  ascii_str = ''
  hex_str.split('').in_groups_of(2){|c| ascii_str << (c[0]+c[1]).hex.chr }
  ascii_str
end

#make_form(request_params) ⇒ Object



78
79
80
# File 'lib/smarts_api/message.rb', line 78

def make_form(request_params)
  request_params.map{|k,v| "#{k}=#{v}"}.join("&")
end

#methodObject



18
19
20
# File 'lib/smarts_api/message.rb', line 18

def method
  :post
end

#sign_request(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smarts_api/message.rb', line 26

def sign_request(params)
  #Below you will find the reverse-engineered ruby implementation of Sparkling Logic's security signing algorithm.
  #this was translated from a javascript library on their documentation site, then

  #The params object include the following parameters, in this order: AppId, pwd, reqData, reqTime, userID, and workspaceID.
  #naming of the parameters is significant, and case-sensitive.

  #combine the parameters into a long encoded string (again, order of elements is critical)
  encoded_parts = []
  http = URI.parse(uri)
  encoded_parts[0] = method.to_s.upcase
  encoded_parts[1] = http.host
  encoded_parts[2] = http.request_uri
  encoded_parts[3] = encode_hash(params)

  encoded = encoded_parts.join("\n") #Join parts with newline character

  #5 Digest the encode string with SHA256, using the pre-shared AccessKey as the digest key.
  digest = OpenSSL::Digest::Digest.new('sha256')
  hash = OpenSSL::HMAC.hexdigest(digest, access_key, encoded)

  #6 OpenSSL::Digest correctly translates the Hash to a string of bytes.  Sparkling Logic's algorithm ...unexpectedle converts the hex value to the corresponding ascii code.
  #So we need to iterate each pair in our byte string and convert to ascii. this is a little ugly, but necessary
  #7 AND THEN we encode that string to base64.  Not sure why, exactly...
  signature =  Base64.encode64(hex_string_to_ascii(hash))
  #8 re-URL_encode the string and remove line-endings.  Again, for absolutely no logical reason.  I think the javascript version did this automatically.
  signature =  url_encode(signature)
  signature =  signature.gsub("%0A", "" )
  return signature

end

#timestampObject



66
67
68
# File 'lib/smarts_api/message.rb', line 66

def timestamp
  Time.now.utc.iso8601.to_s # => "2012-06-21T18:15:09Z"
end

#uriObject



22
23
24
# File 'lib/smarts_api/message.rb', line 22

def uri
  base_uri
end