Class: Saaspose::Utils

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

Constant Summary collapse

DIGEST =
OpenSSL::Digest::Digest.new('sha1')

Class Method Summary collapse

Class Method Details

.call(uri, options = nil) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/saaspose/utils.rb', line 26

def call(uri, options=nil)
  signed_url = Utils.sign(uri, options)
  log(:debug, "calling: #{signed_url}")
  RestClient.get(signed_url, :accept => 'application/json')
rescue
  log(:error, "error: #{$!.inspect}")
  raise
end

.call_and_parse(uri, options = nil) ⇒ Object



35
36
37
38
# File 'lib/saaspose/utils.rb', line 35

def call_and_parse(uri, options=nil)
  response = call(uri, options)
  JSON.parse(response.body)
end

.call_and_save(uri, options, file) ⇒ Object



40
41
42
43
# File 'lib/saaspose/utils.rb', line 40

def call_and_save(uri, options, file)
  response   = response = call(uri, options)
  Utils.save_file(response, file)
end

.log(severity, message) ⇒ Object



49
50
51
# File 'lib/saaspose/utils.rb', line 49

def log(severity, message)
  Configuration.logger.send(severity, message) if Configuration.logger
end

.save_file(response_stream, local_file) ⇒ Object



45
46
47
# File 'lib/saaspose/utils.rb', line 45

def save_file(response_stream, local_file)
  File.open(local_file, "wb") { |file| file.write(response_stream.body) }
end

.sign(uri, options = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/saaspose/utils.rb', line 12

def sign(uri, options=nil)
  options = options ? options.dup : {}
  options.merge!(:appSID => Configuration.app_sid)
  url = "#{Configuration.product_uri}#{uri}"

  url << "?" << options.map{|key, value| "#{key}=#{CGI::escape(value.to_s)}"}.join("&")

  signature = OpenSSL::HMAC.digest(DIGEST, Configuration.app_key, url)
  signature = Base64.strict_encode64(signature).chop
  signature = URI::escape(signature, /[^A-z0-9]/)

  "#{url}&signature=#{signature}"
end