module Attache::API::V1

Constants

ATTACHE_DELETE_URL
ATTACHE_DOWNLOAD_URL
ATTACHE_SECRET_KEY
ATTACHE_UPLOAD_DURATION
ATTACHE_UPLOAD_URL
ATTACHE_URL

Public Instance Methods

attache_auth_options() click to toggle source
# File lib/attache/api/v1.rb, line 44
def attache_auth_options
  if ATTACHE_SECRET_KEY
    uuid = SecureRandom.uuid
    expiration = (Time.now + ATTACHE_UPLOAD_DURATION).to_i
    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), ATTACHE_SECRET_KEY, "#{uuid}#{expiration}")
    { uuid: uuid, expiration: expiration, hmac: hmac }
  else
    {}
  end
end
attache_delete(*paths) click to toggle source
# File lib/attache/api/v1.rb, line 37
def attache_delete(*paths)
  HTTPClient.post_content(
    URI.parse(ATTACHE_DELETE_URL),
    attache_auth_options.merge(paths: paths.join("\n"))
  )
end
attache_options(geometry, current_value, auth_options: true, placeholder: nil, data: {}) click to toggle source
# File lib/attache/api/v1.rb, line 55
def attache_options(geometry, current_value, auth_options: true, placeholder: nil, data: {})
  {
    data: {
      geometry: geometry,
      value: [*current_value],
      placeholder: [*placeholder],
      uploadurl: ATTACHE_UPLOAD_URL,
      downloadurl: ATTACHE_DOWNLOAD_URL,
    }.merge(data || {}).merge(auth_options == false ? {} : attache_auth_options),
  }
end
attache_retry_doing(max_retries, retries = 0, exception_class = Exception) { || ... } click to toggle source
# File lib/attache/api/v1.rb, line 67
def attache_retry_doing(max_retries, retries = 0, exception_class = Exception)
  yield
rescue exception_class
  if (retries += 1) <= max_retries
    max_sleep_seconds = Float(2 ** retries)
    sleep rand(0..max_sleep_seconds)
    retry
  end
  raise
end
attache_upload(readable) click to toggle source
# File lib/attache/api/v1.rb, line 19
def attache_upload(readable)
  uri = URI.parse(ATTACHE_UPLOAD_URL)
  original_filename =
    readable.respond_to?(:original_filename) && readable.original_filename ||
    readable.respond_to?(:path) && File.basename(readable.path) ||
    'noname'
  uri.query = { file: original_filename, **attache_auth_options }.collect {|k,v|
    CGI.escape(k.to_s) + "=" + CGI.escape(v.to_s)
  }.join('&')
  res = attache_retry_doing(3) { HTTPClient.post(uri, readable, {'Content-Type' => 'binary/octet-stream'}) }
  res.body
end
attache_url_for(path, geometry) click to toggle source
# File lib/attache/api/v1.rb, line 32
def attache_url_for(path, geometry)
  prefix, basename = File.split(path)
  [ATTACHE_DOWNLOAD_URL, prefix, CGI.escape(geometry), CGI.escape(basename)].join('/')
end