Class: SmartsApi::Message
- Inherits:
-
Object
- Object
- SmartsApi::Message
- Defined in:
- lib/smarts_api/message.rb
Direct Known Subclasses
Instance Method Summary collapse
- #encode_hash(hash) ⇒ Object
- #hex_string_to_ascii(hex_str) ⇒ Object
- #log(msg) ⇒ Object
- #make_form(request_params) ⇒ Object
- #method ⇒ Object
- #sign_request(params) ⇒ Object
- #timestamp ⇒ Object
- #uri ⇒ Object
Instance Method Details
#encode_hash(hash) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/smarts_api/message.rb', line 66 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
55 56 57 58 59 60 |
# File 'lib/smarts_api/message.rb', line 55 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 |
#log(msg) ⇒ Object
18 19 20 |
# File 'lib/smarts_api/message.rb', line 18 def log(msg) SmartsApi::Configuration.logger.info msg if SmartsApi::Configuration.logger.respond_to?(:info) end |
#make_form(request_params) ⇒ Object
74 75 76 |
# File 'lib/smarts_api/message.rb', line 74 def make_form(request_params) request_params.map{|k,v| "#{k}=#{v}"}.join("&") end |
#method ⇒ Object
10 11 12 |
# File 'lib/smarts_api/message.rb', line 10 def method :post end |
#sign_request(params) ⇒ Object
22 23 24 25 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 |
# File 'lib/smarts_api/message.rb', line 22 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, SmartsApi::Configuration.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 |
#timestamp ⇒ Object
62 63 64 |
# File 'lib/smarts_api/message.rb', line 62 def Time.now.utc.iso8601.to_s # => "2012-06-21T18:15:09Z" end |
#uri ⇒ Object
14 15 16 |
# File 'lib/smarts_api/message.rb', line 14 def uri SmartsApi::Configuration.base_uri end |